Bird
0
0

Find the error in this C# code snippet:

medium📝 Debug Q7 of 15
C Sharp (C#) - Collections
Find the error in this C# code snippet:
var list = new List {1, 2, 3};
list[3] = 4;
Console.WriteLine(list.Count);
AIndex out of range error at list[3] assignment
BList initialization syntax is wrong
CCount property returns 4
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check list indexes

    List has elements at indexes 0,1,2. Index 3 is out of range.
  2. Step 2: Understand assignment at invalid index

    Assigning list[3] causes runtime IndexOutOfRangeException error.
  3. Final Answer:

    Index out of range error at list[3] assignment -> Option A
  4. Quick Check:

    Accessing invalid index = error [OK]
Quick Trick: List indexes go from 0 to Count-1 [OK]
Common Mistakes:
MISTAKES
  • Assuming list auto-expands
  • Thinking Count changes after invalid assignment
  • Ignoring runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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