ProgrammingModerateCapgemini · Accenture

What will be the output of the following pseudocode? (--n subtracts 1 from n and then gives the new value)

What will be the output of the following pseudocode? (--n subtracts 1 from n and then gives the new value)
Integer n = 5, s = 0
do
    s = s + n
while (--n > 2)
Print s, n
  1. A. 9 3
  2. B. 14 2
  3. C. 12 2
  4. D. 12 3

Correct answer: C. 12 2

--n decreases n before the comparison. The body adds 5, then n = 4 > 2, adds 4, then n = 3 > 2, adds 3, then n = 2 fails. s = 12 and n = 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic