stack in data structure: 37 MCQs with answers
All 37 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 18 questions
Q1. What does LIFO stand for?
BeginnerShow answer & explanation
Last In, First Out
LIFO stands for Last In, First Out, which is the defining principle of a stack.
Q2. Which operation removes the top element from a stack?
BeginnerShow answer & explanation
Pop
Pop removes and returns the top element. Push adds, peek views, and isEmpty checks if the stack is empty.
Q3. What is the time complexity of the push operation in a stack implemented with a linked list?
BeginnerShow answer & explanation
O(1)
Push in a linked-list-based stack is O(1) because we only update the head pointer.
Q4. Which of the following is NOT a typical application of a stack?
BeginnerShow answer & explanation
Scheduling processes in an operating system (round-robin)
Round-robin scheduling uses a queue (FIFO), not a stack. The other options are classic stack applications.
Q5. What happens when you try to pop from an empty stack?
BeginnerShow answer & explanation
Stack underflow
A stack underflow occurs when you attempt to pop from an empty stack.
Q6. In an array-based stack, what is a common limitation?
BeginnerShow answer & explanation
It has a fixed capacity
Array-based stacks have a fixed size, which can lead to stack overflow if exceeded. Linked-list implementations are dynamic.
Q7. A developer writes a recursive function without a base case. What is the most likely runtime error?
BeginnerShow answer & explanation
Stack overflow
Infinite recursion causes the call stack to grow until it exceeds memory, resulting in a stack overflow error.
Q8. Which data structure would you use to reverse a string?
BeginnerShow answer & explanation
Stack
A stack reverses order because of LIFO. Pushing characters and then popping them yields the reversed string.
Q9. A stack allows direct access to any element in the middle of the structure.
BeginnerShow answer & explanation
False
A stack only allows access to the top element. To reach a middle element, you must pop all elements above it.
Q10. The call stack is used to manage function calls and local variables in programming languages.
BeginnerShow answer & explanation
True
The call stack stores stack frames for each function call, including local variables and return addresses.
Q11. A queue follows the LIFO principle, just like a stack.
BeginnerShow answer & explanation
False
A queue follows FIFO (First In, First Out), while a stack follows LIFO.
Q12. A stack overflow can be caused by an infinite loop that does not involve function calls.
BeginnerShow answer & explanation
False
A stack overflow is specifically related to the call stack running out of memory, typically from infinite recursion or deeply nested calls, not from a simple infinite loop.
Q13. Explain the LIFO principle of a stack using a real-world analogy.
BeginnerShow answer & explanation
—
The answer should mention LIFO and give a clear analogy like a stack of plates or a pile of books.
Q14. What are the three primary operations of a stack? Briefly describe each.
BeginnerShow answer & explanation
—
The answer must list push, pop, and peek with correct descriptions.
Q15. What is a stack overflow? Give a common cause.
BeginnerShow answer & explanation
—
The answer should define stack overflow and mention infinite recursion as a cause.
Q16. What is a stack underflow?
BeginnerShow answer & explanation
—
The answer should clearly state that it happens when popping from an empty stack.
Q17. What is the difference between a stack and a queue?
BeginnerShow answer & explanation
—
The answer should contrast LIFO vs. FIFO with analogies.
Q18. Why can't you directly access an element in the middle of a stack?
BeginnerShow answer & explanation
—
The answer should state that only the top is accessible.
Building up · 13 questions
Q19. A developer implements a stack using a singly linked list where the **top** of the stack is the head of the list. Which of the following best explains the time complexity of the `push` and `pop` operations in this implementation?
IntermediateShow answer & explanation
Both `push` and `pop` are $O(1)$ because you only ever operate at the head of the list.
When the top of the stack is the head of the linked list, both `push` (insert at head) and `pop` (remove head) involve only updating the head pointer and do not require traversal. Therefore, both operations are $O(1)$.
Q20. Consider the following sequence of stack operations on an initially empty stack: ``` push(5) push(10) push(15) pop() push(20) peek() pop() ``` What is the final content of the stack (from bottom to top) after all operations?
IntermediateShow answer & explanation
$[5]$
After `push(5)`, `push(10)`, `push(15)`, the stack is $[5, 10, 15]$ (top=15). `pop()` removes 15, leaving $[5, 10]$. `push(20)` adds 20 on top, giving $[5, 10, 20]$. `peek()` returns 20 but does not remove it. The final `pop()` removes 20, leaving $[5, 10]$. Wait — the correct final stack is $[5, 10]$, not $[5]$. Let m…
Q21. A compiler uses a stack to check whether parentheses in an expression are balanced. For the expression `{[()]}`, the stack is initially empty. Which of the following correctly describes the state of the stack **immediately after** processing the third character (which is `(`)?
IntermediateShow answer & explanation
Stack contains: `{`, `[`, `(`
The algorithm pushes every opening bracket onto the stack. The first character is `{` → push. The second is `[` → push. The third is `(` → push. So after processing the third character, the stack (from bottom to top) is `{`, `[`, `(`.
Q22. In an array-based stack implementation with a fixed capacity, which of the following scenarios would **directly** cause a **stack overflow**?
IntermediateShow answer & explanation
Calling `push()` when the stack already contains the maximum number of elements.
A stack overflow occurs when a `push` operation is attempted on a stack that has reached its maximum capacity (in a fixed-size array implementation). The other options describe either a stack underflow (`pop` on empty) or normal operations.
Q23. A web browser uses two stacks (back stack and forward stack) for navigation. A user starts at page A, then visits B, then C, then clicks Back twice, then clicks Forward once. Which of the following best describes the contents of the **back stack** after these actions?
IntermediateShow answer & explanation
Back stack contains: [A]
Initial: back=[], forward=[], current=A. Visit B: push A onto back, current=B. Visit C: push B onto back, current=C. Back: pop back (B), push C onto forward, current=B. Back: pop back (A), push B onto forward, current=A. Forward: pop forward (B), push A onto back, current=B. After this, back stack contains [A] (only A…
Q24. What is the time complexity of push, pop, and peek in a well-implemented stack? Why?
IntermediateShow answer & explanation
—
The answer must state O(1) and explain that only the top is accessed.
Q25. Compare and contrast array-based and linked-list-based stack implementations.
IntermediateShow answer & explanation
—
The answer should mention fixed vs. dynamic size and memory overhead.
Q26. How does the call stack support recursion?
IntermediateShow answer & explanation
—
The answer should describe pushing frames for each call and popping them on return.
Q27. Describe how a stack can be used to check balanced parentheses in an expression.
IntermediateShow answer & explanation
—
The answer should outline the algorithm: push open, pop on matching close, check emptiness at end.
Q28. How do web browsers use stacks for back and forward navigation?
IntermediateShow answer & explanation
—
The answer should mention two stacks: back and forward.
Q29. Explain how undo functionality is typically implemented using a stack.
IntermediateShow answer & explanation
—
The answer should describe pushing actions and popping for undo.
Q30. What is a stack frame?
IntermediateShow answer & explanation
—
The answer should define a stack frame and its contents.
Q31. How can a stack be used to reverse a string?
IntermediateShow answer & explanation
—
The answer should describe pushing characters and popping them to reverse.
Topper level · 6 questions
Q32. Consider an array-based stack with a fixed capacity of 5, initially empty. The following operations are performed in order: push(1), push(2), push(3), pop(), push(4), push(5), push(6). Which of the following best describes the state of the stack after all operations?
AdvancedShow answer & explanation
Stack contains [1, 2, 4, 5] from bottom to top, and a stack overflow occurs during push(6).
Q33. A stack is implemented using a singly linked list where the top of the stack is the head of the list. Which of the following correctly describes the time complexity of the `push` and `pop` operations in this implementation?
AdvancedShow answer & explanation
Both `push` and `pop` are $O(1)$ because we only operate at the head.
When the top of the stack is the head of the linked list, both push (insert at head) and pop (remove head) involve only updating the head pointer, which is $O(1)$. No traversal is needed.
Q34. A compiler uses a stack to check balanced parentheses in an expression. For the expression `{[()]}`, the stack is initially empty. Which of the following correctly describes the state of the stack **immediately after** processing the third character (which is `(`)?
AdvancedShow answer & explanation
Stack contains: `{`, `[`, `(` (from bottom to top).
Processing character by character: first char `{` → push `{`. Stack: [`{`]. Second char `[` → push `[`. Stack: [`{`, `[`]. Third char `(` → push `(`. Stack: [`{`, `[`, `(`]. So bottom to top is `{`, `[`, `(`.
Q35. A web browser uses two stacks (back stack and forward stack) for navigation. A user starts at page A, then visits B, then C, then clicks Back twice, then clicks Forward once. Which of the following best describes the contents of the **back stack** after these actions?
AdvancedShow answer & explanation
Back stack contains A and B (B on top).
Start: back=[], forward=[], current=A. Visit B: push A onto back, current=B → back=[A]. Visit C: push B onto back, current=C → back=[A,B]. Click Back: pop back (B), push current C onto forward, current=B → back=[A], forward=[C]. Click Back again: pop back (A), push current B onto forward, current=A → back=[], forward=[…
Q36. Consider the following sequence of stack operations on an initially empty stack: ``` push(5) push(10) push(15) pop() push(20) peek() pop() ``` What is the final content of the stack (from bottom to top) after all operations?
AdvancedShow answer & explanation
[5, 20]
Q37. What is the role of a stack in evaluating arithmetic expressions?
AdvancedShow answer & explanation
—
The answer should mention conversion and evaluation using stacks.
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