0
0
Swiftprogramming~20 mins

Switch must be exhaustive in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Switch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this exhaustive switch?

Consider the following Swift code with an exhaustive switch on an enum. What will it print?

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

let dir = Direction.east

switch dir {
case .north:
    print("Going north")
case .south:
    print("Going south")
case .east:
    print("Going east")
case .west:
    print("Going west")
}
AGoing east
BGoing north
CCompilation error: switch not exhaustive
DRuntime error
Attempts:
2 left
💡 Hint

Check if all enum cases are covered in the switch.

Predict Output
intermediate
2:00remaining
What error does this non-exhaustive switch cause?

What error will this Swift code produce?

Swift
enum Color {
    case red, green, blue
}

let c = Color.green

switch c {
case .red:
    print("Red")
case .blue:
    print("Blue")
}
ACompilation error: switch must be exhaustive
BRuntime error: missing case green
CPrints nothing
DPrints "Green"
Attempts:
2 left
💡 Hint

Swift requires switches on enums to cover all cases or have a default.

🧠 Conceptual
advanced
1:30remaining
Why must switches on enums be exhaustive in Swift?

Why does Swift require switches on enums to be exhaustive?

ATo make the code run faster by skipping unhandled cases
BTo allow the compiler to optimize memory usage
CTo ensure all possible cases are handled, preventing unexpected behavior at runtime
DBecause Swift does not support default cases in switches
Attempts:
2 left
💡 Hint

Think about safety and predictability in code.

📝 Syntax
advanced
2:00remaining
Which switch is exhaustive for this enum?

Given the enum below, which switch statement is exhaustive and compiles without error?

Swift
enum Status {
    case success
    case failure
    case unknown
}
A
switch status {
case .success:
    print("Success")
case .failure:
    print("Failure")
}
B
switch status {
case .success:
    print("Success")
case .failure:
    print("Failure")
case .unknown:
    print("Unknown")
}
C
switch status {
default:
    print("Any")
}
D
switch status {
case .success, .failure:
    print("Known")
}
Attempts:
2 left
💡 Hint

Check if all cases are covered or if a default is used.

🚀 Application
expert
2:30remaining
How many cases must be handled in this switch?

Given this enum with associated values, how many cases must a switch handle to be exhaustive?

Swift
enum Result {
    case success(Int)
    case failure(String)
    case pending
}
A4 cases because success and failure have associated values
B2 cases: success and failure only, pending is optional
C1 case with a default to cover all
D3 cases: success, failure, and pending
Attempts:
2 left
💡 Hint

Remember each enum case counts separately regardless of associated values.