Bird
0
0

Consider the following Swift code:

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

Consider the following Swift code:

typealias Coordinates = (x: Double, y: Double)

let point: Coordinates = (x: 3.5, y: 7.2)
print(point.x)

What will be the output when this code runs?

A(3.5, 7.2)
B3.5
Cx
D7.2
Step-by-Step Solution
Solution:
  1. Step 1: Understand the type alias and tuple usage

    The alias Coordinates represents a tuple with named elements x and y of type Double.
  2. Step 2: Analyze the print statement

    Printing point.x accesses the x value of the tuple, which is 3.5.
  3. Final Answer:

    3.5 -> Option B
  4. Quick Check:

    Access tuple element by name = 3.5 [OK]
Quick Trick: Access tuple elements by their names after aliasing [OK]
Common Mistakes:
  • Printing the whole tuple instead of one element
  • Confusing tuple element names
  • Expecting the alias to change tuple structure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes