ProgrammingHardTCS NQT Advanced · Capgemini

What will be the output of the following pseudocode? (i++ gives the old value of i and then adds 1)

What will be the output of the following pseudocode? (i++ gives the old value of i and then adds 1)
Integer i = 0, c = 0
while (i++ < 5)
    c = c + i
end while
Print c, i
  1. A. 15 6
  2. B. 10 5
  3. C. 15 5
  4. D. 10 6

Correct answer: A. 15 6

i++ compares the OLD value and then increments. The tests use 0, 1, 2, 3, 4 (true) and 5 (false), so the body sees i = 1 to 5: c = 15. The failing test still increments, leaving i = 6: 15 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic