Bird
Raised Fist0
Swiftprogramming~20 mins

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

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

Practice

(1/5)
1. What does the enumerated() method do when used on an array in Swift?
easy
A. It provides both the index and the value of each element during iteration.
B. It reverses the order of the array elements.
C. It filters out duplicate elements from the array.
D. It sorts the array in ascending order.

Solution

  1. Step 1: Understand the purpose of enumerated()

    The enumerated() method returns a sequence of pairs (index, value) for each element in the array.
  2. Step 2: Identify what is returned during iteration

    Each iteration gives you the position (index) and the element itself (value), allowing you to use both.
  3. Final Answer:

    It provides both the index and the value of each element during iteration. -> Option A
  4. Quick Check:

    enumerated() = index + value [OK]
Hint: Remember: enumerated() gives index and item together [OK]
Common Mistakes:
  • Thinking enumerated() changes array order
  • Believing enumerated() filters or sorts elements
  • Confusing enumerated() with map or filter
2. Which of the following is the correct syntax to iterate over an array fruits using enumerated() in Swift?
easy
A. for (index, fruit) in fruits.enumerated() { print(index, fruit) }
B. for index, fruit in fruits { print(index, fruit) }
C. for index, fruit in fruits.enumerated { print(index, fruit) }
D. for (fruit, index) in fruits.enumerated() { print(index, fruit) }

Solution

  1. Step 1: Check the correct use of enumerated()

    The method enumerated() must be called with parentheses to return the sequence.
  2. Step 2: Verify tuple unpacking syntax

    The for loop must unpack the tuple as (index, value) in parentheses.
  3. Final Answer:

    for (index, fruit) in fruits.enumerated() { print(index, fruit) } -> Option A
  4. Quick Check:

    Correct syntax = for (index, fruit) in fruits.enumerated() { print(index, fruit) } [OK]
Hint: Use parentheses with enumerated() and tuple unpacking [OK]
Common Mistakes:
  • Omitting parentheses after enumerated()
  • Swapping index and value order in tuple
  • Trying to unpack without parentheses
3. What will be the output of the following Swift code?
let animals = ["cat", "dog", "bird"]
for (i, animal) in animals.enumerated() {
    print("\(i): \(animal)")
}
medium
A. cat: 0 dog: 1 bird: 2
B. 1: cat 2: dog 3: bird
C. 0: cat 1: dog 2: bird
D. cat dog bird

Solution

  1. Step 1: Understand enumerated() output

    enumerated() gives index starting at 0 and the element value.
  2. Step 2: Trace the print statements

    Each line prints index, colon, space, then the animal name.
  3. Final Answer:

    0: cat 1: dog 2: bird -> Option C
  4. Quick Check:

    Index starts at 0, prints index: value [OK]
Hint: Indexes start at 0 unless adjusted [OK]
Common Mistakes:
  • Assuming index starts at 1
  • Printing value before index
  • Ignoring the colon and space formatting
4. Identify the error in the following Swift code snippet:
let numbers = [10, 20, 30]
for index, value in numbers.enumerated {
    print(index, value)
}
medium
A. print statement syntax is invalid.
B. Incorrect variable names in the for loop.
C. Array declaration syntax is wrong.
D. Missing parentheses after enumerated() method call.

Solution

  1. Step 1: Check method call syntax

    The method enumerated must be called with parentheses: enumerated().
  2. Step 2: Verify other parts of the code

    Variable names and array declaration are correct; print syntax is valid.
  3. Final Answer:

    Missing parentheses after enumerated() method call. -> Option D
  4. Quick Check:

    enumerated() needs () [OK]
Hint: Always add () after enumerated [OK]
Common Mistakes:
  • Forgetting parentheses after enumerated
  • Swapping index and value order
  • Using wrong print syntax
5. Given an array scores = [85, 92, 78, 90], write Swift code using enumerated() to print each score with its position starting from 1, like "Player 1: 85". Which code snippet correctly does this?
hard
A. for score in scores { print("Player \(score): \(score)") }
B. for (index, score) in scores.enumerated() { print("Player \(index + 1): \(score)") }
C. for (index, score) in scores.enumerated() { print("Player \(index): \(score)") }
D. for (score, index) in scores.enumerated() { print("Player \(index + 1): \(score)") }

Solution

  1. Step 1: Use enumerated() to get index and score

    enumerated() returns (index, value) tuples; index starts at 0.
  2. Step 2: Adjust index to start from 1 in print

    Add 1 to index when printing to start counting players from 1.
  3. Final Answer:

    for (index, score) in scores.enumerated() { print("Player \(index + 1): \(score)") } -> Option B
  4. Quick Check:

    Index + 1 for counting players [OK]
Hint: Add 1 to index to start counting from 1 [OK]
Common Mistakes:
  • Printing index without adding 1
  • Swapping index and value in tuple
  • Using wrong tuple unpacking order
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