OutputHard2 marks
Evaluate the expression when the value of x = 4: x *= --x + x++ + x;
Topic: Increment / decrement operators
Previous-year question practice database
Answer
Exam answer
x = 40
Explanation
Expand the shorthand: x *= ... means x = x * (...). The x on the LEFT of the * is read first, while x is still 4. Now the bracket, left to right: --x : prefix, so x becomes 3 and 3 is used x++ : postfix, so 3 is used and then x becomes 4 x : now 4, so 4 is used total = 3 + 3 + 4 = 10 Finally x = 4 * 10 = 40. The whole question rests on one rule: prefix changes the variable then uses it, postfix uses it then changes it. Write the running value of x beside each term as you go and mistakes disappear.
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