Bird
Raised Fist0

What is the output of this C# code?

medium📝 Predict Output Q13 of Q15
C Sharp (C#) - Collections
What is the output of this C# code?
var stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
Console.WriteLine(stack.Pop());
Console.WriteLine(stack.Peek());
A3\n2
B1\n2
C2\n3
D3\n3
Step-by-Step Solution
Solution:
  1. Step 1: Trace Push operations

    Stack after pushes: bottom=1, middle=2, top=3.
  2. Step 2: Execute Pop and Peek

    Pop() removes and returns top (3). Peek() returns new top (2) without removing.
  3. Final Answer:

    3\n2 -> Option A
  4. Quick Check:

    Pop=3, Peek=2 [OK]
Quick Trick: Pop removes top, Peek shows top without removing [OK]
Common Mistakes:
MISTAKES
  • Mixing Pop and Peek results
  • Assuming FIFO order
  • Confusing stack order
  • Forgetting Pop removes item

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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