MCQHard1 mark
6
Give the output of the following:
Give the output of the following: int x = 2, y = 4, z = 1; int result = (++z) + y + (++x) + (z++); System.out.print(result);
Topic: Increment operators
Previous-year question practice database
Try it
Pick an option and check your answer. The full worked answer is below either way.
Answer
Exam answer
(a) 11.
Explanation
Evaluate left to right, applying each operator as it is reached: ++z : prefix, so z becomes 2 and 2 is used y : 4 ++x : prefix, so x becomes 3 and 3 is used z++ : postfix, so the CURRENT value 2 is used and then z becomes 3 result = 2 + 4 + 3 + 2 = 11 The last term is the trap: z is 2 at that moment, not 3, because postfix uses the value first and increments afterwards.
In words
11.
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