Bird
0
0

Identify the error in this Swift switch statement:

medium📝 Debug Q14 of 15
Swift - Control Flow
Identify the error in this Swift switch statement:
let value = 3
switch value {
 case 1:
   print("One")
 case 2:
   print("Two")
 case 3:
   print("Three")
   fallthrough
 case 4:
   print("Four")
   fallthrough
 case 5:
   print("Five")
}
AMissing default case causes a compile error.
BMultiple fallthroughs cause a runtime error.
CNo error; code runs and prints Three, Four, Five.
DFallthrough cannot be used in the last case.
Step-by-Step Solution
Solution:
  1. Step 1: Exhaustiveness requirement

    Swift switch on Int must be exhaustive: cover all possible values or add default. Cases 1-5 don't cover all Ints, no default: compile error.
  2. Step 2: Fallthrough usage is valid

    Fallthroughs in cases 3 and 4 are allowed; no fallthrough in case 5 (correct); multiples OK, no runtime issues.
  3. Final Answer:

    Missing default case causes a compile error. -> Option A
  4. Quick Check:

    Non-exhaustive switch on Int = compile error [OK]
Quick Trick: Swift switch on Int needs default unless all cases covered [OK]
Common Mistakes:
  • Picking A: no fallthrough in last case
  • Picking B: multiple fallthroughs allowed
  • Picking C: ignores compile-time exhaustiveness error
  • Thinking value=3 makes it exhaustive

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes