Stack Basics: 22 MCQs with answers
All 22 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 13 questions
Q1. What is the fundamental principle followed by a stack data structure?
BeginnerShow answer & explanation
Last In, First Out (LIFO)
A stack follows LIFO: the last element pushed is the first to be popped.
Q2. In an array-based stack, which end is used as the top of the stack?
BeginnerShow answer & explanation
The last used index (top)
The top pointer indicates the index of the most recently added element; push/pop operate at that end.
Q3. What does the 'top' variable in an array-based stack represent?
BeginnerShow answer & explanation
The index of the last element pushed
The top variable stores the index of the most recent element; it is updated on push/pop.
Q4. What is the time complexity of the push operation in an array-based stack (without resizing)?
BeginnerShow answer & explanation
O(1)
Push only updates the top index and places the element, so it takes constant time.
Q5. What condition indicates that an array-based stack is empty?
BeginnerShow answer & explanation
top == -1
Initially top is set to -1; when no elements are present, top remains -1.
Q6. In an array-based stack, what does the pop operation do?
BeginnerShow answer & explanation
Removes the element at the top and decrements top
Pop removes the top element and decrements the top index; the element is logically removed.
Q7. Which operation returns the top element without removing it?
BeginnerShow answer & explanation
peek (or top)
Peek (or top) retrieves the top element while leaving the stack unchanged.
Q8. What is the time complexity of the peek operation in an array-based stack?
BeginnerShow answer & explanation
O(1)
Peek directly accesses the element at the top index, so it is constant time.
Q9. In an array-based stack, what is the initial value of the top pointer?
BeginnerShow answer & explanation
-1
Top is initialized to -1 to indicate an empty stack; the first push increments it to 0.
Q10. In an array-based stack, the pop operation removes the element at the top of the stack.
BeginnerShow answer & explanation
True
Pop always removes the most recently added element, which is at the top.
Q11. The peek operation in a stack removes the top element from the stack.
BeginnerShow answer & explanation
False
Peek only returns the top element without modifying the stack; it does not remove it.
Q12. In an array-based stack, what is the sequence of steps to perform a push operation? Include the overflow check.
BeginnerShow answer & explanation
—
This tests the basic understanding of the push operation's order: checking capacity before modifying the top index and placing the element.
Q13. Describe a real-world application of a stack and explain how the LIFO principle is used in that application.
BeginnerShow answer & explanation
—
This tests the ability to connect the abstract concept of a stack to a practical use case.
Building up · 8 questions
Q14. What happens if you try to push onto a full array-based stack?
IntermediateShow answer & explanation
An overflow condition occurs
Without dynamic resizing, pushing to a full stack causes overflow; this is a limitation of fixed-size arrays.
Q15. Which of the following is a common limitation of a fixed-size array-based stack?
IntermediateShow answer & explanation
It cannot handle dynamic resizing
A fixed-size array cannot grow, so it may overflow; dynamic resizing is needed to overcome this.
Q16. What is the result of the expression: push(5), push(10), pop(), peek()?
IntermediateShow answer & explanation
5
After pushing 5 and 10, pop removes 10, leaving 5 at the top, so peek returns 5.
Q17. Which of the following applications uses a stack?
IntermediateShow answer & explanation
Undo operation in text editors
Undo operations use a stack to reverse actions in LIFO order; CPU scheduling typically uses queues.
Q18. What is the main advantage of an array-based stack over a linked-list stack?
IntermediateShow answer & explanation
Better cache locality and faster access
Arrays provide contiguous memory, leading to better cache performance; linked lists use extra memory for pointers.
Q19. Given an array-based stack with capacity 5, initially empty. Perform the following operations and show the final state of the stack (elements from bottom to top) and the value of `top`: push(3), push(7), pop(), push(9), push(2), pop(), peek().
IntermediateShow answer & explanation
—
This requires applying the operations step-by-step, tracking the top index and the array contents, and understanding that peek does not modify the stack.
Q20. Explain why the time complexity of all stack operations (push, pop, peek, isEmpty) is O(1) in an array-based implementation.
IntermediateShow answer & explanation
—
This checks the understanding of constant-time operations and why array-based stacks are efficient.
Q21. What are the consequences of not checking for stack overflow before a push and stack underflow before a pop in an array-based stack? Provide a specific example of each.
IntermediateShow answer & explanation
—
This emphasizes the importance of boundary checks and the typical errors that occur when they are omitted.
Topper level · 1 questions
Q22. Analyze the trade-offs between an array-based stack and a linked-list stack in terms of memory usage, performance, and flexibility. Under what conditions would you prefer the array-based version?
AdvancedShow answer & explanation
—
This requires a deeper understanding of the trade-offs and the ability to justify the choice based on constraints.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages