Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q13 of 15
Swift - Control Flow

What will be printed by this Swift code?

let age = 20
switch age {
case let x where x < 13:
    print("Child")
case let x where x >= 13 && x < 20:
    print("Teenager")
case let x where x >= 20:
    print("Adult")
default:
    print("Unknown")
}
AAdult
BTeenager
CChild
DUnknown
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of age

    The variable age is 20.
  2. Step 2: Evaluate each case condition

    First case: x < 13? No (20 < 13 is false). Second case: 13 <= x < 20? No (20 < 20 is false). Third case: x >= 20? Yes (20 >= 20 is true).
  3. Final Answer:

    Adult -> Option A
  4. Quick Check:

    age 20 matches 'Adult' case [OK]
Quick Trick: Check conditions in order; first true case runs [OK]
Common Mistakes:
  • Choosing 'Teenager' because 20 is close to 19
  • Ignoring >= and < operators
  • Forgetting switch stops at first match

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes