OutputHard2 marks
Evaluate the expression when x is 4: x += x++ * ++x % 2;
Topic: Increment operators in an expression
Previous-year question practice database
Answer
Exam answer
x = 4
Explanation
Expand the shorthand first: x += ... means x = x + (...). Java evaluates operands left to right, then applies precedence. The x on the left of + is read first: 4 x++ uses 4, then makes x = 5 (postfix: use, then change) ++x makes x = 6 and uses 6 (prefix: change, then use) Now apply precedence — * and % outrank +, and both associate left to right: (4 * 6) % 2 = 24 % 2 = 0 So x = 4 + 0 = 4 The final assignment overwrites everything the increments did, which is why x ends at 4 despite having been 5 and 6 along the way. Postfix versus prefix is the whole point of the question.
More from Revision Tour I
The full form of JVM is:2026Which of the following occupies 2 bytes of storage?2026In a statement c = c + (x * d + e); which variable is an accumulator?2026Assertion (A): The result of the Java expression 3 + 7/2 is 6.
Reason (R): According to the hierarchy of operators in Java, addition is done first followed by division.2026Evaluate the Java expression:
x = a * b % (++c) + (++a) + (--b);
if a = 7, b = 8, c = 22026System.out.println('Z' + 32); will display:2025