A stack is like a pile of plates where you can only add or remove the top plate. It follows Last In, First Out (LIFO) order.
Click to reveal answer
beginner
Why do we need a stack in programming?
Stacks help manage tasks that need to be done in reverse order, like undo actions, function calls, or checking matching brackets.
Click to reveal answer
intermediate
How does a stack solve the problem of function calls?
When functions call other functions, the stack keeps track of where to return after each call, ensuring the last called function finishes first.
Click to reveal answer
intermediate
What problem does stack solve in expression evaluation?
Stacks help evaluate expressions by storing operators and operands in the correct order, especially for expressions with parentheses or operator precedence.
Click to reveal answer
beginner
Explain how stack helps in undo functionality.
Each action is pushed onto the stack. When undo is pressed, the last action is popped and reversed, allowing step-by-step undo in reverse order.
Click to reveal answer
What order does a stack follow?
ALast In, First Out (LIFO)
BFirst In, First Out (FIFO)
CRandom Order
DSorted Order
✗ Incorrect
A stack always removes the most recently added item first, which is Last In, First Out.
Which problem is best solved by using a stack?
ASearching in a list
BSorting numbers
CStoring unique items
DTracking function calls
✗ Incorrect
Stacks keep track of function calls and return points, making them ideal for this problem.
What happens when you pop from an empty stack?
ANothing happens
BYou get the first item
CYou get an error or exception
DThe stack resets
✗ Incorrect
Popping from an empty stack usually causes an error because there is nothing to remove.
How does a stack help in checking balanced parentheses?
ABy ignoring brackets
BBy pushing opening brackets and popping when closing brackets appear
CBy counting total brackets
DBy sorting the brackets
✗ Incorrect
Stacks push opening brackets and pop them when matching closing brackets appear, ensuring balance.
Which real-life example best describes a stack?
AA stack of plates
BA queue at a ticket counter
CA bookshelf
DA shopping list
✗ Incorrect
A stack of plates is a perfect example because you add and remove plates from the top only.
Explain why stacks are important in managing function calls in programming.
Think about how the program remembers where to go back after a function finishes.
You got /4 concepts.
Describe how stacks help solve problems like undo actions and expression evaluation.
Consider how the last thing done is the first to be undone or evaluated.
You got /4 concepts.
Practice
(1/5)
1. Why do we use a stack data structure in programming?
easy
A. To store large amounts of data permanently
B. To store data in random order
C. To sort numbers automatically
D. To keep track of function calls and return addresses
Solution
Step 1: Understand the role of stack in function calls
Stacks help keep track of where each function should return after finishing execution, storing return addresses in order.
Step 2: Compare other options
Stacks do not sort data automatically, nor store data randomly or permanently; their main use is managing function calls.
Final Answer:
To keep track of function calls and return addresses -> Option D
Quick Check:
Stack manages function calls = A [OK]
Hint: Stack remembers last task first, like a pile of plates [OK]
Common Mistakes:
Thinking stack sorts data automatically
Confusing stack with permanent storage
Assuming stack stores data randomly
2. Which of the following is the correct way to push an element onto a stack in Java?
easy
A. stack.push(element);
B. stack.put(element);
C. stack.insert(element);
D. stack.add(element);
Solution
Step 1: Recall Java Stack method names
The Java Stack class uses the method push() to add elements on top of the stack.
Step 2: Check other method names
Methods like add(), insert(), or put() are not standard for Stack push operation in Java.
Final Answer:
stack.push(element); -> Option A
Quick Check:
Push method adds element = D [OK]
Hint: Use push() to add on top of stack in Java [OK]
Common Mistakes:
Using add() instead of push()
Confusing insert() with push()
Using put() which is not a Stack method
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.pop());
System.out.println(stack.peek());
medium
A. 20
30
B. 10
20
C. 30
20
D. 30
10
Solution
Step 1: Trace push operations
Elements pushed in order: 10 (bottom), 20, 30 (top).
Step 2: Trace pop and peek
pop() removes and returns top element 30; peek() returns new top element 20 without removing it.
Final Answer:
30
20 -> Option C
Quick Check:
pop()=30, peek()=20 [OK]
Hint: pop() removes top, peek() shows top without removing [OK]
Common Mistakes:
Confusing pop() and peek() results
Assuming pop() returns bottom element
Mixing order of pushed elements
4. Identify the error in the following Java code using Stack:
Stack<String> stack = new Stack<>();
stack.push("A");
stack.push("B");
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
medium
A. Stack underflow error at third pop()
B. No error, code runs fine
C. Syntax error in stack declaration
D. push() method is incorrect
Solution
Step 1: Count push and pop operations
Two elements pushed: "A", "B"; three pop() calls made.
Step 2: Analyze pop() calls
First two pop() calls remove "B" and "A"; third pop() tries to remove from empty stack causing underflow error.
Final Answer:
Stack underflow error at third pop() -> Option A
Quick Check:
More pops than pushes = underflow error [OK]
Hint: Pop more times than push causes error [OK]
Common Mistakes:
Ignoring stack underflow on extra pop()
Thinking push() syntax is wrong
Assuming no error occurs
5. You want to reverse a string using a stack in Java. Which sequence of operations correctly achieves this?
hard
A. Push all characters, then push again to reverse
B. Push each character, then pop all to build reversed string
C. Pop each character, then push all to build reversed string
D. Peek each character, then pop all to build reversed string
Solution
Step 1: Understand stack reversal logic
Pushing characters stores them in order; popping them reverses order because stack is last-in-first-out.
Step 2: Evaluate options
Only pushing then popping all characters correctly reverses the string; popping before pushing or peeking does not reverse.
Final Answer:
Push each character, then pop all to build reversed string -> Option B
Quick Check:
Push then pop reverses string = C [OK]
Hint: Push all, then pop all to reverse order [OK]
Common Mistakes:
Trying to pop before pushing
Using peek instead of pop to build reversed string
Pushing twice without popping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.