What is the primary difference between a stack and a queue?
a) Stack is LIFO, while queue is FIFO
b) Queue is LIFO, while stack is FIFO
c) Stack allows multiple elements to be processed simultaneously
d) Queue allows random access to elements
Answer:
a) Stack is LIFO, while queue is FIFO
Explanation:
The primary difference between a stack and a queue is the order in which elements are processed. A stack follows Last In, First Out (LIFO) order, meaning the last element added is the first to be removed.
On the other hand, a queue follows First In, First Out (FIFO) order, meaning the first element added is the first to be removed. This makes queues suitable for tasks like scheduling or buffering, where the order of elements matters.
Stacks are commonly used in recursion, backtracking algorithms, and expression evaluation, while queues are used in breadth-first search and task scheduling.
Reference:
DSA (Data Structures and Algorithms) MCQ Questions and Answers