ProgrammingHardCapgemini · Infosys 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 = 30, y = 77
x = x ^ y
y = x ^ y
x = x ^ y
Print x, y
  1. A. 77 31
  2. B. 30 77
  3. C. 77 30
  4. D. 77 77

Correct answer: C. 77 30

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

More Pseudocode and Code Tracing questions

Exams that ask this topic