Bird
0
0

What will be printed by the following Swift code?

medium📝 Predict Output Q5 of 15
Swift - Variables and Constants

What will be printed by the following Swift code?

typealias Vector = (dx: Int, dy: Int)
let movement: Vector = (dx: 4, dy: 6)
print(movement.dx * movement.dy)
A(4, 6)
B24
C10
DError: Cannot multiply tuple elements
Step-by-Step Solution
Solution:
  1. Step 1: Understand the typealias

    Vector is a tuple with two Int components: dx and dy.
  2. Step 2: Evaluate the expression

    The code multiplies movement.dx (4) by movement.dy (6), resulting in 24.
  3. Final Answer:

    24 -> Option B
  4. Quick Check:

    Verify tuple element access and multiplication. [OK]
Quick Trick: Multiply tuple elements accessed by name correctly. [OK]
Common Mistakes:
  • Assuming the tuple prints as a pair instead of calculating
  • Adding instead of multiplying the elements
  • Thinking tuple elements cannot be used in arithmetic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes