0
0
C Sharp (C#)programming~20 mins

Queue and Stack behavior in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Queue and Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Queue operation?
Consider the following C# code using a Queue. What will be printed to the console?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<int> q = new Queue<int>();
        q.Enqueue(10);
        q.Enqueue(20);
        q.Enqueue(30);
        Console.WriteLine(q.Dequeue());
        Console.WriteLine(q.Peek());
    }
}
A20\n30
B10\n30
C10\n20
D20\n10
Attempts:
2 left
💡 Hint
Remember, Queue follows FIFO: first in, first out.
Predict Output
intermediate
2:00remaining
What is the output of this Stack operation?
Look at this C# code using a Stack. What will be printed?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Stack<string> s = new Stack<string>();
        s.Push("apple");
        s.Push("banana");
        s.Push("cherry");
        Console.WriteLine(s.Pop());
        Console.WriteLine(s.Peek());
    }
}
Acherry\nbanana
Bapple\nbanana
Cbanana\napple
Dcherry\napple
Attempts:
2 left
💡 Hint
Stack follows LIFO: last in, first out.
🔧 Debug
advanced
2:00remaining
Why does this Queue code throw an exception?
This C# code throws an InvalidOperationException at runtime. Why?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<int> q = new Queue<int>();
        q.Enqueue(1);
        q.Dequeue();
        q.Dequeue();
    }
}
ABecause Dequeue is called on an empty Queue after removing the only element.
BBecause Enqueue was not called before Dequeue.
CBecause Queue does not support Dequeue operation.
DBecause the Queue was not initialized properly.
Attempts:
2 left
💡 Hint
Check what happens when you remove more items than exist.
Predict Output
advanced
2:00remaining
What is the final content of the Stack?
After running this C# code, what items remain in the Stack (from top to bottom)?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Stack<int> s = new Stack<int>();
        s.Push(5);
        s.Push(10);
        s.Push(15);
        s.Pop();
        s.Push(20);
        foreach(var item in s) {
            Console.Write(item + " ");
        }
    }
}
A5 10 20
B20 10 5
C10 5 20
D15 20 10 5
Attempts:
2 left
💡 Hint
Remember Pop removes the top item, Push adds on top, and foreach iterates from top to bottom.
🧠 Conceptual
expert
2:00remaining
Which statement correctly describes the difference between Queue and Stack?
Choose the option that best explains the fundamental difference between Queue and Stack data structures.
AQueue and Stack both remove elements randomly without any order.
BStack removes elements in the order they were added; Queue removes elements in reverse order of addition.
CBoth Queue and Stack remove elements in the order they were added, but Stack is faster.
DQueue removes elements in the order they were added; Stack removes elements in reverse order of addition.
Attempts:
2 left
💡 Hint
Think about FIFO vs LIFO.