In Java, after executing the following code what are the values of x, y and z?
int x,y=10, z=12;
x=y++ + z++;
(A)
x=22, y=10, z=12
(B)
x=24, y=10, z=12
(C)
x=24, y=11, z=13
(D)
x=22, y=11, z=13
Answer: (D)
Explanation:
x = y++ + z++;
As in post increment operator, first the value is assigned and then it is incremented, this statement can be re-written as:
x = y + z; y = y++; z = z++;
So, the value of x = 10 + 12 = 22, y = 10 + 1 = 11 and z = 12 + 1 = 13. Option (D) is correct.
Quiz of this Question
Please comment below if you find anything wrong in the above post
Article Tags :
Recommended Articles