Bird
0
0

Why does Swift require switch statements to be exhaustive, either by covering all cases or including a default?

hard📝 Conceptual Q10 of 15
Swift - Control Flow
Why does Swift require switch statements to be exhaustive, either by covering all cases or including a default?
ATo allow fallthrough behavior between cases.
BTo make code shorter and easier to write.
CBecause Swift does not support if-else statements.
DTo prevent runtime errors by ensuring all values are handled.
Step-by-Step Solution
Solution:
  1. Step 1: Understand exhaustiveness in Swift switch

    Swift enforces exhaustiveness to guarantee all possible values are handled safely.
  2. Step 2: Evaluate other options

    Options B, C, and D are incorrect because they do not explain safety or exhaustiveness reasons.
  3. Final Answer:

    To prevent runtime errors by ensuring all values are handled. -> Option D
  4. Quick Check:

    Exhaustive switch = Safe code, no missing cases [OK]
Quick Trick: Exhaustiveness avoids unexpected runtime failures [OK]
Common Mistakes:
  • Thinking exhaustiveness is for code brevity
  • Believing Swift lacks if-else
  • Confusing exhaustiveness with fallthrough

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes