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

Correct answer: B. 2 2

x = 101 ^ 111 = 010 = 2. y = 7 & 2 = 2. x = 2 | 2 = 2. This looks like the XOR swap but uses & and |, so it is NOT a swap. Assuming a swap gives 7 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic