Bird
Raised Fist0
Swiftprogramming~20 mins

Array operations (append, insert, remove) in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift code?
Consider the following Swift code that modifies an array. What will be printed?
Swift
var fruits = ["Apple", "Banana", "Cherry"]
fruits.append("Date")
fruits.insert("Elderberry", at: 1)
print(fruits)
A["Apple", "Banana", "Elderberry", "Cherry", "Date"]
B["Apple", "Elderberry", "Banana", "Cherry", "Date"]
C["Apple", "Banana", "Cherry", "Date", "Elderberry"]
D["Elderberry", "Apple", "Banana", "Cherry", "Date"]
Attempts:
2 left
💡 Hint
Remember that insert(at:) adds the element at the specified index, shifting others to the right.
❓ Predict Output
intermediate
2:00remaining
What happens after removing an element?
Given this Swift code, what will be the output?
Swift
var numbers = [10, 20, 30, 40, 50]
numbers.remove(at: 2)
print(numbers)
A[10, 20, 40, 50]
B[10, 20, 30, 40]
C[10, 30, 40, 50]
D[20, 30, 40, 50]
Attempts:
2 left
💡 Hint
Remember that remove(at:) removes the element at the given index and shifts the rest left.
🔧 Debug
advanced
2:00remaining
Why does this Swift code cause a runtime error?
Examine the code below. Why does it crash when run?
Swift
var letters = ["a", "b", "c"]
letters.remove(at: 3)
print(letters)
AIt prints ["a", "b", "c"] without changes.
BIt causes a compile-time error because remove(at:) is not a valid method.
CIt causes a runtime error because index 3 is out of range for the array.
DIt removes the last element "c" and prints ["a", "b"]
Attempts:
2 left
💡 Hint
Check the valid indices for the array before removing.
❓ Predict Output
advanced
2:00remaining
What is the final array after multiple operations?
What will be the output of this Swift code?
Swift
var colors = ["Red", "Green", "Blue"]
colors.insert("Yellow", at: 0)
colors.append("Purple")
colors.remove(at: 2)
print(colors)
A["Yellow", "Red", "Blue", "Purple"]
B["Yellow", "Red", "Green", "Purple"]
C["Red", "Green", "Purple"]
D["Yellow", "Green", "Blue", "Purple"]
Attempts:
2 left
💡 Hint
Track each operation step-by-step on the array.
🧠 Conceptual
expert
2:00remaining
How many elements remain after these operations?
Given this Swift code, how many elements are in the array after all operations?
Swift
var items = ["Pen", "Pencil", "Eraser", "Sharpener"]
items.append("Marker")
items.remove(at: 1)
items.insert("Notebook", at: 2)
items.removeLast()
A6
B5
C3
D4
Attempts:
2 left
💡 Hint
Count carefully after each operation.

Practice

(1/5)
1. What does the append method do in a Swift array?
easy
A. Removes the last element from the array
B. Adds a new element to the end of the array
C. Inserts an element at the beginning of the array
D. Clears all elements from the array

Solution

  1. Step 1: Understand the append method

    The append method adds a new element to the end of the array, increasing its size by one.
  2. Step 2: Compare with other options

    Removing elements or inserting at the beginning are done by other methods, not append.
  3. Final Answer:

    Adds a new element to the end of the array -> Option B
  4. Quick Check:

    append = add at end [OK]
Hint: append always adds at the end of the array [OK]
Common Mistakes:
  • Confusing append with insert
  • Thinking append removes elements
  • Assuming append adds at the start
2. Which of the following is the correct way to insert the number 10 at index 2 in a Swift array named numbers?
easy
A. numbers.insert(10, at: 2)
B. numbers.append(10, at: 2)
C. numbers.add(10, index: 2)
D. numbers.insertAt(2, 10)

Solution

  1. Step 1: Recall the insert syntax in Swift

    The correct syntax is array.insert(element, at: index), so numbers.insert(10, at: 2) is correct.
  2. Step 2: Check other options for syntax errors

    Options A, B, and D use incorrect method names or argument orders.
  3. Final Answer:

    numbers.insert(10, at: 2) -> Option A
  4. Quick Check:

    insert(element, at: index) syntax [OK]
Hint: Use insert(element, at: index) to add anywhere [OK]
Common Mistakes:
  • Using append with index
  • Wrong method names like add or insertAt
  • Swapping argument order
3. What will be the output of the following Swift code?
var fruits = ["Apple", "Banana", "Cherry"]
fruits.append("Date")
fruits.remove(at: 1)
print(fruits)
medium
A. ["Banana", "Cherry", "Date"]
B. ["Apple", "Banana", "Cherry", "Date"]
C. ["Apple", "Cherry", "Date"]
D. ["Apple", "Date"]

Solution

  1. Step 1: Append "Date" to the fruits array

    After append, fruits = ["Apple", "Banana", "Cherry", "Date"].
  2. Step 2: Remove element at index 1

    Index 1 is "Banana", so removing it results in ["Apple", "Cherry", "Date"].
  3. Final Answer:

    ["Apple", "Cherry", "Date"] -> Option C
  4. Quick Check:

    append + remove(at: 1) = ["Apple", "Cherry", "Date"] [OK]
Hint: Append adds end; remove(at:) deletes by index [OK]
Common Mistakes:
  • Removing wrong index
  • Forgetting append adds element
  • Confusing remove(at:) with removeLast()
4. Identify the error in this Swift code snippet:
var numbers = [1, 2, 3]
numbers.remove(at: 3)
medium
A. Cannot remove elements from an array
B. Syntax error due to missing parentheses
C. No error, code runs fine
D. Index out of range error because index 3 does not exist

Solution

  1. Step 1: Check array indexes

    Array 'numbers' has indexes 0, 1, 2. Index 3 is out of range.
  2. Step 2: Understand remove(at:) behavior

    Calling remove(at: 3) causes a runtime error because the index is invalid.
  3. Final Answer:

    Index out of range error because index 3 does not exist -> Option D
  4. Quick Check:

    remove(at:) needs valid index [OK]
Hint: Check array size before remove(at:) [OK]
Common Mistakes:
  • Using invalid index without checking
  • Assuming remove(at:) can remove non-existing index
  • Confusing syntax errors with runtime errors
5. Given an empty Swift array var items = [String](), which sequence of operations will result in ["Pen", "Book", "Eraser"]?
hard
A. items.insert("Pen", at: 0)
items.insert("Book", at: 1)
items.insert("Eraser", at: 2)
B. items.append("Pen")
items.insert("Book", at: 0)
items.append("Eraser")
C. items.append("Pen")
items.append("Book")
items.insert("Eraser", at: 1)
D. items.insert("Pen", at: 1)
items.append("Book")
items.append("Eraser")

Solution

  1. Step 1: Start with empty array and insert "Pen" at index 0

    items = ["Pen"]
  2. Step 2: Insert "Book" at index 1 and "Eraser" at index 2

    items = ["Pen", "Book", "Eraser"]
  3. Step 3: Check other options for correctness

    items.append("Pen")
    items.insert("Book", at: 0)
    items.append("Eraser"): append "Pen" -> ["Pen"]; insert "Book" at 0 -> ["Book", "Pen"]; append "Eraser" -> ["Book", "Pen", "Eraser"] (wrong order).
    items.append("Pen")
    items.append("Book")
    items.insert("Eraser", at: 1): append "Pen", append "Book" -> ["Pen", "Book"]; insert "Eraser" at 1 -> ["Pen", "Eraser", "Book"] (wrong order).
    items.insert("Pen", at: 1)
    items.append("Book")
    items.append("Eraser"): insert "Pen" at 1 on empty array -> index out of range error.
  4. Final Answer:

    items.insert("Pen", at: 0)
    items.insert("Book", at: 1)
    items.insert("Eraser", at: 2)
    -> Option A
  5. Quick Check:

    Insert at correct indexes builds array [OK]
Hint: Insert at indexes 0,1,2 to build array step-by-step [OK]
Common Mistakes:
  • Inserting at invalid index in empty array
  • Confusing append and insert order
  • Assuming insert can add beyond current size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard