ProgrammingEasyTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer x = 10, s = 0
while (x > 0)
    s = s + x
    x = x - 3
end while
Print s, x
  1. A. 22 -2
  2. B. 22 1
  3. C. 21 -2
  4. D. 18 -2

Correct answer: A. 22 -2

The loop adds 10, 7, 4, 1 (s = 22); x then becomes −2 and the condition fails. The final x is the first non-positive value, not 1.

More Pseudocode and Code Tracing questions

Exams that ask this topic