Skip to main contentSkip to solution

Sequence to check if a queue is empty and handle underflow in a programming context:

(A). Attempt to dequeue an element.

(B). Check if the queue is empty.

(C). Return an error message if empty.

(D). Proceed with dequeue if not empty.

Solution

✅ Correct Option: 1

A dequeue request starts the process, and the queue is checked before any element is removed. The two outcomes of the check then follow: an error if the queue is empty, otherwise the dequeue proceeds.

(A) Attempt to dequeue an element.

(B) Check if the queue is empty.

(C) Return an error message if empty (underflow).

(D) Proceed with dequeue if not empty.


Options 2, 3 and 4: each puts an outcome, (C) or (D), before the check (B), but the error or the dequeue can only happen once the queue's state is known.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question