ProgrammingHardTCS NQT · Infosys · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 1, s = 0
Repeat
    s = s + n
    n = n * 2 + 1
Until s >= 100
Print n, s
  1. A. 127 120
  2. B. 255 247
  3. C. 63 119
  4. D. 63 57

Correct answer: A. 127 120

Each pass adds n to s and then updates n: s runs 1, 4, 11, 26, 57, 120 while n becomes 3, 7, 15, 31, 63, 127. The loop stops when s first reaches 100 or more, after the sixth pass, so n and s are 127 and 120.

More Pseudocode and Code Tracing questions

Exams that ask this topic