ProgrammingModerateTCS NQT · Accenture

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 = 37, ones = 0, zeros = 0
while (n > 0)
    if (n mod 2 == 1) ones = ones + 1 else zeros = zeros + 1
    n = n / 2
end while
Print ones, zeros
  1. A. 3 5
  2. B. 2 4
  3. C. 3 3
  4. D. 4 2

Correct answer: C. 3 3

37 = 100101 in binary (6 digits): three 1's and three 0's. The loop stops at the leading 1, so no extra leading zeros are counted: 3 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic