ProgrammingVery hardTCS NQT · Infosys · Capgemini pattern

What will be the output of the following pseudocode? (^ is bitwise XOR.)

What will be the output of the following pseudocode? (^ is bitwise XOR.)
Integer x = 25, y = 20
x = x ^ y
y = x ^ y
x = x ^ y
Print x, y
  1. A. 20 20
  2. B. 20 25
  3. C. 20 26
  4. D. 25 20

Correct answer: B. 20 25

This is the classic XOR swap: after the three XOR steps, x and y end up swapped without a temp variable, so x = 20 (originally y) and y = 25 (originally x). Thinking XOR leaves them unchanged gives the trap "25 20".

More Pseudocode and Code Tracing questions

Exams that ask this topic