Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Collections
What will be the output of this C# code?
List fruits = new List() {"apple", "banana"};
fruits.Add("cherry");
Console.WriteLine(fruits.Count);
A3
B2
C1
DError
Step-by-Step Solution
Solution:
  1. Step 1: Analyze initial list size

    The list starts with two elements: "apple" and "banana".
  2. Step 2: Analyze Add method effect

    Adding "cherry" increases the list size by one, making total 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    List count after Add = 3 [OK]
Quick Trick: Add increases collection size; Count reflects total elements [OK]
Common Mistakes:
MISTAKES
  • Assuming Count remains 2 after Add
  • Confusing Count with capacity
  • Expecting runtime error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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