Bird
0
0

Consider this Swift code:

medium📝 Predict Output Q5 of 15
Swift - Control Flow
Consider this Swift code:
let num = 3
switch num {
 case 1:
  print("One")
 case 2:
  print("Two")
 case 3:
  print("Three")
  fallthrough
 case 4:
  print("Four")
 default:
  print("Default")
}
What is the output?
AThree\nFour
BThree
CFour
DThree\nFour\nDefault
Step-by-Step Solution
Solution:
  1. Step 1: Identify matched case and fallthrough

    Value 3 matches case 3, prints "Three", then fallthrough moves to case 4.
  2. Step 2: Output from case 4 and default

    Case 4 prints "Four"; default is skipped because fallthrough only moves one case.
  3. Final Answer:

    Three Four -> Option A
  4. Quick Check:

    fallthrough prints next case only [OK]
Quick Trick: fallthrough only moves to immediate next case [OK]
Common Mistakes:
  • Expecting default to print after fallthrough
  • Ignoring fallthrough effect
  • Thinking fallthrough repeats current case

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes