Back to Practice Data Structures

Stacks - Practice MCQs for CCAT

50 Questions Section B: Programming Data Structures

Stacks Question Bank for C-CAT

Topic-wise Stacks MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
Stack follows which principle?
AFIFO
BPriority
CRandom
DLIFO
Show Answer & Explanation

Correct Answer: D - LIFO

Stack follows LIFO - Last In First Out principle.

Q2.
Push and Pop operations in stack take:
AO(log n)
BO(n)
CO(1)
DO(n²)
Show Answer & Explanation

Correct Answer: C - O(1)

Push and Pop are O(1) as they operate only on top of stack.

Q3.
Which application does NOT use stack?
AFunction calls
BUndo operation
CCPU scheduling
DBrowser history
Show Answer & Explanation

Correct Answer: C - CPU scheduling

CPU scheduling uses queues, not stacks.

Q4.
Infix to Postfix conversion uses:
AQueue
BLinked List
CArray only
DStack
Show Answer & Explanation

Correct Answer: D - Stack

Stack is used to hold operators during infix to postfix conversion.

Q5.
Postfix expression evaluation uses:
AQueue
BTwo stacks
COne stack
DNo data structure
Show Answer & Explanation

Correct Answer: C - One stack

Postfix evaluation needs one stack to hold operands.

Q6.
To check balanced parentheses, we use:
AQueue
BTree
CStack
DGraph
Show Answer & Explanation

Correct Answer: C - Stack

Stack matches opening brackets with closing brackets.

Q7.
Stack overflow occurs when:
APush to full stack
BPop from empty stack
CStack is empty
DPeek operation fails
Show Answer & Explanation

Correct Answer: A - Push to full stack

Stack overflow happens when pushing to a stack that is already full.

Q8.
Stack underflow occurs when:
AStack is full
BPop from empty stack
CPush succeeds
DMultiple pushes
Show Answer & Explanation

Correct Answer: B - Pop from empty stack

Stack underflow happens when trying to pop from an empty stack.

Q9.
Recursion internally uses:
AQueue
BStack
CHash table
DHeap
Show Answer & Explanation

Correct Answer: B - Stack

Recursion uses call stack to store function calls and local variables.

Q10.
Which traversal uses stack (non-recursive)?
ALevel order
BDFS/Inorder/Preorder/Postorder
CBFS
DNone
Show Answer & Explanation

Correct Answer: B - DFS/Inorder/Preorder/Postorder

DFS and tree traversals use stack for iterative implementation.

Q11.
Two stacks can be used to implement a:
ATree
BQueue
CGraph
DHash table
Show Answer & Explanation

Correct Answer: B - Queue

Two stacks can simulate a queue - one for enqueue operations and one for dequeue.

Q12.
The peek operation in stack:
ARemoves top element
BAdds element
CReturns top without removing
DEmpties stack
Show Answer & Explanation

Correct Answer: C - Returns top without removing

Peek returns the top element without removing it from the stack.

Q13.
Tower of Hanoi problem uses:
AQueue
BGraph
CHash table
DStack/Recursion
Show Answer & Explanation

Correct Answer: D - Stack/Recursion

Tower of Hanoi is solved using recursion which internally uses stack.

Q14.
Minimum number of stacks needed to implement a queue:
A1
B2
C3
D4
Show Answer & Explanation

Correct Answer: B - 2

Two stacks are sufficient to implement a queue efficiently.

Q15.
Which sorting algorithm uses stack?
AMerge sort
BBubble sort
CQuick sort (iterative)
DSelection sort
Show Answer & Explanation

Correct Answer: C - Quick sort (iterative)

Iterative quick sort uses explicit stack to store subarray boundaries.

Q16.
Stock span problem is solved using:
AQueue
BGraph
CTree
DStack
Show Answer & Explanation

Correct Answer: D - Stack

Stock span problem uses stack to find the span of stock prices efficiently.

Q17.
Next greater element problem uses:
AStack
BQueue
CHeap
DTree
Show Answer & Explanation

Correct Answer: A - Stack

Next greater element is efficiently solved using stack in O(n) time.

Q18.
In array implementation of stack, top is initialized to:
A0
B1
C-1
DArray size
Show Answer & Explanation

Correct Answer: C - -1

Top is initialized to -1 indicating empty stack. First push makes top = 0.

Q19.
A stack can be implemented using:
AOnly arrays
BBoth arrays and linked lists
COnly linked lists
DNeither
Show Answer & Explanation

Correct Answer: B - Both arrays and linked lists

Stack can be implemented using both arrays and linked lists with their respective trade-offs.

Q20.
Reversing a string can be done using:
AStack
BQueue
CTree
DGraph
Show Answer & Explanation

Correct Answer: A - Stack

Push all characters to stack, then pop them to get reversed string (LIFO property).

Q21.
Which principle does a stack follow?
AFIFO (First In First Out)
BLIFO (Last In First Out)
CLILO (Last In Last Out)
DPriority-based
Show Answer & Explanation

Correct Answer: B - LIFO (Last In First Out)

A stack follows LIFO — the last element pushed onto the stack is the first one to be popped. This is analogous to a stack of plates where you take from the top.

Q22.
What is the time complexity of push and pop operations in a stack implemented using an array?
AO(n) each
BO(1) each
CO(log n) each
DO(1) for push, O(n) for pop
Show Answer & Explanation

Correct Answer: B - O(1) each

In an array-based stack, push increments top and adds the element, while pop reads the element and decrements top. Both are single operations taking O(1) time.

Q23.
What happens when you try to pop an element from an empty stack?
AStack underflow
BReturns NULL
CReturns 0
DStack overflow
Show Answer & Explanation

Correct Answer: A - Stack underflow

Attempting to pop from an empty stack causes a stack underflow condition. This is an error that should be handled, as there are no elements to remove.

Q24.
What is the postfix expression equivalent of the infix expression: A + B * C?
AA B C * +
BA B + C *
CA B C + *
D+ A * B C
Show Answer & Explanation

Correct Answer: A - A B C * +

Due to operator precedence, B * C is evaluated first: B C *. Then A is added: A B C * +. In postfix, operands appear before their operators.

Q25.
Which data structure is used for function call management in most programming languages?
AQueue
BStack
CArray
DLinked list
Show Answer & Explanation

Correct Answer: B - Stack

The call stack stores function return addresses, local variables, and parameters. When a function is called, a stack frame is pushed; when it returns, the frame is popped.

Q26.
What is the result of evaluating the postfix expression: 5 3 + 8 2 - *?
A48
B16
C40
D24
Show Answer & Explanation

Correct Answer: A - 48

Step by step: 5 3 + gives 8; 8 2 - gives 6; 8 * 6 gives 48. We push operands and apply operators to the top two elements.

Q27.
What is the condition for stack overflow in an array-based stack of size MAX?
Atop == -1
Btop == 0
Ctop == MAX
Dtop == MAX - 1
Show Answer & Explanation

Correct Answer: D - top == MAX - 1

When top equals MAX - 1 (using 0-based indexing), the array is full. Any attempt to push another element causes a stack overflow.

Q28.
Which of the following is NOT a valid application of stacks?
AUndo/Redo operations
BExpression evaluation
CCPU scheduling
DBacktracking algorithms
Show Answer & Explanation

Correct Answer: C - CPU scheduling

CPU scheduling typically uses queues (like ready queue, priority queue) to manage processes. Stacks are used for undo/redo, expression evaluation, and backtracking.

Q29.
The prefix (Polish) notation for A * (B + C) is:
A* + A B C
B+ * A B C
CA * B + C
D* A + B C
Show Answer & Explanation

Correct Answer: D - * A + B C

In prefix notation, the operator comes before its operands. First, B + C becomes + B C. Then A * (result) becomes * A + B C.

Q30.
What is the minimum number of stacks required to implement a queue?
A1
B2
C3
D4
Show Answer & Explanation

Correct Answer: B - 2

A queue can be implemented using two stacks: one for enqueue operations and one for dequeue operations. Elements are transferred between stacks to reverse the LIFO order to FIFO.

Q31.
In a stack-based parenthesis matching algorithm, what action is taken when a closing bracket is encountered?
APop and compare with the top of the stack
BPush it onto the stack
CIgnore it
DClear the stack
Show Answer & Explanation

Correct Answer: A - Pop and compare with the top of the stack

When a closing bracket is encountered, the top element is popped and checked if it matches the corresponding opening bracket. If not, the expression has unbalanced parentheses.

Q32.
What is the output if we push 1, 2, 3, 4 and then pop twice from a stack?
A1, 2
B4, 3
C3, 4
D2, 1
Show Answer & Explanation

Correct Answer: B - 4, 3

Stack follows LIFO. After pushing 1, 2, 3, 4 (top = 4), the first pop returns 4 and the second pop returns 3.

Q33.
Which of the following expressions can be evaluated without parentheses?
ABoth prefix and postfix
BPrefix
CInfix
DNone of the above
Show Answer & Explanation

Correct Answer: A - Both prefix and postfix

Both prefix and postfix notations are unambiguous and do not require parentheses. The order of operators and operands alone determines the evaluation order.

Q34.
A stack is used in Depth-First Search (DFS) of a graph. What is an alternative to using an explicit stack?
AUsing a queue
BUsing a priority queue
CUsing a hash table
DUsing recursion (implicit call stack)
Show Answer & Explanation

Correct Answer: D - Using recursion (implicit call stack)

Recursion uses the program's call stack implicitly. Each recursive call pushes a frame on the call stack, mimicking the behavior of an explicit stack in DFS.

Q35.
In the Tower of Hanoi problem with n disks, the minimum number of moves required is:
A
B2ⁿ
Cn!
D2ⁿ - 1
Show Answer & Explanation

Correct Answer: D - 2ⁿ - 1

The Tower of Hanoi requires 2ⁿ - 1 moves. Each additional disk doubles the moves and adds one. For n = 3: 2³ - 1 = 7 moves.

Q36.
What is the time complexity of converting an infix expression to postfix using a stack?
AO(1)
BO(n log n)
CO(n²)
DO(n)
Show Answer & Explanation

Correct Answer: D - O(n)

The Shunting Yard algorithm processes each token of the expression exactly once. Each token is pushed and popped from the stack at most once, giving O(n) overall.

Q37.
What is the output of evaluating the prefix expression: - * + 3 4 5 6?
A7
B35
C29
D1
Show Answer & Explanation

Correct Answer: C - 29

Evaluating from right to left: + 3 4 = 7, then * 7 5 = 35, then - 35 6 = 29.

Q38.
Which condition checks if a stack implemented using an array is empty?
Atop == -1
Btop == 0
Ctop == MAX
Dtop == MAX - 1
Show Answer & Explanation

Correct Answer: A - top == -1

When top is initialized to -1, the stack is empty. Each push increments top, and each pop decrements it. When top returns to -1, the stack is empty again.

Q39.
Two stacks can be efficiently implemented in a single array by:
AGrowing one stack from each end toward the middle
BDividing the array in half
CUsing alternate indices for each stack
DIt cannot be done efficiently
Show Answer & Explanation

Correct Answer: A - Growing one stack from each end toward the middle

One stack grows from the left end (index 0 upward) and the other from the right end (index n-1 downward). They overflow only when they meet, maximizing space utilization.

Q40.
What type of recursion can be directly replaced by iteration using a stack?
AOnly tail recursion
BAny type of recursion
COnly head recursion
DRecursion cannot be replaced by a stack
Show Answer & Explanation

Correct Answer: B - Any type of recursion

Any recursive function can be converted to an iterative one using an explicit stack. The stack simulates the call stack by storing the parameters and local variables for each recursive call.

Q41.
In the stock span problem, what is the time complexity of the stack-based solution?
AO(n²)
BO(n)
CO(n log n)
DO(log n)
Show Answer & Explanation

Correct Answer: B - O(n)

Each element is pushed and popped from the stack at most once across the entire algorithm. Therefore, the total operations are O(n) despite the inner while loop.

Q42.
The expression (A + B) * (C - D) in postfix form is:
A* + A B - C D
BA B C D + - *
CA B + C D - *
DA B + * C D -
Show Answer & Explanation

Correct Answer: C - A B + C D - *

Convert each sub-expression: A + B becomes A B +, C - D becomes C D -. Then multiply: A B + C D - *. Operands appear before their operators.

Q43.
What is the maximum depth of the recursion stack for calculating factorial(n)?
An
Blog n
Cn - 1
Dn + 1
Show Answer & Explanation

Correct Answer: D - n + 1

factorial(n) calls factorial(n-1), which calls factorial(n-2), down to factorial(0). This creates n + 1 stack frames (from factorial(n) to factorial(0)).

Q44.
A stack can be used to check whether a given string is a palindrome by:
ABoth A and B are valid approaches
BOnly pushing the first half and comparing with the second half
CPushing all characters and comparing with the original
DStacks cannot be used for palindrome checking
Show Answer & Explanation

Correct Answer: A - Both A and B are valid approaches

Both approaches work. Pushing all characters and popping gives the reverse; comparing with original checks palindrome. Pushing only the first half and comparing with the second half is more efficient.

Q45.
What is the space complexity of evaluating a postfix expression with n tokens using a stack?
AO(1)
BO(log n)
CO(n²)
DO(n)
Show Answer & Explanation

Correct Answer: D - O(n)

In the worst case, all operands could be pushed before any operator is encountered. The stack can hold up to O(n) elements.

Q46.
Which of the following problems can be solved using a stack?
AFinding the shortest path
BBreadth-first search in a graph
CNext greater element for each element in an array
DLevel-order traversal of a tree
Show Answer & Explanation

Correct Answer: C - Next greater element for each element in an array

The next greater element problem is solved efficiently using a stack in O(n) time. BFS, shortest path, and level-order traversal all use queues, not stacks.

Q47.
In an infix to postfix conversion, when the scanned operator has lower precedence than the stack top operator, we:
APush the scanned operator onto the stack
BClear the entire stack
CDiscard the scanned operator
DPop the stack top and add to output, then compare again
Show Answer & Explanation

Correct Answer: D - Pop the stack top and add to output, then compare again

When the scanned operator has lower or equal precedence, we pop the stack top to the output and repeat the comparison. We push the scanned operator only when the stack is empty or its precedence is higher than the top.

Q48.
If elements are pushed onto a stack in the order 1, 2, 3, 4, which of the following is NOT a valid pop sequence?
A3, 1, 2, 4
B1, 2, 3, 4
C3, 4, 2, 1
D4, 3, 2, 1
Show Answer & Explanation

Correct Answer: A - 3, 1, 2, 4

After popping 3, the stack has 1, 2 (top). To pop 1 next, we must first pop 2. The sequence 3, 1, 2, 4 requires popping 1 before 2, which is impossible since 2 is on top of 1.

Q49.
Which traversal of a binary tree can be implemented using a single stack iteratively?
AInorder only
BPreorder only
CBoth inorder and preorder
DNone can use a single stack
Show Answer & Explanation

Correct Answer: C - Both inorder and preorder

Both inorder and preorder traversals can be implemented iteratively using a single stack. Preorder pushes and pops nodes while visiting them; inorder pushes left children, pops, visits, and goes right.

Q50.
What is the primary difference between a stack and a queue?
AStack uses LIFO, queue uses FIFO
BStack uses FIFO, queue uses LIFO
CBoth use LIFO
DBoth use FIFO
Show Answer & Explanation

Correct Answer: A - Stack uses LIFO, queue uses FIFO

A stack uses LIFO (Last In, First Out) — the newest element is removed first. A queue uses FIFO (First In, First Out) — the oldest element is removed first.

Showing 1-10 of 50 questions