A stack is empty when it has no elements inside it. This means there is nothing to remove or peek at.
Click to reveal answer
beginner
How can you check if a stack is full in a fixed-size array implementation?
You check if the top pointer/index is at the last position of the array. If yes, the stack is full and cannot hold more elements.
Click to reveal answer
beginner
What is the typical condition to check if a stack is empty in code?
The stack is empty if the top index is -1 (or some initial value indicating no elements).
Click to reveal answer
intermediate
Why is it important to check if a stack is full before pushing an element?
To avoid overflow errors or trying to add elements beyond the stack's capacity, which can cause program crashes or data corruption.
Click to reveal answer
beginner
What happens if you try to pop from an empty stack?
It causes an underflow error because there are no elements to remove, which can lead to unexpected behavior or crashes.
Click to reveal answer
What is the initial value of the top pointer/index in an empty stack?
A0
B-1
C1
DStack size
✗ Incorrect
The top index is usually set to -1 to indicate the stack is empty before any elements are pushed.
When is a stack considered full in an array implementation?
AWhen top == array length - 1
BWhen top == 0
CWhen top == -1
DWhen array is empty
✗ Incorrect
The stack is full when the top index reaches the last valid position in the array.
What should you check before popping an element from a stack?
AIf the stack is full
BIf the stack size is zero
CIf the stack is empty
DIf the stack has only one element
✗ Incorrect
You must check if the stack is empty to avoid underflow errors.
Which of these is NOT a reason to check if a stack is full before pushing?
ATo check if stack is empty
BTo avoid adding beyond capacity
CTo prevent overflow
DTo maintain program stability
✗ Incorrect
Checking if the stack is empty is unrelated to pushing; you check if it is full before pushing.
What does the 'top' variable represent in a stack?
ABottom element index
BTotal size of the stack
CNumber of empty spaces
DIndex of the last pushed element
✗ Incorrect
The 'top' points to the index of the most recently added element in the stack.
Explain how to check if a stack is empty or full in an array-based stack.
Think about the position of the top pointer relative to the array size.
You got /3 concepts.
Why is it important to check stack status before push or pop operations?
Consider what happens if you ignore these checks.
You got /4 concepts.
Practice
(1/5)
1. What does the isEmpty() method in a stack typically check?
easy
A. If the stack has exactly one element
B. If the stack is full
C. If the stack has no elements
D. If the stack is initialized
Solution
Step 1: Understand the purpose of isEmpty()
The isEmpty() method checks whether the stack contains any elements or not.
Step 2: Identify the condition checked
If the stack has no elements, it is empty, so isEmpty() returns true.
Final Answer:
If the stack has no elements -> Option C
Quick Check:
Empty stack means no elements = true [OK]
Hint: Empty means zero elements in stack [OK]
Common Mistakes:
Confusing isEmpty() with isFull()
Thinking isEmpty() checks for one element
Assuming isEmpty() checks initialization
2. Which of the following is the correct syntax to check if a stack named stack is full in Java?
easy
A. stack.isEmpty();
B. stack.isFull();
C. stack.full();
D. stack.isFilled();
Solution
Step 1: Identify the standard method name for full check
In stack implementations, the method to check if the stack is full is usually named isFull().
Step 2: Match the correct syntax
The correct syntax to call this method on a stack object named stack is stack.isFull();.
Final Answer:
stack.isFull(); -> Option B
Quick Check:
Method to check full = isFull() [OK]
Hint: Full check method is isFull() [OK]
Common Mistakes:
Using isEmpty() to check full
Using non-existent methods like full()
Misspelling method names
3. What will be the output of the following Java code?
class Stack {
int maxSize = 3;
int top = -1;
int[] arr = new int[maxSize];
boolean isFull() {
return top == maxSize - 1;
}
boolean isEmpty() {
return top == -1;
}
void push(int val) {
if (!isFull()) {
arr[++top] = val;
System.out.println("Pushed " + val);
} else {
System.out.println("Stack is full");
}
}
}
public class Main {
public static void main(String[] args) {
Stack stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
}
}
medium
A. Pushed 10
Pushed 20
Pushed 30
Stack is full
B. Pushed 10
Pushed 20
Pushed 30
Pushed 40
C. Stack is full
Stack is full
Stack is full
Stack is full
D. Pushed 10
Pushed 20
Stack is full
Stack is full
Solution
Step 1: Trace push operations and stack state
Initially, top = -1. Each push increments top and adds value if stack not full. Max size is 3, so max top index is 2.
Step 2: Check each push call
Push 10: top becomes 0, prints "Pushed 10". Push 20: top becomes 1, prints "Pushed 20". Push 30: top becomes 2, prints "Pushed 30". Push 40: top is 2 (full), so prints "Stack is full".
Final Answer:
Pushed 10
Pushed 20
Pushed 30
Stack is full -> Option A
Quick Check:
Stack max size 3, 4th push fails [OK]
Hint: Stack full when top == maxSize-1 [OK]
Common Mistakes:
Assuming push always succeeds
Ignoring top index limits
Confusing isEmpty() with isFull()
4. Find the error in this stack implementation snippet that checks if the stack is empty:
public boolean isEmpty() {
if (top == 0) {
return true;
} else {
return false;
}
}
medium
A. No error, the code is correct
B. The method should return false when top == 0
C. The method should check if top == maxSize
D. The condition should be top == -1 to check empty
Solution
Step 1: Understand stack empty condition
In stack, empty means no elements pushed, so top is usually -1.
Step 2: Identify incorrect condition
The code checks top == 0, which means one element is present, so it is wrong for empty check.
Final Answer:
The condition should be top == -1 to check empty -> Option D
Quick Check:
Empty stack top = -1 [OK]
Hint: Empty stack top is -1, not 0 [OK]
Common Mistakes:
Checking top == 0 for empty
Confusing full and empty conditions
Returning wrong boolean values
5. You have a stack with max size 5 and current top = 3. Which of the following statements is true about the stack's state?
hard
A. The stack is neither full nor empty
B. The stack is empty
C. The stack is full
D. The stack has exactly 5 elements
Solution
Step 1: Understand stack indexing and size
Stack max size is 5, so valid top indices are 0 to 4. top = 3 means 4 elements are present (indices 0,1,2,3).
Step 2: Determine if stack is full or empty
Full means top == maxSize - 1 which is 4 here, so not full. Empty means top == -1, so not empty. Therefore, stack is neither full nor empty.
Final Answer:
The stack is neither full nor empty -> Option A
Quick Check:
top=3, maxSize=5 means partial fill [OK]
Hint: Full if top = maxSize-1, empty if top = -1 [OK]
Common Mistakes:
Assuming top=3 means full
Confusing top index with element count
Thinking top=3 means empty
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.