Bird
0
0

What will be the output of the following C# code?

medium📝 query result Q5 of 15
C Sharp (C#) - Collections
What will be the output of the following C# code?
var list = new List { 1, 2, 3, 3, 4 };
Console.WriteLine(list.Count);
A3
B4
C5
DError at compile time
Step-by-Step Solution
Solution:
  1. Step 1: Understand List behavior

    List allows duplicate elements and counts all items added.
  2. Step 2: Count elements in the list

    The list contains 5 elements: 1, 2, 3, 3, and 4.
  3. Final Answer:

    5 -> Option C
  4. Quick Check:

    List.Count returns total elements including duplicates [OK]
Quick Trick: List counts duplicates; HashSet does not [OK]
Common Mistakes:
MISTAKES
  • Assuming duplicates are removed automatically
  • Confusing List with HashSet behavior
  • Expecting a compile-time error due to duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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