Bird
0
0

Find the bug in this Swift code:

medium📝 Debug Q7 of 15
Swift - Collections
Find the bug in this Swift code:
let letters = ["a", "b", "c"]
for (i, letter) in letters.enumerated() {
    print(letter, i)
}
AThe order of variables in print is reversed
BThere is no bug; code runs correctly
CThe loop variables should be declared as var
Denumerated() should be called without parentheses
Step-by-Step Solution
Solution:
  1. Step 1: Review enumerated() usage and loop syntax

    enumerated() is correctly called with parentheses, and loop variables are correctly declared.
  2. Step 2: Check print statement

    Printing letter then index is valid and will output the element followed by its index.
  3. Final Answer:

    There is no bug; code runs correctly -> Option B
  4. Quick Check:

    Valid syntax and logic means no bug [OK]
Quick Trick: Order in print can be any; no bug if syntax correct [OK]
Common Mistakes:
  • Thinking print order matters for correctness
  • Omitting parentheses on enumerated()
  • Misdeclaring loop variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes