0
0
Swiftprogramming~10 mins

Switch must be exhaustive in Swift - Interactive Code Practice

Choose your learning style9 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.