Bird
0
0

Examine the following C# code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Collections
Examine the following C# code snippet:
var stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
int value = stack.Dequeue();

What is the issue here?
AThe variable 'value' should be declared as 'string'
BYou cannot push integers onto a Stack<int>
CStack does not have a 'Dequeue' method
DStack requires initialization with a capacity
Step-by-Step Solution
Solution:
  1. Step 1: Identify methods of Stack

    Stack<T> supports Push, Pop, and Peek methods, but not Dequeue.
  2. Step 2: Understand Dequeue usage

    Dequeue is a method of Queue<T>, not Stack<T>.
  3. Final Answer:

    Stack does not have a 'Dequeue' method -> Option C
  4. Quick Check:

    Stack uses Pop, not Dequeue [OK]
Quick Trick: Stack uses Pop, Queue uses Dequeue [OK]
Common Mistakes:
MISTAKES
  • Using Queue methods on Stack objects
  • Assuming Dequeue exists for Stack

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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