A stack is a collection of elements that follows the Last In, First Out (LIFO) principle, where the last element added is the first one to be removed.
Click to reveal answer
beginner
What does the 'push' operation do in a stack?
The 'push' operation adds a new element to the top of the stack.
Click to reveal answer
beginner
What is the purpose of the 'pop' operation in a stack?
The 'pop' operation removes and returns the top element from the stack.
Click to reveal answer
beginner
Explain the 'peek' operation in a stack.
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
Why is the stack called Last In, First Out (LIFO)?
Because the last element pushed (added) onto the stack is the first one to be popped (removed), like a stack of plates where you take the top plate first.
Click to reveal answer
Which operation adds an element to the top of a stack?
AInsert
BPop
CPush
DPeek
✗ Incorrect
The 'push' operation adds a new element to the top of the stack.
What does the 'pop' operation do?
AReturns the bottom element
BRemoves and returns the top element
CAdds an element to the stack
DChecks if the stack is empty
✗ Incorrect
The 'pop' operation removes and returns the top element from the stack.
What is the result of the 'peek' operation?
AReturns the top element without removing it
BRemoves the top element
CAdds an element to the bottom
DClears the stack
✗ Incorrect
The 'peek' operation returns the top element without removing it.
Which principle does a stack follow?
ALast In, First Out (LIFO)
BLast In, Last Out (LILO)
CFirst In, First Out (FIFO)
DRandom Access
✗ Incorrect
A stack follows the Last In, First Out (LIFO) principle.
If you push elements 1, 2, 3 in order, which element will pop return first?
A1
BNone
C2
D3
✗ Incorrect
The last pushed element (3) is the first to be popped in a stack.
Describe the three main operations of a stack and their effects.
Think about how you add, remove, and check the top item in a stack of objects.
You got /3 concepts.
Explain why a stack is called Last In, First Out (LIFO) with a real-life example.
Imagine a pile of books or plates and how you take them off.
You got /3 concepts.
Practice
(1/5)
1. What does the pop operation do in a stack?
easy
A. Shows the bottom item without removing it
B. Adds a new item to the bottom of the stack
C. Removes and returns the top item from the stack
D. Clears all items from the stack
Solution
Step 1: Understand the pop operation
The pop operation removes the last item added to the stack, which is the top item.
Step 2: Confirm the behavior of pop
It returns this top item and removes it from the stack, following last-in, first-out order.
Final Answer:
Removes and returns the top item from the stack -> Option C
Quick Check:
pop = remove top item [OK]
Hint: Pop always removes the last item added [OK]
Common Mistakes:
Thinking pop adds an item
Confusing pop with peek
Assuming pop removes bottom item
2. Which of the following is the correct way to add an item 5 to a stack named s using the push operation?
easy
A. s.pop(5)
B. s.push(5)
C. s.peek(5)
D. s.add(5)
Solution
Step 1: Identify the push syntax
Push adds an item to the top of the stack, usually called with push(item).
Step 2: Match the correct method call
Only s.push(5) correctly adds 5 to the stack.
Final Answer:
s.push(5) -> Option B
Quick Check:
push = add item [OK]
Hint: Push means add, so look for push() method [OK]
Common Mistakes:
Using pop instead of push
Using peek to add items
Using add which is not standard for stacks
3. Given the following stack operations:
s.push(10) s.push(20) s.pop() s.push(30) top = s.peek()
What is the value of top after these operations?
medium
A. 30
B. 20
C. None
D. 10
Solution
Step 1: Trace the stack changes step-by-step
After push(10), stack = [10]. After push(20), stack = [10, 20]. After pop(), top 20 is removed, stack = [10]. After push(30), stack = [10, 30].
Step 2: Determine the top item with peek
Peek returns the top item without removing it, so top = 30.
Final Answer:
30 -> Option A
Quick Check:
Last pushed after pop is 30 [OK]
Hint: Track stack after each operation carefully [OK]