Bird
0
0

What will be printed by this C# code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Collections
What will be printed by this C# code?
var stack = new Stack<string>();
stack.Push("apple");
stack.Push("banana");
stack.Push("cherry");
Console.WriteLine(stack.Pop());
Acherry
Bapple
Cbanana
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand Stack.Push()

    Items "apple", "banana", "cherry" are pushed in order onto the stack.
  2. Step 2: Understand Stack.Pop()

    Pop removes the last item added, which is "cherry".
  3. Final Answer:

    cherry -> Option A
  4. Quick Check:

    Stack.Pop() returns last pushed item [OK]
Quick Trick: Pop returns the newest item in Stack [OK]
Common Mistakes:
MISTAKES
  • Thinking Pop returns first item
  • Confusing Stack with Queue behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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