wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz2-CC104-MakScie

Total questions: 40

Worksheet time: 40mins

Name
Class
Date
1.

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])

a)

type A

b)

delete A

c)

type B

d)

None

2.

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)

A start → A end → B start

b)

A start → B start → A end

c)

B start → A start → A end

d)

A end → B start → A start

3.

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)

a)

DATA

b)

ADTA

c)

ATAD

d)

TADA

4.

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)

a)

['1']

b)

['3']

c)

['1', '3']

5.

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?

a)

Balanced

b)

Unbalanced

c)

Error

d)

None

6.

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?

a)

Back to: github.com

b)

Back to: facebook.com

c)

Back to: google.com

d)

Back to: github.com.com

7.

A developer pushes three numbers into a stack and doubles the top one. stack = [2, 4, 6]

stack[-1] *= 2

print(stack.pop())

a)

12

b)

6

c)

8

d)

4

8.

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=" ")

a)

Task1 Task2 Task3

b)

Task3 Task2 Task1

c)

Task2 Task1 Task3

d)

Error

9.

A developer mistakenly pops too many items. stack = [1, 2]

stack.pop()

stack.pop()

stack.pop()

a)

[ ]

b)

[2]

c)

Error

d)

[1]

10.

A programmer wants to copy stack contents safely. stack = [1, 2, 3]

new_stack = stack.copy()

stack.pop()

print(new_stack)

a)

[1, 2]

b)

[1, 2, 3]

c)

[3]

d)

[]

11.

Stack simulates LIFO order in a warehouse. boxes = ["Box1", "Box2", "Box3"]

boxes.pop()

print(boxes)

a)

['Box1', 'Box2']

b)

['Box3']

c)

['Box2']

d)

 []

12.

Stack operations to check top element: s = [10, 20, 30]

print(s[-1])

a)

30

b)

10

c)

20

d)

Error

13.

A function uses a stack to backtrack paths. path = ["A", "B", "C"]

path.pop()

print("Back to:", path[-1])

a)

A

b)

B

c)

C

d)

None

14.

Stack simulation in postfix evaluation: stack = [3, 4]

stack.append(stack.pop() * 2)

print(stack)

a)

[3, 8]

b)

[6, 4]

c)

[8]

d)

[4, 3]

15.

If a stack has a maximum size of 3, how many pushes cause overflow?

a)

3rd push

b)

4th push

c)

2nd push

d)

1st push

16.

A system serves customers in the order they arrive. from collections import deque

queue = deque(["Anna", "Ben", "Chris"])

queue.popleft()

print(queue)

a)

deque(['Ben', 'Chris'])

b)

deque(['Anna'])

c)

deque(['Chris'])

d)

[]

17.

An operating system schedules jobs in a queue. jobs = ["A", "B", "C"]

jobs.pop(0)

jobs.append("D")

print(jobs)

a)

['B', 'C', 'D']

b)

['A', 'B', 'D']

c)

['C', 'D']

d)

['D']

18.

A queue is used for task scheduling: tasks = []

for i in range(3):

    tasks.append(i)

tasks.pop(0)

print(tasks)

a)

[0, 1]

b)

[1, 2]

c)

[2]

d)

[]

19.

A queue rotates messages cyclically: from collections import deque

msgs = deque(["Msg1", "Msg2", "Msg3"])

msgs.rotate(1)

print(msgs)

a)

deque(['Msg3', 'Msg1', 'Msg2'])

b)

deque(['Msg2', 'Msg3', 'Msg1'])

c)

deque(['Msg1', 'Msg2', 'Msg3'])

d)

deque(['Msg2', 'Msg1', 'Msg3'])

20.

A queue is used to manage print jobs. q = ["Doc1", "Doc2", "Doc3"]

q.pop(0)

q.append("Doc4")

print(q)

a)

['Doc2', 'Doc3', 'Doc4']

b)

['Doc3', 'Doc4']

c)

['Doc4']

d)

['Doc2', 'Doc4']

21.

If a circular queue of size 3 contains [1, 2, 3], what happens on another enqueue?

a)

Overwrites 1

b)

Raises Overflow

c)

Inserts 4 at end

d)

Deletes 2

22.

A queue simulates ticket line processing: line = ["A", "B"]

line.insert(0, "C")

print(line)

a)

['A', 'B', 'C']

b)

['C', 'A', 'B']

23.

Queue popping until empty: q = [1, 2, 3]

while q:

    q.pop(0)

print(q)

a)

[]

b)

[1]

c)

[3]

d)

[0]

24.

Bank queue example: serving first customer. queue = ["John", "Paul", "Mary"]

served = queue.pop(0)

print(served)

a)

Mary

b)

John

c)

Paul

d)

None

25.

Circular buffer drops oldest element automatically.

from collections import deque

q = deque(maxlen=3)

for i in range(5):

    q.append(i)

print(q)

a)

deque([0, 1, 2])

b)

deque([0, 1, 2])

c)

deque([3, 4, 5])

d)

Neverdeque([1, 2, 3])

26.

Queue stores messages in order: q = []

for i in range(3):

    q.append(f"Msg{i}")

print(q.pop(0))

a)

Msg0

b)

Msg1

c)

Msg2

d)

Error

27.

Ticket serving simulation: q = ["C1", "C2", "C3"]

q.pop(0)

q.pop(0)

print(q)

a)

['C3']

b)

['C1']

c)

 []

d)

 ['C2']

28.

Queue enqueue and dequeue pattern:

from collections import deque

q = deque()

q.append(1)

q.append(2)

q.popleft()

q.append(3)

print(q)

a)

deque([1, 3])

b)

deque([2, 3])

c)

deque([3])

d)

deque([1])

29.

A queue used for buffering data packets. When queue is full, new packets are:

a)

Dropped

b)

Overwritten

c)

Duplicated

d)

Sent first

30.

Using list as queue has which performance issue?

a)
pop() is O(1)
b)

pop(0) is O(n)

c)

append() is O(n)

d)

insert() is O(1)

31.

Which of the following is true about list operations in Python?

a)

pop() is O(1)

b)

pop(0) is O(n)

c)

append() is O(n)

d)

insert() is O(1)

32.

Queue empty check:

from collections import deque

q = deque()

print(len(q) == 0)

a)

True

b)

False

c)

Error

d)

None

33.

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?

a)

Underflow

b)

Overflow

c)

Valid operation

d)

None

34.

If a queue is implemented using an array with front = 2, rear = 4, what is the current number of elements?

a)

2

b)

3

c)

4

d)

5

35.

If a stack is implemented using a singly linked list, where should the new element be added for O(1) insertion?

a)

Beginning

b)

Middle

c)

End

d)

Any position

36.

In a circular queue of size 5, front = 3 and rear = 1. How many elements are in the queue?

a)

2

b)

3

c)

4

d)

5

37.

Which of the following is not a valid stack operation?

a)

push()

b)

pop()

c)

peek()

d)

delete()

38.

Which operation of a queue results in underflow?

a)

Enqueue on full queue

b)

Dequeue on empty queue

c)

Peek on full queue

d)

Peek on empty queue

39.

If a stack has elements [5, 10, 15] (top = rightmost), after performing pop(); push(20);, what is the new top?


(a)  

40.

A queue implemented with two stacks uses how many push and pop operations for one enqueue?

a)

1 push

b)

2 push

c)

1 push and 1 pop

d)

2 push and 1 pop