Bird
Raised Fist0
Swiftprogramming~20 mins

Switch statement power in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Switch Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Switch with tuple pattern matching
What is the output of this Swift code using a switch statement with tuple pattern matching?
Swift
let point = (2, -2)
switch point {
case (let x, let y) where x == y:
    print("On the line x == y")
case (let x, let y) where x == -y:
    print("On the line x == -y")
default:
    print("Just a point")
}
AOn the line x == y
BJust a point
CCompilation error
DOn the line x == -y
Attempts:
2 left
💡 Hint
Check the conditions in the where clauses carefully.
❓ Predict Output
intermediate
2:00remaining
Switch with value binding and fallthrough
What will be printed when this Swift code runs?
Swift
let number = 3
switch number {
case 1:
    print("One")
case 2, 3:
    print("Two or Three")
    fallthrough
case 4:
    print("Four")
default:
    print("Other")
}
A
Two or Three
Four
BTwo or Three
CFour
DOther
Attempts:
2 left
💡 Hint
Remember what fallthrough does in Swift switch statements.
🔧 Debug
advanced
2:00remaining
Why does this switch cause a compile error?
This Swift code causes a compile error. What is the reason?
Swift
let value = 5
switch value {
case 1..<5:
    print("Less than 5")
case 5:
    print("Equal to 5")
case 6...10:
    print("Between 6 and 10")
default: 
    print("Other")
}
AThe case _: syntax is invalid; it should be default.
BThe range 1..<5 overlaps with 5 causing ambiguity.
CThe switch cases are not exhaustive.
DThe case _: is unreachable because previous cases cover all values.
Attempts:
2 left
💡 Hint
Check the syntax for the default case in Swift switch statements.
❓ Predict Output
advanced
2:00remaining
Switch with multiple patterns and where clause
What is the output of this Swift code?
Swift
let char: Character = "e"
switch char {
case "a", "e", "i", "o", "u" where char.isLowercase:
    print("Lowercase vowel")
case "A", "E", "I", "O", "U":
    print("Uppercase vowel")
default:
    print("Consonant")
}
ALowercase vowel
BUppercase vowel
CCompilation error
DConsonant
Attempts:
2 left
💡 Hint
Check the placement of the where clause with multiple patterns.
❓ Predict Output
expert
3:00remaining
Switch with enum and associated values
Given this Swift enum and switch, what will be printed?
Swift
enum Result {
    case success(Int)
    case failure(String)
}

let result = Result.success(42)
switch result {
case .success(let code) where code > 40:
    print("Success with code > 40")
case .success:
    print("Success with code <= 40")
case .failure(let message):
    print("Failure: \(message)")
}
ASuccess with code <= 40
BSuccess with code > 40
CFailure: 42
DCompilation error
Attempts:
2 left
💡 Hint
Look at the associated value and the where clause condition.

Practice

(1/5)
1. What is the main advantage of using a switch statement in Swift compared to multiple if-else statements?
easy
A. It runs faster than any other conditional statement.
B. It makes the code easier to read and maintain by clearly checking many cases.
C. It allows only numeric comparisons.
D. It automatically handles errors without extra code.

Solution

  1. Step 1: Understand the purpose of switch

    The switch statement is designed to check a value against multiple cases clearly and cleanly.
  2. Step 2: Compare with if-else statements

    While if-else can do similar checks, switch improves readability and maintenance by organizing cases neatly.
  3. Final Answer:

    It makes the code easier to read and maintain by clearly checking many cases. -> Option B
  4. Quick Check:

    Switch improves readability = A [OK]
Hint: Switch organizes multiple checks clearly for easy reading [OK]
Common Mistakes:
  • Thinking switch is always faster than if-else
  • Believing switch only works with numbers
  • Assuming switch handles errors automatically
2. Which of the following is the correct syntax for a basic switch statement in Swift?
easy
A. switch (value) { case 1 print("One") else print("Other") }
B. switch value { if case 1: print("One") else: print("Other") }
C. switch value: case 1 -> print("One") else -> print("Other")
D. switch value { case 1: print("One") default: print("Other") }

Solution

  1. Step 1: Review correct switch syntax

    In Swift, a switch statement uses curly braces with cases followed by colons and code blocks.
  2. Step 2: Identify the correct option

    switch value { case 1: print("One") default: print("Other") } matches the correct syntax: switch value { case 1: ... default: ... }.
  3. Final Answer:

    switch value { case 1: print("One") default: print("Other") } -> Option D
  4. Quick Check:

    Correct switch syntax = B [OK]
Hint: Remember: cases end with colon and use curly braces [OK]
Common Mistakes:
  • Omitting colons after case labels
  • Using else instead of default
  • Wrong punctuation like arrows or missing braces
3. What will be the output of the following Swift code?
let number = 3
switch number {
 case 1, 3, 5:
   print("Odd")
 case 2, 4, 6:
   print("Even")
 default:
   print("Unknown")
}
medium
A. Odd
B. Even
C. Unknown
D. No output

Solution

  1. Step 1: Check the value of number

    The variable number is set to 3.
  2. Step 2: Match number in switch cases

    Case 1, 3, 5 matches number 3, so it prints "Odd".
  3. Final Answer:

    Odd -> Option A
  4. Quick Check:

    3 in (1,3,5) = Odd [OK]
Hint: Look for matching case values separated by commas [OK]
Common Mistakes:
  • Choosing Even because 3 is odd but not in even case
  • Ignoring multiple values in one case
  • Assuming default runs when a case matches
4. Identify the error in this Swift switch statement:
let grade = "B"
switch grade {
 case "A":
   print("Excellent")
 case "B"
   print("Good")
 default:
   print("Needs Improvement")
}
medium
A. Switch cannot use strings
B. Missing default case
C. Missing colon after case "B"
D. Print statements must be outside switch

Solution

  1. Step 1: Check syntax of each case

    Each case label must end with a colon (:). The case "B" is missing this colon.
  2. Step 2: Confirm other parts are correct

    Default case exists, strings are allowed, and print inside switch is valid.
  3. Final Answer:

    Missing colon after case "B" -> Option C
  4. Quick Check:

    Case labels need colon = C [OK]
Hint: Every case line ends with a colon : [OK]
Common Mistakes:
  • Forgetting colon after case label
  • Thinking default is optional (it is recommended)
  • Believing switch can't handle strings
5. You want to categorize an integer score into grades using a switch statement:
- 90 to 100: "A"
- 80 to 89: "B"
- 70 to 79: "C"
- Below 70: "F"
Which switch statement correctly implements this?
hard
A. switch score { case 90...100: print("A") case 80...89: print("B") case 70...79: print("C") default: print("F") }
B. switch score { case 90..<100: print("A") case 80..<90: print("B") case 70..<80: print("C") default: print("F") }
C. switch score { case 90...100: print("A") case 80...90: print("B") case 70...80: print("C") default: print("F") }
D. switch score { case 90...100: print("A") case 80...89: print("B") case 70...79: print("C") case 0...69: print("F") }

Solution

  1. Step 1: Understand range operators

    Closed range operator (...) includes both ends, so 90...100 includes 90 and 100.
  2. Step 2: Check all ranges cover scores without overlap or gaps

    switch score { case 90...100: print("A") case 80...89: print("B") case 70...79: print("C") default: print("F") } uses closed ranges for exact matches (90-100, 80-89, 70-79) and default catches below 70 perfectly without gaps or overlaps.
  3. Step 3: Identify issues in other options

    B uses half-open ranges (..<) excluding upper bounds like 100 (90..<100 misses 100); C has overlapping ranges (80...90 overlaps 90 with A, 70...80 overlaps 80); D lacks default and doesn't cover all integers (e.g., negatives, >100), causing compile error.
  4. Final Answer:

    switch score { case 90...100: print("A") case 80...89: print("B") case 70...79: print("C") default: print("F") } -> Option A
  5. Quick Check:

    Complete, non-overlapping ranges = A [OK]
Hint: Use closed ranges and cover all values explicitly [OK]
Common Mistakes:
  • Overlapping or missing ranges
  • Using half-open ranges that exclude max values
  • Not covering all possible values explicitly
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