Challenge - 5 Problems
Swift Switch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of switch with multiple values in one case
What is the output of this Swift code?
Swift
let number = 3 switch number { case 1, 3, 5: print("Odd number") case 2, 4, 6: print("Even number") default: print("Unknown") }
Attempts:
2 left
💡 Hint
Check which case matches the value 3 in the switch statement.
✗ Incorrect
The value 3 matches the first case which includes 1, 3, and 5, so it prints "Odd number".
❓ Predict Output
intermediate2:00remaining
Switch with range and multiple values
What will this Swift code print?
Swift
let score = 85 switch score { case 0, 1..<50: print("Fail") case 50..<80, 80: print("Pass") case 81...100: print("Excellent") default: print("Invalid score") }
Attempts:
2 left
💡 Hint
Look carefully at the ranges and values in each case.
✗ Incorrect
The score 85 falls in the range 81...100, so it prints "Excellent".
🔧 Debug
advanced2:00remaining
Identify the error in compound case syntax
Which option shows the correct way to write a compound case with ranges in Swift?
Swift
let value = 7 switch value { case 1...5, 6...10: print("Within range") default: print("Out of range") }
Attempts:
2 left
💡 Hint
Compound cases are separated by commas, not spaces or other symbols.
✗ Incorrect
Only option C uses the correct comma to separate multiple patterns in one case.
❓ Predict Output
advanced2:00remaining
Switch with tuple and compound cases
What is the output of this Swift code?
Swift
let point = (2, 3) switch point { case (0, 0), (1, 1): print("Origin or near origin") case (2, 3), (3, 2): print("Close to (2,3)") default: print("Somewhere else") }
Attempts:
2 left
💡 Hint
Check which tuple matches the value (2, 3).
✗ Incorrect
The tuple (2, 3) matches the second case, so it prints "Close to (2,3)".
❓ Predict Output
expert2:00remaining
Switch with where clause and compound cases
What will this Swift code print?
Swift
let number = 15 switch number { case let x where x % 3 == 0 || x % 5 == 0: print("Divisible by 3 or 5") default: print("Not divisible by 3 or 5") }
Attempts:
2 left
💡 Hint
Check if 15 is divisible by 3 or 5.
✗ Incorrect
15 is divisible by both 3 and 5, so the first case matches and prints "Divisible by 3 or 5".