Tuesday, September 20, 2005
Sounding the Death Knell
You know a blog is doomed when you begin to have people posting really strange posts such as this.
Anyway, to frustrate anyone who might be reading this, the following was what I posted on IVLE (NUS online thingy) 5 minutes ago.
Doubts on Mid-T, 2Oct: Q15 and 99. What are the values of b and sum after executing the following code segment?
int a = 5, b = 5;
int sum = 0;
sum = a++>++b?b++:a+b%a;
(A) b is 6, sum is 6
(B) b is 7, sum is 7
(C) b is 6, sum is 7
(D) b is 7, sum is 6
Same problem with understanding the expression "sum = a++>++b?b++:a+b%a".
I need to clarify, when is variable a incremented? Is it incremented after the whole line is read and computed OR is it incremented immediately after the part "a++>++b?..."
In the lecture 2 notes:
• If the increment or decrement operator is in a prefix position, the identifier is modified, and then the new value is used in evaluating the rest of the expression.
• If the increment or decrement operator is in a postfix position, the old value of the identifier is used to evaluate the rest of the expression, and then the identifier is modified.
What then defines the end of the expression?
Answer for Q9 is A.15. What is printed out by the following code fragment?
int i = 0;
if (i > 0 && 10 / i > 2)
printf(”123\n”);
else
printf(”456\n”);
(A) 123
(B) 456
(C) A compilation error occurs
(D) A runtime error occurs
I have some problem understanding the expression "i > 0 && 10 / i > 2" in the if statement.
1st ArgumentIf I check the operator precedence table, "10/i" would be done first. But wouldn't that result in a runtime error since i=0 (Division by zero)?
2nd ArgumentOr would it be better if I broke the expression into two parts "i > 0" and "10 / i > 2". Computing the first part of the expression would return a false value thus the second part is ignored. But the lack of parenthesis disturbs me (back to argument one where "/" is higher precedence than "&&" and ">").
Answer for Q15 is B.
-----------------------------------------------------------
Kelvin Lim out
@ |10:37 PM|