Bird
0
0

Which of the following correctly defines a Swift function that returns a tuple with named elements width and height as Doubles?

easy📝 Conceptual Q2 of 15
Swift - Functions
Which of the following correctly defines a Swift function that returns a tuple with named elements width and height as Doubles?
Afunc getSize() -> (width: Double, height: Double) { return [10.0, 20.0] }
Bfunc getSize() -> (Double width, Double height) { return (10.0, 20.0) }
Cfunc getSize() -> (width: Double, height: Double) { return width: 10.0, height: 20.0 }
Dfunc getSize() -> (width: Double, height: Double) { return (10.0, 20.0) }
Step-by-Step Solution
Solution:
  1. Step 1: Review tuple syntax

    Named tuple elements are declared as (name: Type, name: Type).
  2. Step 2: Check return statement

    Return a tuple with values matching the declared names and types.
  3. Final Answer:

    func getSize() -> (width: Double, height: Double) { return (10.0, 20.0) } -> Option D
  4. Quick Check:

    Named tuple syntax uses (name: Type) [OK]
Quick Trick: Use (name: Type) for named tuple elements [OK]
Common Mistakes:
  • Using type before name in tuple declaration
  • Returning dictionary or array instead of tuple
  • Incorrect tuple return syntax without parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes