Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q6 of 15
Swift - Control Flow
Identify the error in this Swift code:
enum Animal { case cat, dog }
let pet: Animal = .cat
switch pet {
 case .cat: print("Cat")
}
ASwitch syntax is incorrect
BMissing default case or other enum cases in switch
CEnum Animal is not defined correctly
DVariable pet is not initialized
Step-by-Step Solution
Solution:
  1. Step 1: Check switch cases against enum

    Enum Animal has two cases: cat and dog, but switch only handles cat.
  2. Step 2: Identify missing cases or default

    Switch is not exhaustive because it misses .dog and has no default case.
  3. Final Answer:

    Missing default case or other enum cases in switch -> Option B
  4. Quick Check:

    Switch must be exhaustive with all cases or default [OK]
Quick Trick: All enum cases or default must be in switch [OK]
Common Mistakes:
  • Forgetting to add all enum cases
  • Assuming partial switch compiles
  • Ignoring need for default case

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes