OutputHard2 marks
Evaluate the following if the value of x = 7, y = 5 x += x++ + x + ++y;
Topic: Increment operators
Previous-year question practice database
Answer
Exam answer
x = 28
Explanation
Expand the shorthand: x += ... means x = x + (...). The x on the left of the + is read first, while x is still 7. Then the bracket, left to right: x++ : postfix, so 7 is used and then x becomes 8 x : now 8, so 8 is used ++y : prefix, so y becomes 6 and 6 is used total = 7 + 8 + 6 = 21 Finally x = 7 + 21 = 28. Write the running value of each variable beside every term as you go — that habit alone answers every question of this type.
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