int a;
int b;
a=1;
b=a++;
Explanation: a++
is a post increment, so the
increment happens after the value of the expression is determined.
The value of the expression a++
is the current value of
a, which is 1. So the value 1 is assigned to b, and therefore,
the correct answer is 1.
Explanation: a++
is a post increment, so the
increment happens after the value of the expression is determined.
a's value was 1, and after the increment, it is 2. Therefore,
the correct answer is 2.
For questions 3 through 5, assume the following code:
int x;
int y;
int z;
x=3;
y=4;
z = ++x * y++;
Explanation: We must determine the values being multiplied
together in the statement z=++x * y++;
Dealing with the left operand of the multiplication operator,
the expression is ++x
. This is a pre increment, so we
increment x before determining the result of the expression.
So x is incremented to 4, and this new value of x is
used as the left operand of the multiplication operator.
Dealing with the right operand of the multiplication operator,
the expression is y++
. This is a post increment, so
we determine the result of the expression before performing the
increment. The present value of y is 4, so that value is
used as the right operand of the multiplication operator.
So the expression is evaluated as z=4 * 4
. Therefore
the answer is 16.
Explanation: ++x
is a pre increment, so the
value of a x does get incremented. Therefore, the answer
is 4.
Explanation: y++
is a post increment, so the
value of y does get incremented. Therefore, the answer
is 5.
Return to the CS 222 Home Page.
Return to David's Home Page.
Return to Cayuga's Home Page.