ProgrammingModerateAccenture · Capgemini pattern

What is printed?

What is printed?
int s = 0, i;
for (i = 0; i < 10; i++) {
    if (i % 3) continue;
    s += i;
}
printf("%d %d", s, i);
  1. A. 27 10
  2. B. 18 9
  3. C. 45 10
  4. D. 18 10

Correct answer: D. 18 10

continue skips the rest of the body but still runs i++. Only multiples of 3 reach s += i: 0 + 3 + 6 + 9 = 18. The loop ends when i becomes 10, and i keeps that value afterwards. 27 adds the non-multiples instead.

More C Programming questions

Exams that ask this topic