Bird
0
0

You have an array var tasks = ["Clean", "Cook", "Shop"]. Write Swift code to insert "Laundry" after "Cook" and then remove "Clean". What will the final array look like?

hard📝 Application Q8 of 15
Swift - Collections
You have an array var tasks = ["Clean", "Cook", "Shop"]. Write Swift code to insert "Laundry" after "Cook" and then remove "Clean". What will the final array look like?
A["Clean", "Laundry", "Cook", "Shop"]
B["Clean", "Cook", "Laundry", "Shop"]
C["Laundry", "Clean", "Cook", "Shop"]
D["Cook", "Laundry", "Shop"]
Step-by-Step Solution
Solution:
  1. Step 1: Insert "Laundry" after "Cook"

    "Cook" is at index 1, so insert "Laundry" at index 2: tasks.insert("Laundry", at: 2).
  2. Step 2: Remove "Clean"

    "Clean" is at index 0, so remove at index 0: tasks.remove(at: 0).
  3. Final Answer:

    ["Cook", "Laundry", "Shop"] -> Option D
  4. Quick Check:

    Insert after index 1, remove index 0 = ["Cook", "Laundry", "Shop"] [OK]
Quick Trick: Insert at index after target; remove at target index [OK]
Common Mistakes:
  • Inserting at wrong index
  • Removing wrong element
  • Not updating indices after insert

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes