📖 Scenario: You are managing a stack of books in a library. You want to check which book is currently on the top without removing it.
🎯 Goal: Build a simple program that creates a stack of book titles, then shows the top book using the peek operation.
📋 What You'll Learn
Create a stack of strings called books with these exact titles in this order: "Math", "Science", "History"
Create an integer variable called stackSize to hold the number of books in the stack
Use the Peek() method on the books stack to get the top book without removing it
Print the top book title exactly as shown
💡 Why This Matters
🌍 Real World
Stacks are used in many real-world applications like undo features in text editors, browser history, and managing tasks in order.
💼 Career
Understanding stack operations like push, pop, and peek is fundamental for software development roles, especially in algorithm design and system programming.
Progress0 / 4 steps
1
Create the stack of books
Create a stack of strings called books and add these titles in this order: "Math", "Science", "History"
DSA C#
Hint
Use Stack<string> and the Push() method to add items.
2
Add a variable for stack size
Create an integer variable called stackSize and set it to the number of books in the books stack using the Count property
DSA C#
Hint
Use books.Count to get the number of items in the stack.
3
Peek the top book
Create a string variable called topBook and set it to the top book title using the Peek() method on the books stack
DSA C#
Hint
Use books.Peek() to see the top item without removing it.
4
Print the top book
Print the value of topBook using Console.WriteLine() exactly as shown
DSA C#
Hint
Use Console.WriteLine(topBook); to print the top book.
Practice
(1/5)
1. What does the Peek() method do in a stack?
easy
A. Returns the top element without removing it
B. Removes and returns the top element
C. Adds a new element to the top
D. Checks if the stack is empty
Solution
Step 1: Understand the Peek method purpose
The Peek method is designed to look at the top element of the stack without changing the stack itself.
Step 2: Differentiate from Pop method
Unlike Pop, Peek does not remove the element; it only returns it.
Final Answer:
Returns the top element without removing it -> Option A
Quick Check:
Peek() returns top element without removal [OK]
Hint: Peek shows top without removing it [OK]
Common Mistakes:
Confusing Peek with Pop
Thinking Peek adds elements
Assuming Peek empties the stack
2. Which of the following is the correct syntax to peek the top element of a stack named myStack in C#?
easy
A. var top = myStack.Pop();
B. var top = myStack.Top();
C. var top = myStack.Peek();
D. var top = myStack.GetTop();
Solution
Step 1: Recall Stack method names in C#
The Stack class in C# uses Peek() to look at the top element without removing it.
Step 2: Verify method correctness
Top(), GetTop() are not valid Stack methods; Pop() removes the element.
Final Answer:
var top = myStack.Peek(); -> Option C
Quick Check:
Peek() is correct syntax for top element [OK]
Hint: Use Peek() method to get top element [OK]
Common Mistakes:
Using Pop() instead of Peek()
Using non-existent methods like Top()
Missing parentheses after Peek
3. What will be the output of the following C# code?
Stack<int> numbers = new Stack<int>();
numbers.Push(10);
numbers.Push(20);
numbers.Push(30);
Console.WriteLine(numbers.Peek());
Console.WriteLine(numbers.Count);
medium
A. 10
3
B. 30
3
C. 20
2
D. 30
2
Solution
Step 1: Trace the Push operations
Elements pushed in order: 10, then 20, then 30. Stack top is 30.
Step 2: Evaluate Peek and Count
Peek returns top element 30 without removing it. Count remains 3 as no Pop occurred.
Final Answer:
30
3 -> Option B
Quick Check:
Peek = 30, Count = 3 [OK]
Hint: Peek shows last pushed; Count unchanged by Peek [OK]
Common Mistakes:
Assuming Peek removes element
Confusing Count after Peek
Mixing order of pushed elements
4. Identify the error in this code snippet that tries to peek the top element of a stack:
Stack<string> stack = new Stack<string>();
string top = stack.Peek;
Console.WriteLine(top);
medium
A. Peek removes the element, so top is null
B. Stack is not initialized properly
C. Console.WriteLine cannot print stack elements
D. Peek is used without parentheses, causing a syntax error
Solution
Step 1: Check method call syntax
Peek is a method and must be called with parentheses: Peek()
Step 2: Identify error type
Using Peek without parentheses causes a compile-time syntax error.
Final Answer:
Peek is used without parentheses, causing a syntax error -> Option D
Quick Check:
Method calls need () [OK]
Hint: Always use parentheses when calling methods [OK]
Common Mistakes:
Forgetting parentheses on Peek
Assuming Peek removes element
Thinking Console.WriteLine can't print strings
5. Given a stack Stack<char> letters with elements 'a', 'b', 'c' pushed in that order, write the output after this code: