Bird
0
0

Which of the following creates an empty array of Integers in Swift?

easy📝 Conceptual Q2 of 15
Swift - Collections
Which of the following creates an empty array of Integers in Swift?
Alet numbers = []
Blet numbers = Int[]
Clet numbers: [Int] = []
Dlet numbers = [0]
Step-by-Step Solution
Solution:
  1. Step 1: Understand empty array creation

    An empty array must have its type specified if no elements are given.
  2. Step 2: Check each option

    let numbers: [Int] = [] explicitly declares an empty array of Int type. let numbers = Int[] is invalid because Swift array type syntax is [Int], not Int[]. let numbers = [] is invalid because type cannot be inferred from empty brackets. let numbers = [0] creates an array with one element, 0.
  3. Final Answer:

    let numbers: [Int] = [] -> Option C
  4. Quick Check:

    Empty array with type annotation = let numbers: [Int] = [] [OK]
Quick Trick: Empty arrays need explicit type or initializer [OK]
Common Mistakes:
  • Using [] without type annotation
  • Confusing empty array with array containing zero
  • Using incorrect syntax like let numbers = []

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes