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?
var list = new LinkedList<string>();
list.AddLast("apple");
list.AddFirst("banana");
list.AddLast("cherry");
Console.WriteLine(list.First.Value);
Aapple
Bnull
Ccherry
Dbanana
Step-by-Step Solution
Solution:
  1. Step 1: Trace list additions

    First add "apple" at end, then "banana" at start, then "cherry" at end.
  2. Step 2: Identify first element

    After these operations, first element is "banana" because it was added at the start.
  3. Final Answer:

    banana -> Option D
  4. Quick Check:

    First element = banana [OK]
Quick Trick: AddFirst() changes the first element [OK]
Common Mistakes:
MISTAKES
  • Assuming AddLast() elements come first
  • Confusing First and Last properties
  • Expecting null when list is not empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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