0
0
Swiftprogramming~20 mins

Array iteration and enumerated in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Array Iteration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of enumerated array iteration
What is the output of this Swift code?
Swift
let fruits = ["Apple", "Banana", "Cherry"]
for (index, fruit) in fruits.enumerated() {
    print("\(index): \(fruit)")
}
A
Apple: 0
Banana: 1
Cherry: 2
B
1: Apple
2: Banana
3: Cherry
C
0: Apple
1: Banana
2: Cherry
D
Apple
Banana
Cherry
Attempts:
2 left
💡 Hint
Remember that enumerated() gives pairs of (index, element) starting at 0.
Predict Output
intermediate
2:00remaining
Enumerated index starting value
What will be printed by this Swift code?
Swift
let numbers = [10, 20, 30]
for (index, number) in numbers.enumerated() {
    print(index + 1, number)
}
A
1 20
2 30
3 40
B
0 10
1 20
2 30
C
10 1
20 2
30 3
D
1 10
2 20
3 30
Attempts:
2 left
💡 Hint
The index from enumerated() starts at 0, but the code adds 1 before printing.
🔧 Debug
advanced
2:00remaining
Why does this enumerated loop cause an error?
This Swift code causes a compile error. What is the reason?
Swift
let items = ["a", "b", "c"]
for index, item in items.enumerated() {
    print(index, item)
}
AThe array 'items' is empty
BMissing parentheses around (index, item) in the for loop
CThe variable 'item' is not declared
DCannot use enumerated() on arrays
Attempts:
2 left
💡 Hint
Check the syntax for tuple unpacking in for loops.
Predict Output
advanced
2:00remaining
Output of modifying array during enumerated iteration
What is the output of this Swift code?
Swift
var letters = ["x", "y", "z"]
for (i, letter) in letters.enumerated() {
    letters[i] = letter.uppercased()
}
print(letters)
A["X", "Y", "Z"]
B["x", "y", "z"]
C["X", "y", "z"]
DRuntime error: Concurrent modification
Attempts:
2 left
💡 Hint
Think about whether modifying elements by index during iteration is allowed.
🧠 Conceptual
expert
2:00remaining
Count of elements after filtering with enumerated
Given this Swift code, how many elements will be in the filtered array?
Swift
let values = [5, 10, 15, 20, 25]
let filtered = values.enumerated().filter { index, value in
    index % 2 == 0 && value > 10
}.map { $0.element }
print(filtered.count)
A2
B1
C3
D4
Attempts:
2 left
💡 Hint
Check which elements have even index and value greater than 10.