A stack is empty when it has no elements inside it. This means there is nothing to pop or peek.
Click to reveal answer
beginner
How can you check if a stack is full in a fixed-size stack?
A stack is full when the number of elements reaches its maximum capacity. You check if the top pointer equals the maximum size minus one.
Click to reveal answer
beginner
Why is it important to check if a stack is empty before popping?
Checking if a stack is empty before popping prevents errors because you cannot remove an element from an empty stack.
Click to reveal answer
beginner
What is the typical way to represent the 'top' of a stack in code?
The 'top' is usually an index or pointer that shows the position of the last added element in the stack.
Click to reveal answer
beginner
In a stack implemented with a list and a max size, what condition checks if the stack is full?
If the length of the list equals the max size, the stack is full.
Click to reveal answer
What does the condition 'top == -1' usually mean in a stack?
AThe stack is full
BThe stack is corrupted
CThe stack has one element
DThe stack is empty
✗ Incorrect
When top is -1, it means no elements have been added yet, so the stack is empty.
If a stack has max size 5, which condition means it is full?
Atop == 0
Btop == 4
Ctop == 5
Dtop == -1
✗ Incorrect
Since indexing starts at 0, top == 4 means 5 elements are in the stack, so it is full.
Why should you check if a stack is empty before popping?
ATo avoid removing from an empty stack
BTo avoid adding elements
CTo increase stack size
DTo reset the stack
✗ Incorrect
Popping from an empty stack causes errors, so checking prevents that.
Which of these is NOT a sign that a stack is full?
Atop == -1
Blength of stack list == max_size
CNo more space to push
Dtop == max_size - 1
✗ Incorrect
top == -1 means the stack is empty, not full.
What is the initial value of 'top' in an empty stack?
Amax_size
B0
C-1
D1
✗ Incorrect
top starts at -1 to indicate no elements are present.
Explain how to check if a stack is empty or full in a fixed-size stack.
Think about what the 'top' index means when the stack has no elements or is at capacity.
You got /4 concepts.
Why is it important to check stack state before push or pop operations?
Consider what happens if you try to remove from empty or add to full stack.
You got /4 concepts.
Practice
(1/5)
1. What does the isEmpty() function check in a stack?
easy
A. If the stack has exactly one element
B. If the stack is full
C. If the stack has more than one element
D. If the stack has no elements
Solution
Step 1: Understand the purpose of isEmpty()
The isEmpty() function checks whether the stack contains any elements or not.
Step 2: Identify what empty means for a stack
An empty stack means it has zero elements, so isEmpty() returns true if no elements are present.
Final Answer:
If the stack has no elements -> Option D
Quick Check:
Empty stack means no elements = true [OK]
Hint: Empty stack means zero elements inside [OK]
Common Mistakes:
Confusing empty with full
Thinking it checks for one element
Assuming it checks for capacity
2. Which of the following is the correct syntax to check if a stack named stack is full in Python assuming max size is stored in max_size and current size in top?
easy
A. if top == max_size: print('Stack is full')
B. if top > max_size: print('Stack is full')
C. if top < max_size: print('Stack is full')
D. if top != max_size: print('Stack is full')
Solution
Step 1: Understand stack full condition
A stack is full when the current top index equals the maximum allowed size.
Step 2: Match condition with syntax
The correct check is top == max_size to confirm fullness.
Final Answer:
if top == max_size: print('Stack is full') -> Option A
Two elements (10, 20) are added, so length is 2 which equals max_size.
Step 2: Evaluate isFull and isEmpty
isFull() returns True because length equals max_size; isEmpty() returns False because stack is not empty.
Final Answer:
True
False -> Option C
Quick Check:
Stack full = True, empty = False [OK]
Hint: Full if length equals max size; empty if length zero [OK]
Common Mistakes:
Confusing isFull and isEmpty return values
Ignoring stack length after append
Assuming stack is empty after append
4. Find the error in the following code that checks if a stack is empty:
class Stack:
def __init__(self):
self.stack = []
def isEmpty(self):
if self.stack == 0:
return True
else:
return False
s = Stack()
print(s.isEmpty())
medium
A. Missing return statement in isEmpty
B. Comparing list to integer zero causes error
C. Stack initialization is incorrect
D. Print statement syntax is wrong
Solution
Step 1: Analyze the isEmpty condition
The code compares self.stack == 0, but self.stack is a list, not a number.
Step 2: Identify correct way to check empty list
To check if list is empty, use len(self.stack) == 0 or simply not self.stack.
Final Answer:
Comparing list to integer zero causes error -> Option B
Quick Check:
List cannot be compared to number 0 [OK]
Hint: Check list emptiness with length or not operator [OK]
Common Mistakes:
Comparing list directly to 0
Forgetting list length check
Using wrong comparison operator
5. You have a stack with max size 3. After pushing 3 elements, you pop one element and then check if the stack is full or empty. What will be the correct output of isFull() and isEmpty() methods?
class Stack:
def __init__(self, max_size):
self.stack = []
self.max_size = max_size
def push(self, val):
if len(self.stack) < self.max_size:
self.stack.append(val)
def pop(self):
if not self.isEmpty():
return self.stack.pop()
def isFull(self):
return len(self.stack) == self.max_size
def isEmpty(self):
return len(self.stack) == 0
s = Stack(3)
s.push(1)
s.push(2)
s.push(3)
s.pop()
print(s.isFull())
print(s.isEmpty())
hard
A. False\nFalse
B. True\nFalse
C. False\nTrue
D. True\nTrue
Solution
Step 1: Track stack size after operations
Initially push 3 elements, stack size = 3 (full). After pop, size reduces to 2.
Step 2: Evaluate isFull and isEmpty
Since size is 2 and max_size is 3, isFull() returns False. Stack is not empty, so isEmpty() returns False.
Final Answer:
False
False -> Option A
Quick Check:
Stack size 2 < max size 3, not empty [OK]
Hint: Pop reduces size; full only if size equals max [OK]
Common Mistakes:
Assuming stack is still full after pop
Assuming stack is empty after pop
Ignoring size changes after pop
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.