Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The enum Color has three cases: red, green, and blue. To make the switch exhaustive, you must include all cases. Here, .blue is the missing case.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using cases not defined in the enum.
Missing one or more enum cases in the switch.
✗ Incorrect
The enum Direction has four cases: north, south, east, and west. The missing case in the switch is .west to make it exhaustive.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'default' instead of explicit cases when enum is small.
Writing case without the dot prefix.
✗ Incorrect
The enum Status has two cases: success and failure. The switch must include 'case .failure:' to be exhaustive. Using 'default' is allowed but here the correct case is .failure.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using cases not in the enum like .blue.
Not covering all enum cases explicitly or with default.
✗ Incorrect
To make the switch exhaustive, cases for .red and .green are explicitly handled. The default covers the remaining case .yellow as caution.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Including cases not in the enum like .thursday.
Missing one or more enum cases in the switch.
✗ Incorrect
The enum Weekday has three cases: monday, tuesday, and wednesday. The switch must handle all three to be exhaustive. .thursday is not part of the enum.