OutputHard2 marks
Give the output of the following expression: a += a++ + ++a + --a + a--; when a = 7
Topic: Increment / decrement operators
Previous-year question practice database
Answer
Exam answer
a = 39
Explanation
Expand the shorthand: a += ... means a = a + (...). The a on the LEFT of the + is read first, while a is still 7. Now the bracket, left to right: a++ : postfix, so 7 is used and then a becomes 8 ++a : prefix, so a becomes 9 and 9 is used --a : prefix, so a becomes 8 and 8 is used a-- : postfix, so 8 is used and then a becomes 7 total = 7 + 9 + 8 + 8 = 32 Finally a = 7 + 32 = 39. Four operators in one expression makes this the hardest of its type. Write the running value of a beside each term as you go and it becomes mechanical.
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