ProgrammingModerateAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 45, c = 0
while (n != 0)
    c = c + (n & 1)
    n = n >> 1
end while
Print c
  1. A. 3
  2. B. 5
  3. C. 6
  4. D. 4

Correct answer: D. 4

45 = 101101 in binary. The loop adds the lowest bit and shifts right, counting the 1 bits: 4. It runs once per bit (6 times).

More Pseudocode and Code Tracing questions

Exams that ask this topic