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 = 5
switch number {
 case 1..<5: print("Less than 5")
 case 5: print("Exactly 5")
 case 6...10: print("Between 6 and 10")
 default: print("Out of range")
}
ALess than 5
BBetween 6 and 10
CExactly 5
DOut of range
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of number

    The variable number is 5.
  2. Step 2: Match number with switch cases

    Case 1..<5 means 1 to 4, so 5 does not match. Case 5 matches exactly, so it prints "Exactly 5".
  3. Final Answer:

    Exactly 5 -> Option C
  4. Quick Check:

    Exact match case runs = "Exactly 5" [OK]
Quick Trick: Ranges exclude upper bound with ..< and include with ... [OK]
Common Mistakes:
  • Confusing 1..<5 as including 5
  • Choosing default instead of exact match
  • Ignoring range syntax differences

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes