Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q6 of 15
Swift - Collections
Identify the error in this Swift code:
let items = ["Pen", "Pencil"]
items.append("Eraser")
ANo error, code runs fine
BCannot append to a constant array
CSyntax error in array declaration
DEraser is not a valid string
Step-by-Step Solution
Solution:
  1. Step 1: Check declaration of items

    items is declared with let, so it is immutable.
  2. Step 2: Append operation on immutable array

    Appending to a constant array is not allowed and causes a compile-time error.
  3. Final Answer:

    Cannot append to a constant array -> Option B
  4. Quick Check:

    Appending to let array = Error [OK]
Quick Trick: Append only works on var arrays [OK]
Common Mistakes:
  • Thinking append works on let arrays
  • Misreading syntax as incorrect
  • Ignoring immutability rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes