Bird
0
0

Which of the following is the correct syntax to insert the value 5 at index 1 in a Swift array named numbers?

easy📝 Syntax Q3 of 15
Swift - Collections
Which of the following is the correct syntax to insert the value 5 at index 1 in a Swift array named numbers?
Anumbers.insert(5, at: 1)
Bnumbers.append(5, at: 1)
Cnumbers.insertAt(1, 5)
Dnumbers.add(5, index: 1)
Step-by-Step Solution
Solution:
  1. Step 1: Recall insert syntax in Swift

    The insert method uses the syntax insert(_:at:) where the first argument is the element and second is the index.
  2. Step 2: Match syntax with options

    numbers.insert(5, at: 1) matches the correct syntax: numbers.insert(5, at: 1).
  3. Final Answer:

    numbers.insert(5, at: 1) -> Option A
  4. Quick Check:

    insert(_:at:) syntax = insert(element, at: index) [OK]
Quick Trick: insert(_:at:) needs element first, then index [OK]
Common Mistakes:
  • Swapping element and index order
  • Using append() with index
  • Using non-existent methods like insertAt or add

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes