Bird
0
0

What is the output of the following Swift code?

medium📝 Predict Output Q13 of 15
Swift - Control Flow
What is 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")
}
AThree
BTwo Three
CTwo
DTwo Three Other
Step-by-Step Solution
Solution:
  1. Step 1: Identify matched case

    The value 2 matches case 2, so it prints "Two".
  2. Step 2: Check for fallthrough

    Since fallthrough is used, execution continues to case 3, printing "Three".
  3. Final Answer:

    Two Three -> Option B
  4. Quick Check:

    fallthrough causes next case to run = Two and Three [OK]
Quick Trick: Look for fallthrough keyword to know if next case runs [OK]
Common Mistakes:
  • Ignoring fallthrough and printing only "Two"
  • Printing "Three" only without "Two"
  • Expecting default case to run
  • Assuming all cases print

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes