ProgrammingHardTCS Digital · Infosys pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0, i
for (i = 0; i < 10; i = i + 1)
    if (i mod 3 == 0)
        i = i + 2
    end if
    c = c + 1
end for
Print c, i
  1. A. 4 11
  2. B. 10 10
  3. C. 5 12
  4. D. 4 12

Correct answer: D. 4 12

Changing i inside the body skips values: i = 0 → 2 (c = 1) → 3 → 5 (c = 2) → 6 → 8 (c = 3) → 9 → 11 (c = 4) → the update makes i = 12, and 12 < 10 fails. So c = 4 and i = 12. '4 11' forgets the final i = i + 1 that runs before the failing test.

More Pseudocode and Code Tracing questions

Exams that ask this topic