ProgrammingModerateTCS NQT · Accenture pattern

What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right shifts.)

What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right shifts.)
Integer a = 9, b = 14
a = a ^ b
b = a ^ b
a = a ^ b
Print a - b
  1. A. 5
  2. B. 0
  3. C. 23
  4. D. -5

Correct answer: A. 5

Three XORs swap the values (x ^ x = 0), so a = 14 and b = 9 and a - b = 5. Not noticing the swap gives -5.

More Pseudocode and Code Tracing questions

Exams that ask this topic