OutputHard2 marks
What is the value of y after evaluating the expression given below? y += ++y + y-- + --y; when int y = 8
Topic: Increment / decrement operators
Previous-year question practice database
Answer
Exam answer
y = 33
Explanation
Expand the shorthand: y += ... means y = y + (...). The y on the LEFT of the + is read first, while y is still 8. Now the bracket, left to right: ++y : prefix, so y becomes 9 and 9 is used y-- : postfix, so 9 is used and then y becomes 8 --y : prefix, so y becomes 7 and 7 is used total = 9 + 9 + 7 = 25 Finally y = 8 + 25 = 33. Note how the final assignment overwrites everything the increments did along the way. Write the running value of y beside each term and this type of question 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