Questions: Switch Statements and Case Selection

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

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
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
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
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
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.