Bird
0
0

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

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Collections
What will be the output of the following C# code?
var set = new HashSet<string>();
set.Add("apple");
set.Add("banana");
set.Add("apple");
Console.WriteLine(set.Count);
A0
B3
C1
D2
Step-by-Step Solution
Solution:
  1. Step 1: Add elements to HashSet

    "apple" is added first, then "banana", then "apple" again.
  2. Step 2: Understand duplicate handling

    The second "apple" is ignored because HashSet stores unique elements only.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Duplicates ignored, count = 2 [OK]
Quick Trick: Count equals unique items added, duplicates ignored [OK]
Common Mistakes:
MISTAKES
  • Counting duplicates as separate elements
  • Assuming HashSet allows duplicates
  • Confusing Count with number of Add calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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