Font size
WorksheetsQuiz2-CC104-MakScie
Total questions: 40
Worksheet time: 40mins
A text editor uses a stack to implement undo operations. Each action is pushed onto the stack. What will be printed after the following code?
stack = ["type A", "type B", "delete A"]
stack.pop()
print(stack[-1])
type A
delete A
type B
None
When functions call each other, the call stack stores the return addresses. What will the code print?
def A():
print("A start")
B()
print("A end")
def B():
print("B start")
A()
A start → A end → B start
A start → B start → A end
B start → A start → A end
A end → B start → A start
A developer uses a stack to reverse a string.
word = "DATA"
stack = list(word)
reversed_word = ""
while stack:
reversed_word += stack.pop()
print(reversed_word)
DATA
ADTA
ATAD
TADA
A stack simulates plate stacking. The code below represents stacking and unstacking actions: stack = []
actions = ["add 1", "add 2", "remove", "add 3"]
for action in actions:
if "add" in action:
stack.append(action[-1])
else:
stack.pop()
print(stack)
['1']
['3']
['1', '3']
A compiler checks balanced parentheses using stacks. expr = "(a+b)*(c+d))"
stack = []
for ch in expr:
if ch == '(':
stack.append(ch)
elif ch == ')':
if stack:
stack.pop()
else:
print("Unbalanced")
break
What will it print?
Balanced
Unbalanced
Error
None
A browser uses a stack for back navigation. history = ["google.com", "facebook.com", "github.com"]
history.pop()
print("Back to:", history[-1])
What will be printed by the code above?
Back to: github.com
Back to: facebook.com
Back to: google.com
Back to: github.com.com
A developer pushes three numbers into a stack and doubles the top one. stack = [2, 4, 6]
stack[-1] *= 2
print(stack.pop())
12
6
8
4
A stack is used to reverse order of tasks. tasks = []
for t in ["Task1", "Task2", "Task3"]:
tasks.append(t)
while tasks:
print(tasks.pop(), end=" ")
Task1 Task2 Task3
Task3 Task2 Task1
Task2 Task1 Task3
Error
A developer mistakenly pops too many items. stack = [1, 2]
stack.pop()
stack.pop()
stack.pop()
[ ]
[2]
Error
[1]
A programmer wants to copy stack contents safely. stack = [1, 2, 3]
new_stack = stack.copy()
stack.pop()
print(new_stack)
[1, 2]
[1, 2, 3]
[3]
[]
Stack simulates LIFO order in a warehouse. boxes = ["Box1", "Box2", "Box3"]
boxes.pop()
print(boxes)
['Box1', 'Box2']
['Box3']
['Box2']
[]
Stack operations to check top element: s = [10, 20, 30]
print(s[-1])
30
10
20
Error
A function uses a stack to backtrack paths. path = ["A", "B", "C"]
path.pop()
print("Back to:", path[-1])
A
B
C
None
Stack simulation in postfix evaluation: stack = [3, 4]
stack.append(stack.pop() * 2)
print(stack)
[3, 8]
[6, 4]
[8]
[4, 3]
If a stack has a maximum size of 3, how many pushes cause overflow?
3rd push
4th push
2nd push
1st push
A system serves customers in the order they arrive. from collections import deque
queue = deque(["Anna", "Ben", "Chris"])
queue.popleft()
print(queue)
deque(['Ben', 'Chris'])
deque(['Anna'])
deque(['Chris'])
[]
An operating system schedules jobs in a queue. jobs = ["A", "B", "C"]
jobs.pop(0)
jobs.append("D")
print(jobs)
['B', 'C', 'D']
['A', 'B', 'D']
['C', 'D']
['D']
A queue is used for task scheduling: tasks = []
for i in range(3):
tasks.append(i)
tasks.pop(0)
print(tasks)
[0, 1]
[1, 2]
[2]
[]
A queue rotates messages cyclically: from collections import deque
msgs = deque(["Msg1", "Msg2", "Msg3"])
msgs.rotate(1)
print(msgs)
deque(['Msg3', 'Msg1', 'Msg2'])
deque(['Msg2', 'Msg3', 'Msg1'])
deque(['Msg1', 'Msg2', 'Msg3'])
deque(['Msg2', 'Msg1', 'Msg3'])
A queue is used to manage print jobs. q = ["Doc1", "Doc2", "Doc3"]
q.pop(0)
q.append("Doc4")
print(q)
['Doc2', 'Doc3', 'Doc4']
['Doc3', 'Doc4']
['Doc4']
['Doc2', 'Doc4']
If a circular queue of size 3 contains [1, 2, 3], what happens on another enqueue?
Overwrites 1
Raises Overflow
Inserts 4 at end
Deletes 2
A queue simulates ticket line processing: line = ["A", "B"]
line.insert(0, "C")
print(line)
['A', 'B', 'C']
['C', 'A', 'B']
Queue popping until empty: q = [1, 2, 3]
while q:
q.pop(0)
print(q)
[]
[1]
[3]
[0]
Bank queue example: serving first customer. queue = ["John", "Paul", "Mary"]
served = queue.pop(0)
print(served)
Mary
John
Paul
None
Circular buffer drops oldest element automatically.
from collections import deque
q = deque(maxlen=3)
for i in range(5):
q.append(i)
print(q)
deque([0, 1, 2])
deque([0, 1, 2])
deque([3, 4, 5])
Neverdeque([1, 2, 3])
Queue stores messages in order: q = []
for i in range(3):
q.append(f"Msg{i}")
print(q.pop(0))
Msg0
Msg1
Msg2
Error
Ticket serving simulation: q = ["C1", "C2", "C3"]
q.pop(0)
q.pop(0)
print(q)
['C3']
['C1']
[]
['C2']
Queue enqueue and dequeue pattern:
from collections import deque
q = deque()
q.append(1)
q.append(2)
q.popleft()
q.append(3)
print(q)
deque([1, 3])
deque([2, 3])
deque([3])
deque([1])
A queue used for buffering data packets. When queue is full, new packets are:
Dropped
Overwritten
Duplicated
Sent first
Using list as queue has which performance issue?
pop(0) is O(n)
append() is O(n)
insert() is O(1)
Which of the following is true about list operations in Python?
pop() is O(1)
pop(0) is O(n)
append() is O(n)
insert() is O(1)
Queue empty check:
from collections import deque
q = deque()
print(len(q) == 0)
True
False
Error
None
A stack is implemented using an array. If the stack size is 5 and currently holds 4 elements, what will happen if a push operation is performed?
Underflow
Overflow
Valid operation
None
If a queue is implemented using an array with front = 2, rear = 4, what is the current number of elements?
2
3
4
5
If a stack is implemented using a singly linked list, where should the new element be added for O(1) insertion?
Beginning
Middle
End
Any position
In a circular queue of size 5, front = 3 and rear = 1. How many elements are in the queue?
2
3
4
5
Which of the following is not a valid stack operation?
push()
pop()
peek()
delete()
Which operation of a queue results in underflow?
Enqueue on full queue
Dequeue on empty queue
Peek on full queue
Peek on empty queue
If a stack has elements [5, 10, 15] (top = rightmost), after performing pop(); push(20);, what is the new top?
(a)
A queue implemented with two stacks uses how many push and pop operations for one enqueue?
1 push
2 push
1 push and 1 pop
2 push and 1 pop
