ProgrammingModerateGoldman Sachs OA · Infosys

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a = 29, b = 15, x, c = 0
x = a ^ b
while (x > 0)
    c = c + (x & 1)
    x = x >> 1
end while
Print c
  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 1

Correct answer: B. 2

c counts the bits where a and b differ. 29 = 11101 and 15 = 01111, so a ^ b = 10010, which has two 1's: c = 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic