Bird
0
0

Identify the error in this Swift code snippet:

medium📝 Debug Q6 of 15
Swift - Collections
Identify the error in this Swift code snippet:
var letters = ["a", "b", "c"]
letters.remove(at: 3)
ASyntax error due to missing parentheses.
BIndex out of range error because index 3 does not exist.
CNo error; element at index 3 is removed.
DRuntime error because array is empty.
Step-by-Step Solution
Solution:
  1. Step 1: Check array indices

    Array has elements at indices 0, 1, 2 only.
  2. Step 2: Remove at index 3

    Index 3 is out of range, causing runtime error.
  3. Final Answer:

    Index out of range error because index 3 does not exist. -> Option B
  4. Quick Check:

    remove(at:) with invalid index = runtime error [OK]
Quick Trick: Indices start at 0; max index is count-1 [OK]
Common Mistakes:
  • Assuming remove(at:) silently fails
  • Confusing syntax error with runtime error
  • Thinking array is empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes