0
0
Swiftprogramming~10 mins

Why Swift has no implicit fallthrough - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print a message for the number 2 using a switch statement.

Swift
let number = 2
switch number {
case 1:
    print("One")
case [1]:
    print("Two")
default:
    print("Other")
}
Drag options to blanks, or click blank then click option'
A4
B3
C2
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a case value that does not match the number variable.
2fill in blank
medium

Complete the code to allow fallthrough in Swift switch cases.

Swift
let number = 1
switch number {
case 1:
    print("One")
    [1]
case 2:
    print("Two")
default:
    print("Other")
}
Drag options to blanks, or click blank then click option'
Areturn
Bfallthrough
Ccontinue
Dbreak
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break', 'continue', or 'return' which do not allow fallthrough and may cause compile errors.
3fill in blank
hard

Fix the error in the switch statement by adding the correct keyword to allow fallthrough.

Swift
let number = 1
switch number {
case 1:
    print("One")
    [1]
case 2:
    print("Two")
default:
    print("Other")
}
Drag options to blanks, or click blank then click option'
Acontinue
Breturn
Cbreak
Dfallthrough
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' which stops execution instead of allowing fallthrough.
4fill in blank
hard

Fill both blanks to create a switch that prints 'Low' for 1 or 2, and 'High' for 3 or 4.

Swift
let number = 3
switch number {
case [1], [2]:
    print("Low")
default:
    print("High")
}
Drag options to blanks, or click blank then click option'
A1
B2
C3
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers that should print 'High' in the 'Low' case.
5fill in blank
hard

Fill all three blanks to create a switch that prints 'Weekend' for Saturday and Sunday, and 'Weekday' otherwise.

Swift
let day = "Sunday"
switch day {
case [1], [2]:
    print("Weekend")
default:
    print("[3]")
}
Drag options to blanks, or click blank then click option'
A"Saturday"
B"Sunday"
CWeekday
D"Monday"
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes incorrectly or mixing string and non-string values.