ProgrammingVery hardGoldman Sachs OA · Amazon OA

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 = 0
for (each i from 1 to 10)
    x = x ^ i
end for
Integer y = 0
for (each i from 1 to 12)
    y = y ^ i
end for
Print x, y
  1. A. 1 0
  2. B. 11 13
  3. C. 11 12
  4. D. 10 12

Correct answer: C. 11 12

XOR of 1..n follows a cycle of 4: it equals n, 1, n + 1, 0 for n ≡ 0, 1, 2, 3 (mod 4). n = 10 ≡ 2 gives 11 and n = 12 ≡ 0 gives 12. Output 11 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic