stack data structure: 29 MCQs with answers
All 29 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 19 questions
Q1. Which principle does a stack follow?
BeginnerShow answer & explanation
Last In, First Out (LIFO)
A stack is a LIFO data structure, meaning the last element added is the first to be removed.
Q2. Which operation adds an element to the top of a stack?
BeginnerShow answer & explanation
push
The push operation inserts an element onto the top of the stack.
Q3. Which operation removes the top element from a stack?
BeginnerShow answer & explanation
pop
The pop operation removes and returns the top element of the stack.
Q4. What does the 'peek' operation do on a stack?
BeginnerShow answer & explanation
Returns the top element without removing it
Peek (or top) returns the top element without modifying the stack.
Q5. If you push elements 1, 2, 3 onto a stack in that order, what will be the first element popped?
BeginnerShow answer & explanation
3
Due to LIFO, the last pushed element (3) is popped first.
Q6. Which of the following is NOT a typical stack operation?
BeginnerShow answer & explanation
enqueue
Enqueue is a queue operation, not a stack operation.
Q7. Which of the following is a real-world analogy for a stack?
BeginnerShow answer & explanation
A stack of plates
A stack of plates is a classic analogy: you add and remove plates from the top.
Q8. A stack is a FIFO data structure.
BeginnerShow answer & explanation
False
A stack is LIFO (Last In, First Out), not FIFO.
Q9. The 'peek' operation removes the top element from the stack.
BeginnerShow answer & explanation
False
Peek returns the top element without removing it; pop removes it.
Q10. What does LIFO stand for in the context of a stack?
BeginnerShow answer & explanation
Last In, First Out
LIFO stands for Last In, First Out, which describes how the most recently added item is the first to be removed.
Q11. Which operation adds an item to the top of the stack?
BeginnerShow answer & explanation
push(item)
The `push(item)` operation is used to add an item to the top of the stack.
Q12. What is the result of calling `pop()` on a stack that is empty?
BeginnerShow answer & explanation
Throws an exception
Calling `pop()` on an empty stack typically results in an error condition known as underflow, which may throw an exception.
Q13. Which of the following is a common application of stacks?
BeginnerShow answer & explanation
Checking balanced parentheses
Stacks are commonly used to check balanced parentheses, as they follow the LIFO principle.
Q14. What does the `peek()` operation do?
BeginnerShow answer & explanation
Returns the top item without removing it
The `peek()` operation allows you to view the top item of the stack without removing it.
Q15. How does a stack handle function calls in programming?
BeginnerShow answer & explanation
By pushing the current state onto a call stack
A stack manages function calls by pushing the current state onto a call stack, allowing for proper return and recursion handling.
Q16. Which of the following best describes a stack's structure?
BeginnerShow answer & explanation
A linear data structure with a single open end
A stack is a linear data structure that has a single open end called the top, where all operations occur.
Q17. What is the time complexity for the push operation in a stack?
BeginnerShow answer & explanation
O(1)
The time complexity for the `push` operation in a stack is $O(1)$, meaning it takes constant time regardless of the number of elements.
Q18. Which operation checks if a stack has no elements?
BeginnerShow answer & explanation
isEmpty()
The `isEmpty()` operation checks whether the stack has no elements.
Q19. What is a real-world analogy for a stack?
BeginnerShow answer & explanation
A stack of plates
A stack can be likened to a stack of plates, where you add and remove plates from the top, illustrating the LIFO principle.
Building up · 9 questions
Q20. Which of the following is a common application of a stack?
IntermediateShow answer & explanation
Undo/Redo in text editors
Stacks are used to implement undo/redo features, as they naturally store actions in LIFO order.
Q21. Which data structure is used to implement function calls in most programming languages?
IntermediateShow answer & explanation
Stack
The call stack is a stack data structure that manages function calls and returns.
Q22. What is the time complexity of push and pop operations on a stack implemented with an array?
IntermediateShow answer & explanation
O(1)
Array-based stack operations (push/pop) operate at the end of the array, which is O(1) on average.
Q23. In the expression '2 3 +', which notation is used?
IntermediateShow answer & explanation
Postfix
Postfix notation places operators after operands; stacks are used to evaluate postfix expressions.
Q24. What is the result of evaluating the postfix expression '5 3 + 2 *'?
IntermediateShow answer & explanation
16
Push 5, push 3, pop 5 and 3, add to get 8, push 8, push 2, pop 8 and 2, multiply to get 16.
Q25. Which of the following problems is commonly solved using a stack?
IntermediateShow answer & explanation
Balanced parentheses checking
Stacks are used to check balanced parentheses by pushing opening brackets and popping on closing brackets.
Q26. What happens if you pop from an empty stack?
IntermediateShow answer & explanation
It throws an underflow error
Popping from an empty stack is an underflow condition, typically resulting in an error or exception.
Q27. What is the space complexity of a stack with n elements?
IntermediateShow answer & explanation
O(n)
A stack storing n elements requires O(n) space.
Q28. Which of the following is NOT a typical use of a stack?
IntermediateShow answer & explanation
Breadth-first search
Breadth-first search uses a queue, not a stack.
Topper level · 1 questions
Q29. Which of the following is a common application of stacks in compilers?
AdvancedShow answer & explanation
Syntax analysis
Stacks are used in syntax analysis (parsing) to track nested structures and operators.
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