Stack in data structure: 47 MCQs with answers

All 47 multiple-choice questions from this kit, free and with no sign-up.

Warm-up · 23 questions

Q1. What does LIFO stand for?

Beginner
A First In, First Out B Last In, Last Out C First In, Last Out D Last In, First Out
Show 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?

Beginner
A Pop B Push C Peek D isEmpty
Show 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?

Beginner
A O(log n) B O(n^2) C O(n) D O(1)
Show 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?

Beginner
A Checking balanced parentheses in code B Undo/Redo in text editors C Scheduling processes in an operating system (round-robin) D Managing function calls in recursion
Show 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?

Beginner
A Null value is returned B Stack underflow C The stack creates a new element D Stack overflow
Show 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?

Beginner
A It cannot be implemented in C B It does not support peek C It has a fixed capacity D Push and pop are O(n)
Show 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?

Beginner
A Stack underflow B Stack overflow C Null pointer exception D Segmentation fault
Show 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?

Beginner
A Queue B Array C Linked list D Stack
Show 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.

Beginner
A True B False
Show 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.

Beginner
A True B False
Show 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.

Beginner
A True B False
Show 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.

Beginner
A True B False
Show 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.

Beginner
Show 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.

Beginner
Show answer & explanation

—

The answer must list push, pop, and peek with correct descriptions.

Q15. What is a stack overflow? Give a common cause.

Beginner
Show answer & explanation

—

The answer should define stack overflow and mention infinite recursion as a cause.

Q16. What is a stack underflow?

Beginner
Show 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?

Beginner
Show 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?

Beginner
Show answer & explanation

—

The answer should state that only the top is accessible.

Q19. What is the time complexity of the push operation in a stack implemented with a linked list?

Beginner
A O(log n) B O(n^2) C O(n) D O(1)
Show answer & explanation

O(1)

Push in a linked-list-based stack is O(1) because we only update the head pointer.

Q20. What does LIFO stand for?

Beginner
A First In, First Out B Last In, Last Out C First In, Last Out D Last In, First Out
Show answer & explanation

Last In, First Out

LIFO stands for Last In, First Out, which is the defining principle of a stack.

Q21. The call stack is used to manage function calls and local variables in programming languages.

Beginner
A True B False
Show answer & explanation

True

The call stack stores stack frames for each function call, including local variables and return addresses.

Q22. Which data structure would you use to reverse a string?

Beginner
A Queue B Array C Linked list D Stack
Show answer & explanation

Stack

A stack reverses order because of LIFO. Pushing characters and then popping them yields the reversed string.

Q23. A stack overflow can be caused by an infinite loop that does not involve function calls.

Beginner
A True B False
Show 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.

Building up · 16 questions

Q24. 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?

Intermediate
A `push` is $O(1)$ and `pop` is $O(n)$ because you need to update the head pointer after removal. B Both `push` and `pop` are $O(1)$ because you only ever operate at the head of the list. C Both `push` and `pop` are $O(n)$ because you must traverse the list to find the tail. D `push` is $O(n)$ and `pop` is $O(1)$ because inserting at the head requires shifting all elements.
Show 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)$.

Q25. 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?

Intermediate
A $[5, 10, 20]$ B $[5, 20]$ C $[5, 10]$ D $[5]$
Show 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…

Q26. 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 `(`)?

Intermediate
A Stack contains: `(` B Stack contains: `{`, `[`, `(` C Stack contains: `{`, `[` D Stack contains: `[`, `(`
Show 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 `{`, `[`, `(`.

Q27. In an array-based stack implementation with a fixed capacity, which of the following scenarios would **directly** cause a **stack overflow**?

Intermediate
A Calling `push()` when the stack already contains the maximum number of elements. B Calling `isEmpty()` on a stack that is full. C Calling `peek()` on a stack that has only one element. D Calling `pop()` on an empty stack.
Show 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.

Q28. 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?

Intermediate
A Back stack contains: [A] B Back stack contains: [A, B, C] C Back stack contains: [A, B] D Back stack contains: [C]
Show 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…

Q29. What is the time complexity of push, pop, and peek in a well-implemented stack? Why?

Intermediate
Show answer & explanation

—

The answer must state O(1) and explain that only the top is accessed.

Q30. Compare and contrast array-based and linked-list-based stack implementations.

Intermediate
Show answer & explanation

—

The answer should mention fixed vs. dynamic size and memory overhead.

Q31. How does the call stack support recursion?

Intermediate
Show answer & explanation

—

The answer should describe pushing frames for each call and popping them on return.

Q32. Describe how a stack can be used to check balanced parentheses in an expression.

Intermediate
Show answer & explanation

—

The answer should outline the algorithm: push open, pop on matching close, check emptiness at end.

Q33. How do web browsers use stacks for back and forward navigation?

Intermediate
Show answer & explanation

—

The answer should mention two stacks: back and forward.

Q34. Explain how undo functionality is typically implemented using a stack.

Intermediate
Show answer & explanation

—

The answer should describe pushing actions and popping for undo.

Q35. What is a stack frame?

Intermediate
Show answer & explanation

—

The answer should define a stack frame and its contents.

Q36. How can a stack be used to reverse a string?

Intermediate
Show answer & explanation

—

The answer should describe pushing characters and popping them to reverse.

Q37. 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?

Intermediate
A $[5, 10, 20]$ B $[5, 20]$ C $[5, 10]$ D $[5]$
Show 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…

Q38. 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?

Intermediate
A `push` is $O(1)$ and `pop` is $O(n)$ because you need to update the head pointer after removal. B Both `push` and `pop` are $O(1)$ because you only ever operate at the head of the list. C Both `push` and `pop` are $O(n)$ because you must traverse the list to find the tail. D `push` is $O(n)$ and `pop` is $O(1)$ because inserting at the head requires shifting all elements.
Show 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)$.

Q39. 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 `(`)?

Intermediate
A Stack contains: `(` B Stack contains: `{`, `[`, `(` C Stack contains: `{`, `[` D Stack contains: `[`, `(`
Show 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 `{`, `[`, `(`.

Topper level · 8 questions

Q40. 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?

Advanced
A Stack contains [1, 2, 3, 4, 5] from bottom to top, and a stack overflow occurs during push(6). B Stack contains [1, 2, 4, 5, 6] from bottom to top, and no overflow occurs. C Stack contains [1, 2, 4, 5] from bottom to top, and a stack overflow occurs during push(6). D Stack contains [1, 2, 4, 5] from bottom to top, and no overflow occurs.
Show answer & explanation

Stack contains [1, 2, 4, 5] from bottom to top, and a stack overflow occurs during push(6).

Q41. 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?

Advanced
A Both `push` and `pop` are $O(1)$ because we only operate at the head. B Both `push` and `pop` are $O(n)$ because linked list operations always require traversal. C `push` is $O(n)$ because we must traverse to the end to add, and `pop` is $O(1)$. D `push` is $O(1)$ but `pop` is $O(n)$ because we need to traverse to find the new top.
Show 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.

Q42. 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 `(`)?

Advanced
A Stack contains: `(` only. B Stack contains: `{`, `[`, `(` (from bottom to top). C Stack contains: `(`, `[`, `{` (from bottom to top). D Stack contains: `{`, `[` (from bottom to top).
Show 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 `{`, `[`, `(`.

Q43. 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?

Advanced
A Back stack contains A and B (B on top). B Back stack contains A, B, and C (C on top). C Back stack contains A only. D Back stack is empty.
Show 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=[…

Q44. 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?

Advanced
A [5] B [5, 10, 20] C [5, 10] D [5, 20]
Show answer & explanation

[5, 20]

Q45. What is the role of a stack in evaluating arithmetic expressions?

Advanced
Show answer & explanation

—

The answer should mention conversion and evaluation using stacks.

Q46. 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 `(`)?

Advanced
A Stack contains: `(` only. B Stack contains: `{`, `[`, `(` (from bottom to top). C Stack contains: `(`, `[`, `{` (from bottom to top). D Stack contains: `{`, `[` (from bottom to top).
Show 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 `{`, `[`, `(`.

Q47. 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?

Advanced
A Both `push` and `pop` are $O(1)$ because we only operate at the head. B Both `push` and `pop` are $O(n)$ because linked list operations always require traversal. C `push` is $O(n)$ because we must traverse to the end to add, and `pop` is $O(1)$. D `push` is $O(1)$ but `pop` is $O(n)$ because we need to traverse to find the new top.
Show 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.

Make your own

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