Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q5 of 15
Swift - Control Flow
What will be printed by this Swift code?
let number = 3
switch number {
 case 1:
   print("One")
 case 2:
   print("Two")
 case 3:
   print("Three")
   fallthrough
 case 4:
   print("Four")
 default:
   print("Default")
}
AThree Four
BThree
CFour
DThree Four Default
Step-by-Step Solution
Solution:
  1. Step 1: Match the case

    Value is 3, so case 3 matches and prints "Three".
  2. Step 2: Apply fallthrough

    fallthrough causes execution to continue to case 4, printing "Four".
  3. Final Answer:

    Three Four -> Option A
  4. Quick Check:

    fallthrough continues to next case regardless of condition [OK]
Quick Trick: fallthrough prints next case even if condition false [OK]
Common Mistakes:
  • Printing only "Three"
  • Printing default case incorrectly
  • Assuming fallthrough skips next case

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes