1. Home
  2. Computer Science MCQs
  3. C Programming
  4. int x = 2; switch (x) { case 1: puts("a"); case 2…

Added 30 Nov -0001

int x = 2; switch (x) { case 1: puts("a"); case 2: puts("b"); case 3: puts("c"); } What is printed?

  1. b only, because the matching case block ends automatically at the next label
  2. a, b and c, because every case label above the match is also evaluated in sequence
  3. Nothing, because no case matches once the fall-through behaviour is detected
  4. b then c, because execution continues into the later labels without any break Correct answer

Correct answer

D. b then c, because execution continues into the later labels without any break

13 views

MCQSEXAM

Explanation

Switch fall-through means control enters at the matching label and keeps executing subsequent statements until a break, a return or the end of the switch, so x equal to 2 prints b and then c. Grouping several labels over one block is idiomatic, but a forgotten break remains a classic source of silent bugs. The compiler warning -Wimplicit-fallthrough catches most cases.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs