Stack Data Structure in C Programming
Stack Practice Questions
1. What is the time complexity of push operation in stack?
A) O(n)
B) O(log n)
C) O(1)
D) O(n log n)
Answer: C) O(1)
Push operation in stack has constant time complexity as it only involves adding an element at the top.
2. Which principle does stack follow?
A) FIFO (First In First Out)
B) LIFO (Last In First Out)
C) FILO (First In Last Out)
D) LILO (Last In Last Out)
Answer: B) LIFO (Last In First Out)
Stack follows LIFO principle where the last element added is the first one to be removed.
3. What happens when you try to pop from an empty stack?
A) Segmentation fault
B) Stack overflow
C) Stack underflow
D) Returns 0
Answer: C) Stack underflow
Attempting to pop from an empty stack results in stack underflow condition.
4. Which of these is NOT a valid stack operation?
A) Push
B) Pop
C) Peek
D) Enqueue
Answer: D) Enqueue
Enqueue is an operation of queue data structure, not stack.
5. What is the maximum number of stacks needed to implement a queue?
A) 1
B) 2
C) 3
D) 4
Answer: B) 2
A queue can be implemented using two stacks - one for enqueue operations and another for dequeue operations.
6. Which of the following applications typically uses stack?
A) CPU scheduling
B) Function call management
C) Printer queue
D) All of the above
Answer: B) Function call management
Stacks are used in function call management to keep track of return addresses and local variables.
7. What is the result of the following postfix expression: 5 3 + 8 2 - * ?
A) 48
B) 64
C) 32
D) 16
Answer: A) 48
Evaluation: (5+3)*(8-2) = 8*6 = 48. Postfix expressions are evaluated using stacks.
8. Which data structure is used to implement recursion?
A) Queue
B) Stack
C) Array
D) Tree
Answer: B) Stack
The system stack is used to manage function calls and returns in recursion.
9. What is the minimum number of stacks needed to implement a stack with getMin() operation in O(1) time?
A) 1
B) 2
C) 3
D) 4
Answer: B) 2
You need an auxiliary stack to keep track of minimum elements to achieve O(1) time for getMin().
10. Which of the following is true about stack implementation using linked list?
A) Push operation is always O(n)
B) Pop operation is always O(n)
C) Both push and pop are O(1)
D) Stack size must be declared in advance
Answer: C) Both push and pop are O(1)
In linked list implementation, both push and pop operations can be done in constant time by adding/removing from the head.
11. What would be the stack configuration after these operations: push(5), push(3), pop(), push(7), pop(), push(9)?
A) [5, 7, 9]
B) [5, 9]
C) [9, 5]
D) [5, 3, 9]
Answer: B) [5, 9]
Operation sequence: push(5)→[5], push(3)→[5,3], pop()→[5], push(7)→[5,7], pop()→[5], push(9)→[5,9]
12. Which algorithm uses stack for balanced parenthesis checking?
A) Dijkstra's algorithm
B) Shunting-yard algorithm
C) Tower of Hanoi
D) All of the above
Answer: B) Shunting-yard algorithm
The shunting-yard algorithm uses stack to check for balanced parentheses while converting infix to postfix notation.
Related Data Structure Resources