Bird
0
0

Which of the following is the correct syntax to iterate over an array fruits using enumerated() in Swift?

easy📝 Syntax Q12 of 15
Swift - Collections
Which of the following is the correct syntax to iterate over an array fruits using enumerated() in Swift?
Afor (index, fruit) in fruits.enumerated() { print(index, fruit) }
Bfor index, fruit in fruits { print(index, fruit) }
Cfor index, fruit in fruits.enumerated { print(index, fruit) }
Dfor (fruit, index) in fruits.enumerated() { print(index, fruit) }
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes