Bird
0
0

Which of the following Swift switch statements is syntactically correct and exhaustive for an enum Direction with cases north, south, east, and west?

easy📝 Syntax Q12 of 15
Swift - Control Flow
Which of the following Swift switch statements is syntactically correct and exhaustive for an enum Direction with cases north, south, east, and west?
Aswitch dir { case .north: print("North") case .south: print("South") }
Bswitch dir { case .north: print("North") case .south: print("South") case .east: print("East") case .west: print("West") }
Cswitch dir { case .north: print("North") case .south: print("South") case .east: print("East") }
Dswitch (dir) { case .north: print("North") case .south: print("South") default: print("Other") }
Step-by-Step Solution
Solution:
  1. Step 1: Check all enum cases covered

    The enum has four cases: north, south, east, west. The switch must cover all or use default.
  2. Step 2: Analyze each option

    switch dir { case .north: print("North") case .south: print("South") case .east: print("East") case .west: print("West") } covers all four cases explicitly, making it exhaustive and syntactically correct.
  3. Final Answer:

    switch with all four cases covered explicitly -> Option B
  4. Quick Check:

    All enum cases covered = D [OK]
Quick Trick: Cover all enum cases or use default to be exhaustive [OK]
Common Mistakes:
  • Missing some enum cases
  • Not using default when cases are incomplete
  • Assuming partial cases are enough

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes