0
0
Swiftprogramming~20 mins

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

Choose your learning style9 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.