ProgrammingModerateTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer s = 0, i = 0
while (true)
    i = i + 1
    if (i mod 4 == 0) continue
    s = s + i
    if (s > 20) break
end while
Print i, s
  1. A. 6 17
  2. B. 8 24
  3. C. 7 24
  4. D. 7 28

Correct answer: C. 7 24

s grows 1, 3, 6, (4 skipped), 11, 17, 24; at i = 7 the sum exceeds 20 and the loop breaks. Including 4 would give 1 + 2 + 3 + 4 + 5 + 6 = 21 at i = 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic