Bird
0
0

What will be the output of the following Swift code?

medium📝 Predict Output Q13 of 15
Swift - Control Flow
What will be the output of the following Swift code?
let number = 2
switch number {
 case 1:
     print("One")
 case 2:
     print("Two")
     fallthrough
 case 3:
     print("Three")
 default:
     print("Other")
}
ATwo
BTwo\nThree
CThree
DTwo\nOther
Step-by-Step Solution
Solution:
  1. Step 1: Identify matched case and fallthrough

    The value 2 matches case 2, so "Two" is printed, then fallthrough causes execution to continue to case 3.
  2. Step 2: Execute fallthrough case

    Case 3 prints "Three". No further fallthrough, so switch ends.
  3. Final Answer:

    Two Three -> Option B
  4. Quick Check:

    fallthrough prints both cases [OK]
Quick Trick: fallthrough prints next case output too [OK]
Common Mistakes:
  • Assuming only case 2 prints
  • Thinking fallthrough skips printing
  • Confusing default case execution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes