ProgrammingHardInfosys · Accenture pattern

What will be the output of the following pseudocode? (/ is integer division.)

What will be the output of the following pseudocode? (/ is integer division.)
Integer a = 1, b = 100, steps = 0
while (a < b)
    a = a * 2
    b = b - 10
    steps = steps + 1
end while
Print steps, a, b
  1. A. 6 64 40
  2. B. 5 32 50
  3. C. 7 128 30
  4. D. 6 32 40

Correct answer: A. 6 64 40

(a, b): (1, 100) → (2, 90) → (4, 80) → (8, 70) → (16, 60) → (32, 50) → (64, 40). After 6 steps a = 64 ≥ b = 40 and the loop stops. '5 32 50' stops one step early (32 < 50 still holds).

More Pseudocode and Code Tracing questions

Exams that ask this topic