ProgrammingHardProduct-company placement · GATE CS

What is printed?

What is printed?
int i = 5;
i = i++ + ++i;
System.out.print(i);
  1. A. 13
  2. B. 7
  3. C. 6
  4. D. 12

Correct answer: D. 12

Java evaluates operands left to right: i++ yields 5 (i becomes 6), then ++i makes i 7 and yields 7. The sum 12 is assigned, overwriting i. 13 applies the postfix increment after the assignment, 7 lets the increments win, and 6 is i just after i++.

More OOP with C++ and Java questions

Exams that ask this topic