What is a compound case in a Swift switch statement?
A compound case lets you group multiple values together in one case using commas, so the same code runs for any of those values.
Click to reveal answer
beginner
How do you write multiple values in one case in Swift's switch?
You list the values separated by commas after the case keyword, like case 1, 2, 3:.
Click to reveal answer
beginner
Why use compound cases in a switch statement?
It makes your code shorter and clearer by handling several values with the same code instead of repeating cases.
Click to reveal answer
intermediate
Can you use ranges or conditions with compound cases in Swift?
Yes, you can combine compound cases with ranges or use where clauses for extra conditions.
Click to reveal answer
beginner
Example: What does this Swift code print?<br><pre>let number = 3
switch number {
case 1, 3, 5:
print("Odd number")
case 2, 4, 6:
print("Even number")
default:
print("Unknown")
}</pre>
It prints Odd number because 3 matches the first compound case with 1, 3, and 5.
Click to reveal answer
In Swift, how do you group multiple values in one switch case?
ASeparate values with commas after case
BUse semicolons between values
CWrite multiple case lines for each value
DUse parentheses around values
✗ Incorrect
You separate multiple values with commas in one case, like case 1, 2, 3:.
What happens if a value matches a compound case in Swift switch?
AIt runs all cases
BThe switch skips that case
CThe code inside that case runs
DThe program crashes
✗ Incorrect
When a value matches any value in a compound case, the code inside that case executes.
Can you mix ranges and compound cases in Swift switch?
AYes, you can combine them
BNo, ranges and compound cases are exclusive
COnly if you use if-else inside switch
DOnly with default case
✗ Incorrect
Swift allows combining ranges and compound cases, and you can add conditions with where clauses.
Which keyword lets you add extra conditions to a Swift switch case?
Aelse
Bif
Ccase
Dwhere
✗ Incorrect
The where keyword adds extra conditions to a case in Swift switch.
What is the benefit of using compound cases in Swift switch?
ASlower program execution
BShorter and clearer code
CMore lines of code
DAvoids using default case
✗ Incorrect
Compound cases help write shorter and clearer code by grouping multiple values in one case.
Explain how to use compound cases in a Swift switch statement and why they are useful.
Think about how you can handle several options together without repeating code.
You got /3 concepts.
Write a Swift switch statement using compound cases to print "Weekend" for Saturday and Sunday, and "Weekday" for other days.
Use compound cases to group weekend days together.
You got /3 concepts.
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
Step 1: Understand compound cases
Compound cases let you list several values separated by commas in one case.
Step 2: Recognize the benefit
This makes the code cleaner by avoiding repeated code for similar cases.
Final Answer:
Match multiple values in a single case using commas -> Option D
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
Step 1: Recall Swift compound case syntax
Swift uses commas to separate multiple values in one case.
Step 2: Check each option
case 1, 2, 3: uses commas correctly; others use semicolons, parentheses, or brackets incorrectly.
Final Answer:
case 1, 2, 3: -> Option A
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
Step 1: Identify the value of letter
The variable letter is "b".
Step 2: Match letter in switch cases
"b" matches the second case with "b", "c", "d" which prints "Consonant".
Final Answer:
Consonant -> Option C
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
Step 1: Check syntax of each case
Each case must end with a colon (:). The first case is missing it.
Step 2: Verify other parts
Default case is present; compound cases with integers are allowed; parentheses are not needed.
Final Answer:
Missing colon after case 1, 2 -> Option A
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
Step 1: Identify the value of grade
The variable grade is "B".
Step 2: Match grade in compound cases
"B" matches the first case "A", "B", "C" which prints "Pass".
Final Answer:
Pass -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.