Bird
0
0

What is the output of the following Swift code?

medium📝 Predict Output Q4 of 15
Swift - Collections
What is the output of the following Swift code?
let colors = ["red", "green", "blue"]
for (index, color) in colors.enumerated() {
    print("\(index): \(color)")
}
A0: red 1: green 2: blue
Bred: 0 green: 1 blue: 2
Cred green blue
D0 red 1 green 2 blue
Step-by-Step Solution
Solution:
  1. Step 1: Understand enumerated() output

    enumerated() provides index and element pairs, so index is 0,1,2 and color is each string.
  2. Step 2: Analyze print statement format

    The print uses string interpolation with format "index: color", so output lines are "0: red", "1: green", "2: blue".
  3. Final Answer:

    0: red 1: green 2: blue -> Option A
  4. Quick Check:

    Print format matches index: element [OK]
Quick Trick: enumerated() pairs index with element in order [OK]
Common Mistakes:
  • Confusing order of index and element
  • Ignoring string interpolation format
  • Expecting reversed output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes