0
0
Swiftprogramming~20 mins

No implicit fallthrough in switch in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Switch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of switch without explicit fallthrough
What will be the output of this Swift code snippet?
Swift
let number = 2
switch number {
case 1:
    print("One")
case 2:
    print("Two")
case 3:
    print("Three")
default:
    print("Other")
}
AOther
BTwo\nThree
COne\nTwo
DTwo
Attempts:
2 left
💡 Hint
Remember that Swift switch cases do not fall through by default.
Predict Output
intermediate
2:00remaining
Effect of fallthrough keyword in Swift switch
What will be printed when this Swift code runs?
Swift
let value = 1
switch value {
case 1:
    print("Start")
    fallthrough
case 2:
    print("Middle")
case 3:
    print("End")
default:
    print("Default")
}
AStart\nMiddle
BStart
CStart\nMiddle\nEnd
DMiddle
Attempts:
2 left
💡 Hint
Check what the 'fallthrough' keyword does in Swift switch cases.
🔧 Debug
advanced
2:00remaining
Why does this Swift switch code not compile?
Identify the reason this Swift code causes a compile error.
Swift
let x = 5
switch x {
case 1:
    print("One")
case 2:
    print("Two")
case 3:
    print("Three")
}
AVariable x must be mutable (var) to use in switch
BSwitch cases must have fallthrough keyword
CMissing default case causes compile error
DPrint statements must be inside braces
Attempts:
2 left
💡 Hint
Swift switch statements must be exhaustive for non-enum types.
🧠 Conceptual
advanced
2:00remaining
Understanding no implicit fallthrough in Swift switch
Which statement best describes Swift's switch behavior regarding fallthrough?
ASwift switch cases never fall through unless 'fallthrough' keyword is explicitly used
BSwift switch requires 'continue' keyword to fall through cases
CSwift switch cases automatically fall through to the next case unless 'break' is used
DSwift switch cases fall through only if cases share the same code block
Attempts:
2 left
💡 Hint
Think about how Swift differs from C-like languages in switch behavior.
🚀 Application
expert
3:00remaining
Predict the output with multiple fallthroughs in Swift switch
What is the output of this Swift code?
Swift
let n = 1
switch n {
case 1:
    print("A")
    fallthrough
case 2:
    print("B")
    fallthrough
case 3:
    print("C")
default:
    print("D")
}
AA\nB
BA\nB\nC
CA
DA\nB\nC\nD
Attempts:
2 left
💡 Hint
Each fallthrough moves execution to the next case regardless of its condition.