The 'peek' operation returns the top element of the stack without removing it, allowing you to see what is on top.
Click to reveal answer
beginner
In a stack, what happens if you try to peek when the stack is empty?
Peeking an empty stack usually results in an error or returns a special value (like null or exception) because there is no top element to show.
Click to reveal answer
beginner
Java code snippet: How to peek the top element of a stack named 'stack'?
Use stack.peek(); This returns the top element without removing it.
Click to reveal answer
intermediate
Why is peek useful compared to pop in stack operations?
Peek lets you check the top element without changing the stack, while pop removes the top element. Peek is useful when you want to look but not remove.
Click to reveal answer
beginner
What is the time complexity of the peek operation in a stack?
The peek operation runs in O(1) time because it directly accesses the top element without any traversal.
Click to reveal answer
What does the peek operation do in a stack?
AReturns the top element without removing it
BRemoves the top element
CAdds a new element to the top
DChecks if the stack is empty
✗ Incorrect
Peek returns the top element without removing it, unlike pop which removes it.
What happens if you peek an empty stack in Java's Stack class?
AThrows EmptyStackException
BReturns 0
CReturns null
DRemoves an element
✗ Incorrect
Java's Stack.peek() throws EmptyStackException if the stack is empty.
Which method is used to peek the top element in Java's Stack?
Atop()
Bpeek()
Cpop()
Dget()
✗ Incorrect
The peek() method returns the top element without removing it.
What is the time complexity of the peek operation in a stack?
AO(n)
BO(log n)
CO(n^2)
DO(1)
✗ Incorrect
Peek runs in constant time O(1) because it accesses the top element directly.
Why would you use peek instead of pop?
ATo remove the top element
BTo add a new element
CTo see the top element without removing it
DTo clear the stack
✗ Incorrect
Peek lets you see the top element without removing it, unlike pop which removes it.
Explain how the peek operation works in a stack and what happens if the stack is empty.
Think about looking at the top card in a deck without taking it out.
You got /3 concepts.
Write a simple Java code snippet to peek the top element of a stack and handle the case when the stack is empty.
Use try-catch to avoid errors when stack is empty.
You got /3 concepts.
Practice
(1/5)
1. What does the peek() method do in a stack?
easy
A. Checks if the stack is empty
B. Removes and returns the top element
C. Adds a new element to the top
D. Returns the top element without removing it
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.
Step 2: Compare with other stack methods
Unlike pop, peek does not remove the element; it only returns it.
Final Answer:
Returns the top element without removing it -> Option D
Quick Check:
peek() = 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 checks emptiness
2. Which of the following is the correct syntax to peek the top element of a Java Stack named stack?
easy
A. stack.peek();
B. stack.top();
C. stack.getTop();
D. stack.pop();
Solution
Step 1: Recall Java Stack method names
The Java Stack class uses peek() to view the top element without removing it.
Step 2: Check other options for correctness
top() and getTop() are not valid Stack methods; pop() removes the top element.
Final Answer:
stack.peek(); -> Option A
Quick Check:
Correct syntax for peek is stack.peek() [OK]
Hint: Use stack.peek() to view top element [OK]
Common Mistakes:
Using pop() instead of peek()
Using non-existent methods like top()
Missing parentheses in method call
3. What will be the output of the following Java code?
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println(stack.peek());
medium
A. 10
B. 30
C. 20
D. StackEmptyException
Solution
Step 1: Trace the push operations
Elements pushed in order: 10 (bottom), 20, then 30 (top).
Step 2: Understand peek output
peek() returns the top element without removing it, which is 30.
Final Answer:
30 -> Option B
Quick Check:
Top element after pushes = 30 [OK]
Hint: Peek shows last pushed element [OK]
Common Mistakes:
Confusing peek with pop output
Assuming peek returns bottom element
Expecting exception on non-empty stack
4. Identify the error in the following code snippet that tries to peek the top element of a stack:
Stack<String> stack = new Stack<>();
System.out.println(stack.peek());
medium
A. Throws EmptyStackException at runtime
B. Prints empty string
C. Compilation error: peek() not found
D. No error, prints null
Solution
Step 1: Check stack initialization and usage
The stack is created but no elements are pushed before peek is called.
Step 2: Understand peek behavior on empty stack
Calling peek() on an empty stack throws EmptyStackException at runtime.
Final Answer:
Throws EmptyStackException at runtime -> Option A
Quick Check:
peek() on empty stack throws exception [OK]
Hint: Peek on empty stack throws exception [OK]
Common Mistakes:
Assuming peek returns null if empty
Expecting compile-time error
Thinking it prints empty string
5. Given a stack with elements [5, 10, 15] (5 at bottom, 15 at top), which sequence of operations will peek the top element without removing it and then add 20 on top?