Bird
0
0

Why does Swift require switch statements on enums to be exhaustive?

hard📝 Conceptual Q10 of 15
Swift - Control Flow
Why does Swift require switch statements on enums to be exhaustive?
ABecause Swift does not support default cases.
BTo ensure all possible cases are handled, preventing runtime errors.
CTo make code shorter and easier to write.
DTo allow switching on any type, not just enums.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Swift's safety goals

    Swift aims to prevent runtime errors by forcing all enum cases to be handled.
  2. Step 2: Analyze options

    To ensure all possible cases are handled, preventing runtime errors. correctly states that exhaustiveness ensures all cases are handled, avoiding runtime errors.
  3. Final Answer:

    To ensure all possible cases are handled, preventing runtime errors. -> Option B
  4. Quick Check:

    Exhaustiveness = safety from missing cases [OK]
Quick Trick: Exhaustive switch prevents missing cases bugs [OK]
Common Mistakes:
  • Thinking default is not allowed
  • Believing exhaustiveness is for code brevity
  • Confusing switch behavior with other languages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes