What does the enumerated() method do when used on an array in Swift?
The enumerated() method returns a sequence of pairs, where each pair contains an index and the corresponding element from the array. This helps to access both the position and value during iteration.
Click to reveal answer
beginner
How do you write a simple for-in loop to iterate over all elements in a Swift array named fruits?
You write: for fruit in fruits { print(fruit) } This prints each fruit in the array one by one.
Click to reveal answer
intermediate
Why might you want to use enumerated() instead of a simple for-in loop?
Using enumerated() lets you know the index of each element while looping. This is useful when you need to know the position of the item, like numbering a list or updating elements by index.
Click to reveal answer
intermediate
What is the type of the value returned by enumerated() on an array?
It returns a sequence of tuples where each tuple has two parts: an Int index and the element of the array's type. For example, (Int, String) if the array holds strings.
Click to reveal answer
beginner
Write a Swift code snippet that prints the index and value of each element in an array colors using enumerated().
let colors = ["Red", "Green", "Blue"]
for (index, color) in colors.enumerated() {
print("\(index): \(color)")
}
This prints:<br>0: Red<br>1: Green<br>2: Blue
Click to reveal answer
What does the enumerated() method return when called on an array?
AOnly the elements of the array
BA sequence of (index, element) pairs
COnly the indices of the array
DA reversed array
✗ Incorrect
enumerated() returns pairs of index and element, allowing access to both during iteration.
How do you access the index and value inside a for-in loop using enumerated()?
Afor (index, value) in array.enumerated()
Bfor index in array.enumerated()
Cfor value in array.enumerated()
Dfor array in (index, value)
✗ Incorrect
You use tuple unpacking: for (index, value) in array.enumerated().
Which of these is a correct way to iterate over an array numbers to print each element?
Afor number in numbers { print(number) }
Bnumbers.forEach(print)
CAll of the above
Dfor (index, number) in numbers.enumerated() { print(number) }
✗ Incorrect
All listed methods correctly iterate and print elements of the array.
What type is the index provided by enumerated()?
AInt
BString
CDouble
DBool
✗ Incorrect
The index is always an Int representing the position in the array.
If you want to print the position starting from 1 instead of 0 using enumerated(), what should you do?
ASubtract 1 from the index
BUse <code>enumerated(startingAt: 1)</code>
CYou cannot change the starting index
DAdd 1 to the index inside the loop
✗ Incorrect
You add 1 to the index manually inside the loop to start counting from 1.
Explain how to use enumerated() to loop through an array and access both the index and the element.
Think about how you get both position and value in one loop.
You got /3 concepts.
Describe the difference between a simple for-in loop over an array and using enumerated() in Swift.
One gives you just the items, the other gives you items with their positions.
You got /3 concepts.
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
Step 1: Understand the purpose of enumerated()
The enumerated() method returns a sequence of pairs (index, value) for each element in the array.
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.
Final Answer:
It provides both the index and the value of each element during iteration. -> Option A
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
Step 1: Check the correct use of enumerated()
The method enumerated() must be called with parentheses to return the sequence.
Step 2: Verify tuple unpacking syntax
The for loop must unpack the tuple as (index, value) in parentheses.
Final Answer:
for (index, fruit) in fruits.enumerated() { print(index, fruit) } -> Option A
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
Step 1: Understand enumerated() output
enumerated() gives index starting at 0 and the element value.
Step 2: Trace the print statements
Each line prints index, colon, space, then the animal name.
Final Answer:
0: cat
1: dog
2: bird -> Option C
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
Step 1: Check method call syntax
The method enumerated must be called with parentheses: enumerated().
Step 2: Verify other parts of the code
Variable names and array declaration are correct; print syntax is valid.
Final Answer:
Missing parentheses after enumerated() method call. -> Option D
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
Step 1: Use enumerated() to get index and score
enumerated() returns (index, value) tuples; index starts at 0.
Step 2: Adjust index to start from 1 in print
Add 1 to index when printing to start counting players from 1.
Final Answer:
for (index, score) in scores.enumerated() { print("Player \(index + 1): \(score)") } -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.