Consider this code: switch(x) { case 1: print('one'); case 2: print('two'); break; case 3: print('three'); } When x equals 1, what is printed?
Aone
Bone and two (fall-through causes both cases to execute)
Cone, two, and three (all cases run when no break is present)
DNothing — case 1 has no break, so the switch statement is invalid
This is the classic fall-through scenario. Case 1 matches and prints 'one,' but because there is no `break` statement, execution falls through into case 2, printing 'two.' The `break` in case 2 then exits the switch before case 3 can run. Fall-through is not an error — it is a deliberate language feature — but it surprises programmers who expect case matching to behave like exclusive branches. Option D represents a misconception: missing `break` is syntactically valid, just behaviorally surprising.
Question 2 Multiple Choice
A program needs to respond differently based on a user's letter grade: 'A', 'B', 'C', 'D', or 'F'. Which control structure is most appropriate?
AAn else-if chain, because else-if handles all conditional logic
BA switch statement, because this is exactly the pattern switch is optimized for: matching a single value against a set of known discrete possibilities
CA while loop with embedded conditionals
DSeparate if statements with no else, to avoid fall-through risk
Switch statements are best suited for matching a single expression against a set of discrete, known values — days of the week, menu options, letter grades, error codes. This is precisely that pattern. Option A works but is less readable and idiomatic for this case. Switch makes the structure clear: one value, multiple labeled outcomes. Option D would execute multiple branches simultaneously, producing incorrect behavior.
Question 3 True / False
A switch statement's `default` case is mainly necessary when you have not listed most possible value the expression could take.
TTrue
FFalse
Answer: False
Including a `default` case is good practice even when you believe you have covered all possibilities. It handles unexpected inputs gracefully, makes your assumptions explicit in the code, and allows you to signal an error condition (rather than silently doing nothing) if a value reaches the default that should not exist. Omitting default because 'all cases are covered' is an assumption that breaks silently when unexpected input arrives.
Question 4 True / False
Stacking multiple case labels without code between them (e.g., `case 'Saturday': case 'Sunday': print('weekend'); break;`) is a legitimate use of fall-through to share code between cases.
TTrue
FFalse
Answer: True
This is intentional fall-through used productively. When two or more cases should execute exactly the same code, stacking their labels and providing a single code block is cleaner than duplicating the block. The fall-through here is deliberate: case 'Saturday' has no code, so execution falls through to case 'Sunday''s label and continues into the shared block. The `break` after the block exits the switch after handling either day.
Question 5 Short Answer
When should you choose an else-if chain over a switch statement, even when a switch would technically work?
Think about your answer, then reveal below.
Model answer: Else-if chains are better when conditions involve ranges (e.g., score >= 90), complex boolean expressions, or comparisons between two different variables — anything that cannot be expressed as equality to a single known value. Switch statements match one expression against discrete values, so they cannot natively handle 'between 80 and 90' without additional workarounds. Else-if is also necessary when your language's switch does not support the data type you're working with (e.g., floating-point numbers).
The practical heuristic: use switch when you're asking 'which specific value is this?' and else-if when you're asking 'which condition is true?' The two structures solve related but distinct problems, and choosing between them based on the shape of the decision makes code more readable and intentional.