Bird
Raised Fist0
Swiftprogramming~20 mins

Switch with compound cases in Swift - Practice Problems & Coding Challenges

Choose your learning style10 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
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")
}
AOdd number
BCompilation error
CUnknown
DEven number
Attempts:
2 left
💡 Hint
Check which case matches the value 3 in the switch statement.
❓ Predict Output
intermediate
2: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")
}
AInvalid score
BPass
CFail
DExcellent
Attempts:
2 left
💡 Hint
Look carefully at the ranges and values in each case.
🔧 Debug
advanced
2: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")
}
Acase 1...5; 6...10: print("Within range")
Bcase 1...5 6...10: print("Within range")
Ccase 1...5, 6...10: print("Within range")
Dcase 1...5 && 6...10: print("Within range")
Attempts:
2 left
💡 Hint
Compound cases are separated by commas, not spaces or other symbols.
❓ Predict Output
advanced
2: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")
}
AClose to (2,3)
BOrigin or near origin
CSomewhere else
DCompilation error
Attempts:
2 left
💡 Hint
Check which tuple matches the value (2, 3).
❓ Predict Output
expert
2: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")
}
ANot divisible by 3 or 5
BDivisible by 3 or 5
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Check if 15 is divisible by 3 or 5.

Practice

(1/5)
1.

What does a compound case in a Swift switch statement allow you to do?

easy
A. Skip the default case safely
B. Use multiple switch statements at once
C. Combine if and switch in one line
D. Match multiple values in a single case using commas

Solution

  1. Step 1: Understand compound cases

    Compound cases let you list several values separated by commas in one case.
  2. Step 2: Recognize the benefit

    This makes the code cleaner by avoiding repeated code for similar cases.
  3. Final Answer:

    Match multiple values in a single case using commas -> Option D
  4. Quick Check:

    Compound case = multiple values in one case [OK]
Hint: Look for commas inside a case to spot compound cases [OK]
Common Mistakes:
  • Thinking compound cases combine multiple switches
  • Believing compound cases skip default
  • Confusing compound cases with if-else
2.

Which of the following is the correct syntax for a compound case in Swift?

switch value {
    case 1, 2, 3:
        print("One to three")
    default:
        print("Other")
}
easy
A. case 1, 2, 3:
B. case 1; 2; 3:
C. case (1, 2, 3):
D. case [1, 2, 3]:

Solution

  1. Step 1: Recall Swift compound case syntax

    Swift uses commas to separate multiple values in one case.
  2. Step 2: Check each option

    case 1, 2, 3: uses commas correctly; others use semicolons, parentheses, or brackets incorrectly.
  3. Final Answer:

    case 1, 2, 3: -> Option A
  4. Quick Check:

    Commas separate values in compound case [OK]
Hint: Use commas, not semicolons or brackets, for compound cases [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Putting values inside parentheses or brackets
  • Missing the colon after case
3.

What will be printed by this Swift code?

let letter = "b"
switch letter {
case "a", "e", "i", "o", "u":
    print("Vowel")
case "b", "c", "d":
    print("Consonant")
default:
    print("Other")
}
medium
A. Vowel
B. No output
C. Consonant
D. Other

Solution

  1. Step 1: Identify the value of letter

    The variable letter is "b".
  2. Step 2: Match letter in switch cases

    "b" matches the second case with "b", "c", "d" which prints "Consonant".
  3. Final Answer:

    Consonant -> Option C
  4. Quick Check:

    "b" in second case = Consonant [OK]
Hint: Check which case list contains the value [OK]
Common Mistakes:
  • Confusing vowels and consonants
  • Forgetting to include default case
  • Assuming no output if no exact match
4.

Find the error in this Swift switch statement:

let number = 3
switch number {
case 1, 2
    print("One or Two")
case 3:
    print("Three")
default:
    print("Other")
}
medium
A. Missing colon after case 1, 2
B. Missing default case
C. Cannot use compound cases with integers
D. Missing parentheses around 1, 2

Solution

  1. Step 1: Check syntax of each case

    Each case must end with a colon (:). The first case is missing it.
  2. Step 2: Verify other parts

    Default case is present; compound cases with integers are allowed; parentheses are not needed.
  3. Final Answer:

    Missing colon after case 1, 2 -> Option A
  4. Quick Check:

    Case lines must end with colon [OK]
Hint: Look for missing colons after case labels [OK]
Common Mistakes:
  • Forgetting colon after case
  • Thinking parentheses are required
  • Believing compound cases disallow integers
5.

Given this Swift code, what will be the output?

let grade = "B"
switch grade {
case "A", "B", "C":
    print("Pass")
case "D", "F":
    print("Fail")
case "I", "W":
    print("Incomplete or Withdrawn")
default:
    print("Unknown grade")
}
hard
A. Incomplete or Withdrawn
B. Pass
C. Fail
D. Unknown grade

Solution

  1. Step 1: Identify the value of grade

    The variable grade is "B".
  2. Step 2: Match grade in compound cases

    "B" matches the first case "A", "B", "C" which prints "Pass".
  3. Final Answer:

    Pass -> Option B
  4. Quick Check:

    "B" in first case = Pass [OK]
Hint: Match value to any in compound case list [OK]
Common Mistakes:
  • Assuming "B" is a fail grade
  • Ignoring default case
  • Confusing case groups
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