Practice Questions – Data Structure Stack
MCQ type questions:
1. What is the data structure used in a function call?
A) Queue B) Stack C) Array D) Linked List
2. Which of the following is NOT an application of stack?
A) Reversing a string B) Recursion C) Parentheses checking D) Level-order traversal of a tree
3. What will be the output of the following operations on a stack?
push(1), push(2), push(3), pop(), push(4), pop()
A) 2 B) 3 C) 4 D) 1
4. Which operation is used to remove an element from a stack?
A) Insert B) Delete C) Pop D) Push
5. What is the time complexity of the push operation in a stack (array-based)?
A) O(1) B) O(n) C) O(log n) D) O(n log n)
6. Which of the following is the correct representation of a stack?
A) FIFO B) LIFO C) FILO D) Both B and C
7. Which function is used to add an element to the top of the stack?
A) append() B) insert() C) pop() D) push()
8. What does the following code do?
stack = []
stack.append(10)
stack.append(20)
stack.pop()
print(stack[-1])
A) 10 B) 20 C) Error D) None
9. What happens when you pop from an empty stack in Python?
A) Returns None B) Returns 0 C) Raises IndexError D) Does nothing
10. In a stack implemented using a list in Python, which method removes the top element?
A) pop() B) remove() C) del() D) discard()
Short Answer type questions:
1. Define a stack. Mention its two primary operations.
2. Write the difference between push() and pop() operations.
3. Give any two real-life examples where a stack is used.
4. What happens when a pop operation is performed on an empty stack?
5. State one difference between a stack and a queue.
6. Write a Python code snippet to check if a stack is empty.
7. How is a stack used in function call management?
8. Define stack overflow and underflow.
9. What is the use of stack[-1] in Python?
10. Write a Python function to display the contents of a stack without using loops.
Output type questions:
Q1. Predict the output:
stack = []
stack.append(10)
stack.append(20)
stack.pop()
stack.append(30)
print(stack)
Q2. What will be the output of the following code?
def stack_display(stack):
if not stack:
return
top = stack.pop()
print(top)
stack_display(stack)
stack.append(top)
s = [1, 2, 3]
stack_display(s)
print(s)
Q3. Find the output:
s = []
for i in range(5):
s.append(i)
for i in range(3):
s.pop()
print(s)
Q4. Output of the following stack operations?
stack = []
stack.append('A')
stack.append('B')
x = stack.pop()
stack.append('C')
print(x)
print(stack)
Q5. Identify the output:
def reverse_stack(s):
if s:
x = s.pop()
reverse_stack(s)
print(x, end=' ')
stack = [1, 2, 3, 4]
reverse_stack(stack)
Q6. What will be printed?
s = []
s.append(100)
print(s[-1])
s.pop()
print("Empty" if not s else "Not Empty")
Q7. Predict the output of the code:
stack = []
stack.append(1)
stack.append(2)
stack.append(3)
print(stack.pop(), end=' ')
print(stack.pop(), end=' ')
stack.append(4)
print(stack[-1])
Program type questions:
Q1. Write a Python program to implement a stack using list with the following operations:
Push an element
Pop an element
Display stack elements
Q2. Write a Python program to reverse a string using stack.