A stack is a collection where elements are added and removed from only one end called the top. It follows the Last In, First Out (LIFO) principle.
Click to reveal answer
beginner
Explain the LIFO principle in simple terms.
LIFO means the last item you put in is the first one you take out, like a stack of plates where you take the top plate first.
Click to reveal answer
beginner
What are the two main operations of a stack?
The two main operations are push (to add an item on top) and pop (to remove the top item).
Click to reveal answer
intermediate
What happens if you try to pop from an empty stack?
This is called underflow. It means there is no item to remove because the stack is empty.
Click to reveal answer
intermediate
How does a stack differ from a queue?
A stack uses LIFO (last in, first out), while a queue uses FIFO (first in, first out). In a stack, you remove the last added item first; in a queue, you remove the oldest item first.
Click to reveal answer
Which operation adds an element to the top of a stack?
APush
BPop
CPeek
DEnqueue
✗ Incorrect
Push adds an element to the top of the stack.
What does LIFO stand for in the context of stacks?
ALast In, Final Out
BList In, First Out
CLast In, First Out
DLast Input, First Output
✗ Incorrect
LIFO means the last element added is the first one removed.
If you pop an element from a stack, which element do you remove?
AThe bottom element
BThe top element
CA random element
DThe middle element
✗ Incorrect
Pop removes the top element from the stack.
What is the term for trying to pop from an empty stack?
AUnderflow
BNull pointer
CStack fault
DOverflow
✗ Incorrect
Underflow happens when popping from an empty stack.
Which data structure follows FIFO instead of LIFO?
AStack
BTree
CGraph
DQueue
✗ Incorrect
Queue follows FIFO (first in, first out).
Describe how a stack works and explain the LIFO principle with a real-life example.
Think about how you add and remove items from a pile.
You got /3 concepts.
List and explain the main operations of a stack and what happens during underflow.
Focus on adding/removing items and empty stack behavior.
You got /4 concepts.
Practice
(1/5)
1. What does the LIFO principle mean in the context of a stack?
easy
A. First In, First Out - the first element added is removed first
B. Last In, First Out - the last element added is removed first
C. Last In, First Out - the first element added is removed first
D. First In, Last Out - the first element added is removed last
Solution
Step 1: Understand LIFO meaning
LIFO means the last element added to the stack is the first one to be removed.
Step 2: Match with options
Last In, First Out - the last element added is removed first correctly describes this behavior as "Last In, First Out".
Final Answer:
Last In, First Out - the last element added is removed first -> Option B
Quick Check:
LIFO = Last In, First Out [OK]
Hint: Remember stack removes newest item first [OK]
Common Mistakes:
Confusing LIFO with FIFO
Thinking first added is removed first
Mixing up order of insertion and removal
2. Which of the following is the correct way to push an element 'x' onto a stack named 's' in C?
easy
A. s.push = x;
B. s.push(x);
C. push(x, s);
D. push(s, x);
Solution
Step 1: Identify correct function call syntax in C
In C, stack operations are usually functions like push(stack, element).
Step 2: Check options
push(s, x); uses push(s, x); which is the correct syntax for pushing x onto stack s.
Final Answer:
push(s, x); -> Option D
Quick Check:
Function call push(stack, element) = push(s, x) [OK]
Hint: In C, function calls have parameters in parentheses [OK]
Common Mistakes:
Using object.method() syntax (not C style)
Swapping parameter order
Assigning instead of calling function
3. Consider the following C code using a stack with push and pop operations: