ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Integer n = 1024, c = 0
while (n > 1)
    n = n / 2
    c = c + 1
end while
Print c
  1. A. 9
  2. B. 10
  3. C. 11
  4. D. 1024

Correct answer: B. 10

n is repeatedly halved: 1024->512->256->...->1, which takes exactly log2(1024) = 10 halvings before n reaches 1 and the loop stops.

More Pseudocode and Code Tracing questions

Exams that ask this topic