OutputHard2 marks
Evaluate the following expression if the value of x = 2, y = 3 and z = 1. v = x + --z + y++ + y;
Topic: Increment / decrement operators
Previous-year question practice database
Answer
Exam answer
v = 9
Explanation
Evaluate left to right, applying each operator as it is reached. x : 2 --z : prefix, so z becomes 0 and 0 is used y++ : postfix, so 3 is used and then y becomes 4 y : now 4, so 4 is used v = 2 + 0 + 3 + 4 = 9 The whole question turns on the difference between prefix and postfix: --z changes z BEFORE its value is taken, while y++ takes the value FIRST and changes y afterwards — which is why the last y in the expression is 4 and not 3.
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