Identify the task performed by the given function.
def f1(myQueue):
if isEmpty(myQueue):
print('Queue is empty')
return None
else:
return myQueue[0]
Identify the task performed by the given function.
def f1(myQueue):
if isEmpty(myQueue):
print('Queue is empty')
return None
else:
return myQueue[0]
Solution
The function first checks whether the queue is empty. If it is, it prints a message and returns None. Otherwise it returns myQueue[0], the front element, without removing it. This is the peek (front) operation of a queue.
Option 1: deleting the front element (dequeue) would need myQueue.pop(0), which removes the element. Here the element only is returned.
Option 3: insertion (enqueue) would use append(), which this function does not call.
Option 4: displaying all elements would need a loop or a print of the whole list. This function returns a single element.
Related questions:
2026: 20 May Shift 1
2025: 22 May Shift 1
2026: 23 May Shift 2
2026: 26 May Shift 1
2025: 16 May Shift 1
2026: 24 May Shift 1