ProgrammingHardAccenture · TCS · Infosys

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 = 98, y = 60
x = x ^ y
y = x ^ y
x = x ^ y
Print x, y
  1. A. 60 98
  2. B. 60 60
  3. C. 60 99
  4. D. 98 60

Correct answer: A. 60 98

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

More Pseudocode and Code Tracing questions

Exams that ask this topic