ProgrammingHardTCS 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 a = 1, b = 100, steps = 0
while (a < b)
    a = a * 3
    b = b / 2
    steps = steps + 1
end while
Print steps, a, b
  1. A. 4 81 6
  2. B. 2 9 25
  3. C. 3 27 12
  4. D. 4 81 3

Correct answer: C. 3 27 12

Track (a, b) after each step: (3, 50), (9, 25), (27, 12). Now a ≥ b, so the loop stops after 3 steps: 3 27 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic