ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
int x = 5, s = 0;
switch (x) {
    default: s += 1;
    case 1:  s += 10;
    case 2:  s += 100; break;
    case 3:  s += 1000;
}
printf("%d", s);
  1. A. 1111
  2. B. 1
  3. C. 101
  4. D. 111

Correct answer: D. 111

No case matches 5, so control jumps to default, wherever it is written. With no break it falls through case 1 and case 2 (1 + 10 + 100) and stops at that break. '1' assumes default ends the switch; '1111' ignores the break.

More C Programming questions

Exams that ask this topic