Bird
0
0

Identify the error in this C# code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Collections
Identify the error in this C# code snippet:
var items = new List<string>>();
items.Add("apple");
items.RemoveAt(1);
ARemoveAt(1) causes an exception because the list has only one element at index 0.
BAdd("apple") is invalid syntax.
CList cannot be empty when calling RemoveAt.
DRemoveAt requires a string parameter, not an integer.
Step-by-Step Solution
Solution:
  1. Step 1: Check list contents

    After adding "apple", list has one element at index 0.
  2. Step 2: RemoveAt(1)

    Index 1 does not exist, so this causes an ArgumentOutOfRangeException.
  3. Final Answer:

    RemoveAt(1) causes an exception because the list has only one element at index 0. -> Option A
  4. Quick Check:

    RemoveAt index must exist [OK]
Quick Trick: RemoveAt index must be valid [OK]
Common Mistakes:
MISTAKES
  • Assuming RemoveAt accepts string
  • Thinking Add syntax is wrong
  • Believing RemoveAt works on empty list

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes