OutputHard2 marks
What is the value of x1 if x = 5? x1 = ++x - x++ + --x;
Topic: Increment / decrement operators
Previous-year question practice database
Answer
Exam answer
x1 = 6
Explanation
Evaluate left to right, applying each operator the moment it is reached. ++x : prefix, so x becomes 6 and 6 is used x++ : postfix, so 6 is used and then x becomes 7 --x : prefix, so x becomes 6 and 6 is used x1 = 6 - 6 + 6 = 6 The tidy part of this question is that x also ends at 6, but that is coincidence — do not rely on it. Write the running value of x beside each term as you work and this whole family of questions 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