Bird
0
0

Find the mistake in this Swift code:

medium📝 Debug Q7 of 15
Swift - Collections
Find the mistake in this Swift code:
var nums = [1, 2, 3]
nums.insert(4, 3)
print(nums)
AIndex 3 is out of range for insert.
Bappend should be used instead of insert.
Cprint statement syntax is incorrect.
Dinsert method missing 'at:' label before index.
Step-by-Step Solution
Solution:
  1. Step 1: Review insert method syntax

    insert(_:at:) requires 'at:' label before index.
  2. Step 2: Identify error in code

    Code uses nums.insert(4, 3) missing 'at:' label, causing syntax error.
  3. Final Answer:

    insert method missing 'at:' label before index. -> Option D
  4. Quick Check:

    insert syntax requires 'at:' label [OK]
Quick Trick: insert needs 'at:' label before index [OK]
Common Mistakes:
  • Omitting 'at:' label in insert
  • Using append instead of insert incorrectly
  • Assuming index 3 is invalid for insert

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes