Bird
0
0

What will be the output of this C# code?

medium📝 query result Q13 of 15
C Sharp (C#) - Collections
What will be the output of this C# code?
var fruits = new List { "apple", "banana", "cherry" };
fruits.RemoveAt(1);
Console.WriteLine(fruits[1]);
Abanana
BIndexOutOfRangeException
Ccherry
Dapple
Step-by-Step Solution
Solution:
  1. Step 1: Understand RemoveAt effect on list

    RemoveAt(1) removes the item at index 1, which is "banana". The list becomes ["apple", "cherry"].
  2. Step 2: Access the item at index 1 after removal

    After removal, fruits[1] is "cherry" because the list shifted left.
  3. Final Answer:

    cherry -> Option C
  4. Quick Check:

    RemoveAt shifts items left, fruits[1] = cherry [OK]
Quick Trick: RemoveAt shifts list left; index 1 now points to next item [OK]
Common Mistakes:
MISTAKES
  • Assuming removed item still exists
  • Expecting original index items unchanged
  • Confusing RemoveAt with Remove

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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