ProgrammingVery hardGoldman Sachs OA · Amazon OA

What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift)

What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift)
Integer a = 13, b = 11, carry, rounds = 0
while (b != 0)
    carry = (a & b) << 1
    a = a ^ b
    b = carry
    rounds = rounds + 1
end while
Print a, rounds
  1. A. 24 2
  2. B. 24 4
  3. C. 6 4
  4. D. 24 3

Correct answer: B. 24 4

XOR adds without carries and (a & b) << 1 is the carry. (13, 11) → (6, 18) → (20, 4) → (16, 8) → (24, 0): the sum 24 appears after 4 rounds.

More Pseudocode and Code Tracing questions

Exams that ask this topic