Multiple choice technology programming languages

What does it print? public class CleverSwap { public static void main(String[] args) { int x = 1984; int y = 2001; x ^= y ^= x ^= y; System.out.println("x = " + x + "; y = " + y); } }

  1. x = 0; y = 1984

  2. x = 2001; y = 1984

  3. x = 2001; y = 0

  4. x = 1984; y = 2001

  5. x = 0; y = 0

  6. Error

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Java evaluates operands left-to-right. The statement x ^= y ^= x ^= y; is evaluated as x ^= (y ^= (x ^= y)). The initial value of x (1984) is loaded first for the outermost assignment. The innermost x ^= y sets x to 1984 ^ 2001. The middle y ^= x sets y to 2001 ^ (1984 ^ 2001) = 1984. Finally, the outer x ^= y sets x to 1984 ^ 1984 = 0.