ProgrammingVery hardTCS NQT Advanced · Capgemini

What will be the output of the following pseudocode? (C-style for loop: the update i = i + 1 runs after each pass of the body)

What will be the output of the following pseudocode? (C-style for loop: the update i = i + 1 runs after each pass of the body)
Integer c = 0, i
for (i = 0; i < 10; i = i + 1)
    if (i mod 3 == 0)
        i = i + 2
    end if
    c = c + i
end for
Print c, i
  1. A. 45 10
  2. B. 18 10
  3. C. 26 12
  4. D. 26 11

Correct answer: C. 26 12

The body changes i and then the update adds 1 more. i goes 0 → 2, 3 → 5, 6 → 8, 9 → 11, so c = 2 + 5 + 8 + 11 = 26; the final update makes i = 12, which ends the loop. Output 26 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic