A stack is a collection where elements are added and removed in a last-in, first-out (LIFO) order. Think of it like a stack of plates where you add or remove the top plate only.
Click to reveal answer
beginner
What are the two main operations of a stack?
The two main operations are push (to add an element on top) and pop (to remove the top element).
Click to reveal answer
intermediate
How is a stack implemented using an array?
A stack using an array keeps track of the top index. Push adds an element at the next top position, and pop removes the element at the current top, then moves the top down.
Click to reveal answer
intermediate
What happens if you try to push an element when the stack is full in an array implementation?
This is called a stack overflow. Since the array has a fixed size, you cannot add more elements beyond its capacity.
Click to reveal answer
beginner
What is the time complexity of push and pop operations in a stack implemented using an array?
Both push and pop operations take constant time, O(1), because they only add or remove the top element without shifting other elements.
Click to reveal answer
Which operation removes the top element from a stack?
Apush
Benqueue
Cpeek
Dpop
✗ Incorrect
The pop operation removes the top element from the stack.
What does 'LIFO' stand for in the context of a stack?
ALast In, First Out
BLast In, Final Out
CList In, First Out
DLast Input, First Output
✗ Incorrect
LIFO means Last In, First Out, describing how the last added element is the first to be removed.
What is the initial value of the 'top' index in an empty stack implemented with an array?
A0
B-1
C1
Dnull
✗ Incorrect
The top index starts at -1 to indicate the stack is empty.
What happens when you try to pop from an empty stack?
AAdds a new element
BStack overflow error
CStack underflow error
DNothing happens
✗ Incorrect
Popping from an empty stack causes a stack underflow error.
Which of these is NOT a stack operation?
AinsertAtBottom
Bpop
Cpeek
Dpush
✗ Incorrect
insertAtBottom is not a standard stack operation.
Explain how push and pop operations work in a stack implemented using an array.
Think about how the top index changes with each operation.
You got /4 concepts.
Describe what stack overflow and stack underflow mean in the context of array-based stacks.
Consider what happens when you exceed or go below stack limits.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using an array to implement a stack?
easy
A. To allow random access to elements
B. To store elements in a first-in, first-out (FIFO) order
C. To sort elements automatically
D. To store elements in a last-in, first-out (LIFO) order
Solution
Step 1: Understand stack behavior
A stack stores elements so that the last element added is the first one removed, called LIFO.
Step 2: Role of array in stack
An array helps store elements in order and access the last added element easily for push/pop operations.
Final Answer:
To store elements in a last-in, first-out (LIFO) order -> Option D
Quick Check:
Stack = LIFO [OK]
Hint: Stack means last-in, first-out order [OK]
Common Mistakes:
Confusing stack with queue (FIFO)
Thinking array sorts elements automatically
Assuming random access is main stack feature
2. Which of the following is the correct way to initialize an empty stack using an array in Python?
easy
A. stack = []
B. stack = {}
C. stack = ()
D. stack = null
Solution
Step 1: Identify Python data types for stack
An array-like structure for stack is a list, initialized with square brackets [].
Step 2: Check each option
stack = [] uses [], which is a list. stack = {} is a dictionary, () is a tuple, null (no data).
Final Answer:
stack = [] -> Option A
Quick Check:
Empty list = empty stack [OK]
Hint: Use [] to create empty list for stack [OK]
Common Mistakes:
Using {} which creates a dictionary
Using () which creates a tuple
Assigning null instead of a list
3. What will be the output after executing the following code?
Python lists use append() to add elements, not push().
Step 2: Identify error
stack.push(5) causes AttributeError because 'push' is not a list method.
Final Answer:
'push' method does not exist for list -> Option A
Quick Check:
Use append(), not push() for list [OK]
Hint: Use append() to add elements to list [OK]
Common Mistakes:
Using push() instead of append()
Assuming push() is valid for Python lists
Ignoring error messages
5. Given a stack implemented using an array, how can you check if the stack is empty before popping an element to avoid errors?
hard
A. Check if stack is null
B. Check if stack's length is zero using len(stack) == 0
C. Try popping and catch exceptions if stack is empty
D. Use stack[0] == null to check emptiness
Solution
Step 1: Understand stack emptiness check
To avoid errors, check if stack has no elements by verifying its length is zero.
Step 2: Evaluate options
Check if stack's length is zero using len(stack) == 0 correctly checks length with len(stack) == 0. Try popping and catch exceptions if stack is empty is valid but less efficient. Check if stack is null checks null which is incorrect. Use stack[0] == null to check emptiness checks first element which may cause error if empty.
Final Answer:
Check if stack's length is zero using len(stack) == 0 -> Option B
Quick Check:
Empty stack means length zero [OK]
Hint: Use len(stack) == 0 to check empty stack [OK]
Common Mistakes:
Checking stack is null instead of empty
Accessing stack[0] without checking length
Relying only on try-except without condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.