Bird
0
0

Given this Swift code, what will be printed?

hard📝 Application Q9 of 15
Swift - Control Flow

Given this Swift code, what will be printed?

let point = (x: 3, y: 4)
switch point {
case let (x, y) where x == y:
    print("On the diagonal")
case let (x, y) where x * x + y * y == 25:
    print("On the circle with radius 5")
default:
    print("Somewhere else")
}
AOn the diagonal
BNo output
COn the circle with radius 5
DSomewhere else
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate first case condition

    Point is (3,4). Check if x == y: 3 == 4 is false.
  2. Step 2: Evaluate second case condition

    Check if x*x + y*y == 25: 3*3 + 4*4 = 9 + 16 = 25, true.
  3. Step 3: Switch executes first matching case

    Second case matches, so it prints "On the circle with radius 5".
  4. Final Answer:

    On the circle with radius 5 -> Option C
  5. Quick Check:

    First true where case runs = On the circle with radius 5 [OK]
Quick Trick: Switch matches first true where case in order [OK]
Common Mistakes:
  • Assuming first case matches
  • Ignoring tuple pattern matching
  • Miscomputing sum of squares

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes