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?

Beginner
A Last In, First Out (LIFO) B Priority Order C Random Access D First In, First Out (FIFO)
Show 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?

Beginner
A peek B pop C push D enqueue
Show 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?

Beginner
A pop B peek C dequeue D push
Show 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?

Beginner
A Adds an element to the bottom B Checks if the stack is empty C Returns the top element without removing it D Removes the top element
Show 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?

Beginner
A None B 3 C 1 D 2
Show 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?

Beginner
A push B pop C peek D enqueue
Show 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?

Beginner
A A playlist in shuffle mode B A stack of plates C A round-robin tournament D A line of people waiting for a bus
Show 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.

Beginner
A True B False
Show answer & explanation

False

A stack is LIFO (Last In, First Out), not FIFO.

Q9. The 'peek' operation removes the top element from the stack.

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

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

Beginner
A push(item) B pop() C peek() D isEmpty()
Show 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?

Beginner
A Removes the last element B Returns the top element C Returns null D Throws an exception
Show 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?

Beginner
A Checking balanced parentheses B Storing data in a table C Sorting a list D Searching for an element
Show 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?

Beginner
A Adds an item to the stack B Checks if the stack is empty C Removes the top item D Returns the top item without removing it
Show 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?

Beginner
A By executing them in random order B By storing all function calls in an array C By pushing the current state onto a call stack D By using a queue
Show 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?

Beginner
A A linear data structure with a single open end B A circular data structure with multiple open ends C A hierarchical data structure with nodes D A random access data structure
Show 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?

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

Beginner
A isEmpty() B pop() C push(item) D size()
Show answer & explanation

isEmpty()

The `isEmpty()` operation checks whether the stack has no elements.

Q19. What is a real-world analogy for a stack?

Beginner
A A pile of laundry B A bookshelf C A line of people waiting D A stack of plates
Show 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?

Intermediate
A Round-robin scheduling B Undo/Redo in text editors C Breadth-first search D CPU scheduling
Show 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?

Intermediate
A Linked list B Stack C Tree D Queue
Show 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?

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

Intermediate
A Reverse Polish B Postfix C Prefix D Infix
Show 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 *'?

Intermediate
A 10 B 16 C 13 D 8
Show 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?

Intermediate
A Balanced parentheses checking B Finding the shortest path C Hashing D Sorting a list
Show 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?

Intermediate
A It throws an underflow error B It returns 0 C It returns null D It creates a new element
Show 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?

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

Intermediate
A Function call management B Depth-first search C Breadth-first search D Expression evaluation
Show 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?

Advanced
A Code optimization B Memory allocation C Garbage collection D Syntax analysis
Show answer & explanation

Syntax analysis

Stacks are used in syntax analysis (parsing) to track nested structures and operators.

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