ProgrammingVery hardGATE CS · TCS NQT

What is the value of s after this loop? int i, s = 0; for (i = 0; i < 5; i++) { switch (i % 3) { case 0: s += 1; case 1: s += 10; break; default: s += 100; } }

  1. A. 122
  2. B. 542
  3. C. 111
  4. D. 142

Correct answer: D. 142

Case 0 has no break, so it falls into case 1 and adds 1 + 10 = 11. The iterations i = 0..4 add 11, 10, 100, 11, 10, a total of 142. 122 comes from forgetting the fall-through.

More C Programming questions

Exams that ask this topic