Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Control Flow

What will be printed by this Swift code?

let number = 15
switch number {
case let x where x % 3 == 0:
    print("Divisible by 3")
case let x where x % 5 == 0:
    print("Divisible by 5")
default:
    print("Other")
}
ANo output
BDivisible by 3
COther
DDivisible by 5
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the value against cases

    Number is 15. Check first case: 15 % 3 == 0 is true, so this case matches.
  2. Step 2: Understand switch behavior

    Switch executes the first matching case and stops. So it prints "Divisible by 3" and does not check further cases.
  3. Final Answer:

    Divisible by 3 -> Option B
  4. Quick Check:

    First matching where case runs = Divisible by 3 [OK]
Quick Trick: Switch stops at first matching where case [OK]
Common Mistakes:
  • Thinking both cases run
  • Ignoring order of cases
  • Assuming default runs if multiple matches

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes