Bird
Raised Fist0
Swiftprogramming~10 mins

Switch must be exhaustive in Swift - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the switch statement exhaustive for the enum Color.

Swift
enum Color {
    case red, green, blue
}

func describe(color: Color) {
    switch color {
    case .red:
        print("Red color")
    case .green:
        print("Green color")
    case [1]:
        print("Blue color")
    }
}
Drag options to blanks, or click blank then click option'
A.blue
B.yellow
C.black
D.white
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to include all enum cases in the switch.
Using a case that does not exist in the enum.
2fill in blank
medium

Complete the switch statement to handle all cases of the enum Direction.

Swift
enum Direction {
    case north, south, east, west
}

func move(direction: Direction) {
    switch direction {
    case .north:
        print("Moving north")
    case .south:
        print("Moving south")
    case .east:
        print("Moving east")
    case [1]:
        print("Moving west")
    }
}
Drag options to blanks, or click blank then click option'
A.west
B.up
C.left
D.down
Attempts:
3 left
💡 Hint
Common Mistakes
Using cases not defined in the enum.
Missing one or more enum cases in the switch.
3fill in blank
hard

Fix the error in the switch statement by making it exhaustive for the enum Status.

Swift
enum Status {
    case success, failure
}

func handle(status: Status) {
    switch status {
    case .success:
        print("Success")
    [1]:
        print("Failure")
    }
}
Drag options to blanks, or click blank then click option'
Acase .error
Bcase failure
Cdefault
Dcase .failure
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'default' instead of explicit cases when enum is small.
Writing case without the dot prefix.
4fill in blank
hard

Fill both blanks to make the switch exhaustive for the enum Light.

Swift
enum Light {
    case red, yellow, green
}

func action(light: Light) {
    switch light {
    case [1]:
        print("Stop")
    case [2]:
        print("Go")
    default:
        print("Caution")
    }
}
Drag options to blanks, or click blank then click option'
A.red
B.blue
C.green
D.yellow
Attempts:
3 left
💡 Hint
Common Mistakes
Using cases not in the enum like .blue.
Not covering all enum cases explicitly or with default.
5fill in blank
hard

Fill all three blanks to make the switch exhaustive for the enum Weekday.

Swift
enum Weekday {
    case monday, tuesday, wednesday
}

func plan(day: Weekday) {
    switch day {
    case [1]:
        print("Start of week")
    case [2]:
        print("Midweek")
    case [3]:
        print("Almost weekend")
    }
}
Drag options to blanks, or click blank then click option'
A.monday
B.tuesday
C.wednesday
D.thursday
Attempts:
3 left
💡 Hint
Common Mistakes
Including cases not in the enum like .thursday.
Missing one or more enum cases in the switch.

Practice

(1/5)
1. What does it mean when we say a Swift switch statement must be exhaustive?
easy
A. It must handle all possible values of the variable being switched on.
B. It can skip some cases if they are unlikely to happen.
C. It only needs to handle the first case it matches.
D. It must have at least two cases.

Solution

  1. Step 1: Understand the meaning of exhaustive switch

    In Swift, a switch must cover every possible value of the variable it checks to avoid missing cases.
  2. Step 2: Compare options to definition

    Only It must handle all possible values of the variable being switched on. states that all possible values must be handled, which matches the requirement.
  3. Final Answer:

    It must handle all possible values of the variable being switched on. -> Option A
  4. Quick Check:

    Exhaustive means all cases covered = A [OK]
Hint: Remember: switch covers all cases or uses default [OK]
Common Mistakes:
  • Thinking switch can skip cases
  • Believing only first match matters
  • Assuming minimum number of cases
2. Which of the following Swift switch statements is syntactically correct and exhaustive for an enum Direction with cases north, south, east, and west?
easy
A. switch dir { case .north: print("North") case .south: print("South") }
B. switch dir { case .north: print("North") case .south: print("South") case .east: print("East") case .west: print("West") }
C. switch dir { case .north: print("North") case .south: print("South") case .east: print("East") }
D. switch (dir) { case .north: print("North") case .south: print("South") default: print("Other") }

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]
Hint: 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
3. What will be the output of this Swift code?
enum Mood { case happy, sad, neutral }
let todayMood = Mood.sad
switch todayMood {
 case .happy: print("Feeling great!")
 case .sad: print("Feeling down.")
 case .neutral: print("Just okay.")
}
medium
A. Feeling great!
B. Just okay.
C. Feeling down.
D. Compile error: switch not exhaustive

Solution

  1. Step 1: Identify the value of todayMood

    todayMood is set to Mood.sad.
  2. Step 2: Match the switch case

    The switch has a case for .sad that prints "Feeling down." so this case runs.
  3. Final Answer:

    Feeling down. -> Option C
  4. Quick Check:

    Value sad matches case sad = B [OK]
Hint: Match variable value to switch case to find output [OK]
Common Mistakes:
  • Confusing cases and printing wrong output
  • Thinking switch is not exhaustive here
  • Ignoring the actual value assigned
4. Identify the error in this Swift switch statement for enum Color with cases red, green, and blue:
switch color {
 case .red: print("Red")
 case .green: print("Green")
}
medium
A. Cannot use enum cases without raw values.
B. Syntax error: missing parentheses after switch.
C. No error, this switch is correct.
D. Missing case for .blue or default case, so switch is not exhaustive.

Solution

  1. Step 1: Check all enum cases handled

    The enum has three cases: red, green, blue. The switch only handles red and green.
  2. Step 2: Identify missing coverage

    Since .blue is missing and no default case is provided, the switch is not exhaustive, causing a compile error.
  3. Final Answer:

    Missing case for .blue or default case, so switch is not exhaustive. -> Option D
  4. Quick Check:

    All cases must be covered or default = C [OK]
Hint: Always cover all enum cases or add default [OK]
Common Mistakes:
  • Ignoring missing enum cases
  • Thinking parentheses needed after switch
  • Confusing raw values with case coverage
5. Given this enum and switch, how can you make the switch exhaustive without listing all cases explicitly?
enum Status { case success, failure, unknown }
let currentStatus = Status.unknown
switch currentStatus {
 case .success: print("Success")
 case .failure: print("Failure")
}
hard
A. Add a default case to handle all other values.
B. Remove the switch and use if-else instead.
C. Add a case for .unknown explicitly.
D. Change enum to have only two cases.

Solution

  1. Step 1: Understand the problem with current switch

    The switch handles .success and .failure but misses .unknown, so it's not exhaustive.
  2. Step 2: Find a way to cover missing cases without listing all explicitly

    Adding a default case covers any cases not explicitly handled, making the switch exhaustive.
  3. Final Answer:

    Add a default case to handle all other values. -> Option A
  4. Quick Check:

    Use default to cover missing cases = A [OK]
Hint: Use default to cover all unlisted cases quickly [OK]
Common Mistakes:
  • Thinking you must list every case explicitly
  • Removing switch instead of fixing it
  • Changing enum just to fix switch
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard