Bird
0
0

What is the type of the array created by let numbers = [1, 2.5, 3] in Swift?

medium📝 Predict Output Q13 of 15
Swift - Collections
What is the type of the array created by let numbers = [1, 2.5, 3] in Swift?
A[Int]
B[Double]
C[Float]
DError: mixed types not allowed
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the array elements' types

    The array contains 1 (Int), 2.5 (Double), and 3 (Int). Swift will promote Int values to Double to keep the array type consistent.
  2. Step 2: Determine the inferred array type

    Because of the Double value 2.5, Swift infers the array type as [Double], converting Ints to Double.
  3. Final Answer:

    [Double] -> Option B
  4. Quick Check:

    Mixed Int and Double = [Double] [OK]
Quick Trick: Mixed Int and Double become [Double] array [OK]
Common Mistakes:
  • Assuming array is [Int]
  • Expecting a syntax error for mixed types
  • Thinking array becomes [Float]

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes