Skip to content

Ch 4 · Queue

Exercises

Exercise 1 of 2

Queue

Exercises · Class 12 Computer Science · NCERT 2026-27

Question 1Fill in the blanks
Fill in the blanks:
  1. (a) ___ is a linear list of elements in which insertion and deletion take place from different ends.
  2. (b) Operations on a queue are performed in ___ order.
  3. (c) Insertion operation in a queue is called ___ and deletion operation is called ___.
  4. (d) Deletion of elements is performed from the ___ end of the queue.
  5. (e) Elements 'A','S','D' and 'F' are present in the queue and deleted one at a time; ___ is the sequence of elements received.
  6. (f) ___ is a data structure where elements can be added or removed at either end, but not in the middle.
  7. (g) A deque contains 'z','x','c','v' and 'b'. Elements received after deletion are 'z','b','v','x' and 'c'. ___ is the sequence of deletion operations performed on the deque.
Answer

(a) Queue is a linear list of elements with insertion and deletion at different ends. (b) Operations are performed in FIFO (First In, First Out) order. (c) Insertion is called enqueue, deletion is called dequeue. (d) Deletion happens from the front end. (e) Since a queue is FIFO, deleting one at a time returns them in the same order they were inserted: A, S, D, F. (f) This describes a Deque (Double-Ended Queue). (g) With the deque z-x-c-v-b (front to rear), tracing which end each received element came from: 'z' is deleted from the front; 'b' from the rear; 'v' (now at the rear of the remaining x-c-v) from the rear; 'x' (now at the front of x-c) from the front; and finally 'c' (the last remaining element). So the sequence of operations is: deleteFront, deleteRear, deleteRear, deleteFront, deleteFront (or deleteRear, since only one element remains).

Question 2
Compare and contrast queue with stack.
Answer

Both are linear data structures. A stack follows LIFO; insertion and deletion both occur at the top. A queue follows FIFO; insertion occurs at the rear and deletion at the front.

Question 3
How does FIFO describe queue?
Answer

FIFO means First In, First Out. The element inserted first is kept at the front and is therefore the first element removed.

Question 4
Write a menu-driven Python program using a queue to implement the movement of a shuttlecock in its box.
Answer
queue = []
while True: print("1. Add shuttlecock\n2. Remove shuttlecock\n3. Display\n4. Exit") ch = int(input("Enter choice: ")) if ch == 1: queue.append(input("Enter shuttlecock ID: ")) elif ch == 2: if queue: print("Removed:", queue.pop(0)) else: print("Box is empty") elif ch == 3: print(queue) elif ch == 4: break
Question 5
How is the queue data type different from the deque data type?
Answer

A queue follows strict FIFO — insertion only at the rear, deletion only from the front. A deque (double-ended queue) allows insertion and deletion at both ends, giving it more flexibility — it can function as both a stack and a queue.

Question 6
Show the status of a queue after each operation: enqueue(34), enqueue(54), dequeue(), enqueue(12), dequeue(), enqueue(61), peek(), dequeue(), dequeue(), dequeue(), dequeue(), enqueue(1).
Answer

enqueue(34): [34]; enqueue(54): [34,54]; dequeue(): removes 34 → [54]; enqueue(12): [54,12]; dequeue(): removes 54 → [12]; enqueue(61): [12,61]; peek(): shows front=12, queue unchanged [12,61]; dequeue(): removes 12 → [61]; dequeue(): removes 61 → []; dequeue(): queue empty – underflow error; dequeue(): queue empty – underflow error; enqueue(1): [1].

Question 7
Show the status of a deque after each operation: peek(), insertFront(12), insertRear(67), deletionFront(), insertRear(43), deletionRear(), deletionFront(), deletionRear().
Answer

peek(): deque empty – error; insertFront(12): [12]; insertRear(67): [12,67]; deletionFront(): removes 12 → [67]; insertRear(43): [67,43]; deletionRear(): removes 43 → [67]; deletionFront(): removes 67 → []; deletionRear(): deque empty – underflow error.

Question 8
Write a Python program to check whether a given string is a palindrome, using a deque.
Answer
from collections import deque

def is_palindrome(s): dq = deque(s) while len(dq) > 1: if dq.popleft() != dq.pop(): return False return True

print(is_palindrome("MALAYALAM"))