Java OCA OCP Practice Question 946

Question

After execution of the following code fragment, what are the values of the variables x, a, and b?

1. int x, a = 6, b = 7; 
2. x = a++ + b++; 
  • A. x = 15, a = 7, b = 8
  • B. x = 15, a = 6, b = 7
  • C. x = 13, a = 7, b = 8
  • D. x = 13, a = 6, b = 7


C.

Note

The assignment statement is evaluated as if it were

x = a + b; 
a = a + 1; 
b = b + 1; 

So the assignment to x is made using the sum of 6 + 7, giving 13.

After the addition, the values of a and b are incremented; the new values, 7 and 8, are stored in the variables.




PreviousNext

Related