Queue using Array

Uncategorized

This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Queue using Array”.

1. In a circular queue, how do you increment the rear end of the queue?
A) rear++
B) (rear+1) % CAPACITY
C) (rear % CAPACITY)+1
D) rear–

Answer: B
Explanation: Ensures that the values in the rear range from 0 to (CAPACITY-1).

2. Which of the following properties is associated with a queue?
A) First In Last Out
B) First In First Out
C) Last In First Out
D) Last In Last Out

Answer: B
Explanation: The queue is arranged in the order of first in, first out

3. What is the time complexity of enqueue operation?
A) O(logn)
B) O(nlogn)
C) O(n)
D) O(1)

Answer: D
Explanation: The enqueue process is performed at the back end, and adding a new item to the queue takes O(1) time.

4. What is the term for inserting into a full queue known as?
A) overflow
B) underflow
C) null pointer exception
D) program won’t be compiled

Answer: A
Explanation: Inserting into a complete queue is known as overload, much like stacking.

5. What is the need for a circular queue?
A) effective usage of memory
B) easier computations
C) to delete elements based on priority
D) implement LIFO principle in queues

Answer: A
Explanation: The dequeue operation in a linear queue causes the starting elements of the array to be zero, and there is no way to use that space, while in a circular queue, you can use that space effectively. The priority queue is used to delete elements according to their value. The elements with the highest priority would be removed first, followed by the elements with the lowest priority. The FIFO theory is often applied to queue data structures.

6. What does the following Java code do?

public Object function()
{
	if(isEmpty())
	return -999;
	else
	{
		Object high;
		high = q[front];
		return high;
	}
}

A) Dequeue
B) Enqueue
C) Return the front element
D) Return the last element

Answer: C
Explanation: It is not a dequeue operation because q[front] returns the element at the front of the queue; we are not transferring the ‘front’ to the next element.

7. Which of the following best describes the growth of a linear queue at runtime? (Q is the original queue, size() returns the number of elements in the queue)
A)

private void expand()
{
	int length = size();
	int[] newQ = new int[length<<1];
	for(int i=front; i<=rear; i++)
	{
		newQ[i-front] = Q[i%CAPACITY];
	}
	Q = newQ;
	front = 0;
	rear = size()-1;
}

B)

private void expand()
{
	int length = size();
	int[] newQ = new int[length<<1];
	for(int i=front; i<=rear; i++)
	{
		newQ[i-front] = Q[i%CAPACITY];
	}
	Q = newQ;
}

C)

private void expand()
{
	int length = size();
	int[] newQ = new int[length<<1];
	for(int i=front; i<=rear; i++)
	{
		newQ[i-front] = Q[i];
	}
	Q = newQ;
	front = 0;
	rear = size()-1;
}

D)

private void expand()
{
	int length = size();
	int[] newQ = new int[length*2];
	for(int i=front; i<=rear; i++)
	{
		newQ[i-front] = Q[i%CAPACITY];
	}
	Q = newQ;
}

Answer: A
Explanation: Simply doubling the size of an array at runtime is a common technique for growing its size. Create a new array twice the previous size and copy all the elements; don't forget to set front = 0 and rear = size()-1 after copying, as these are needed to keep the queue operations in order.

8. Which of the following represents a dequeue operation? (count is the number of elements in the queue)
A)
public Object dequeue() { if(count == 0) { System.out.println("Queue underflow"); return 0; } else { Object ele = q[front]; q[front] = null; front = (front+1)%CAPACITY; count--; return ele; } }
B)
public Object dequeue() { if(count == 0) { System.out.println("Queue underflow"); return 0; } else { Object ele = q[front]; front = (front+1)%CAPACITY; q[front] = null; count--; return ele; } }
C)
public Object dequeue() { if(count == 0) { System.out.println("Queue underflow"); return 0; } else { front = (front+1)%CAPACITY; Object ele = q[front]; q[front] = null; count--; return ele; } }
D)
public Object dequeue() { if(count == 0) { System.out.println("Queue underflow"); return 0; } else { Object ele = q[front]; q[front] = null; front = (front+1)%CAPACITY; return ele; count--; } }

Answer: A
Explanation: Dequeue removes the first item from the queue, while ‘front' refers to the queue's front end and returns the first item.

 9.What is the output of the following Java code? 
 public class CircularQueue
 {
     protected static final int CAPACITY = 100;
     protected int size,front,rear;
     protected Object q[];
     int count = 0;
 public CircularQueue() {     this(CAPACITY); } public CircularQueue (int n) {     size = n;     front = 0;     rear = 0;     q = new Object[size]; } public void enqueue(Object item) {     if(count == size)     {         System.out.println("Queue overflow");             return;     }     else     {         q[rear] = item;         rear = (rear+1)%size;         count++;     } } public Object dequeue() {     if(count == 0)     {         System.out.println("Queue underflow");         return 0;     }     else     {         Object ele = q[front];         q[front] = null;         front = (front+1)%size;         count--;         return ele;     } } public Object frontElement() {     if(count == 0)     return -999;     else     {         Object high;         high = q[front];         return high;     } } public Object rearElement() {     if(count == 0)     return -999;     else     {         Object low;         rear = (rear-1)%size;         low = q[rear];         rear = (rear+1)%size;         return low;     } }
 }
 public class CircularQueueDemo
 {
     public static void main(String args[])
     {
         Object var;
         CircularQueue myQ = new CircularQueue();
         myQ.enqueue(10);
         myQ.enqueue(3);
         var = myQ.rearElement();
         myQ.dequeue();
         myQ.enqueue(6);
         var = mQ.frontElement();
         System.out.println(var+" "+var);
     }
 }
 A) 3 3
 B) 3 6
 C) 6 6
 D) 10 6
Answer: A
Explanation: Enqueue 10 and 3 into the queue first, then dequeue(removes 10) and finally enqueue (6), Since 3 is at the front of the queue and 6 is at the back, calling frontElement() will return 3, which will be shown twice.

10. What is the space complexity of a linear queue having n elements?
A) O(n)
B) O(nlogn)
C) O(logn)
D) O(1)
Answer: A
Explanation: There are n elements in the equation.

A queue is a linear structure in which operations are carried out in a specific order. First in, first out is the order (FIFO). Any queue of customers for a resource where the customer who arrived first is served first is a good example of a queue. Stacks and queues vary in how they are removed. An Array, Stack, or Linked List may be used to enforce a Queue. Using an Array is the simplest way to execute a queue. The queue's head(FRONT) and tail(REAR) initially point to the array's first index (starting the index of array from 0 ).

Leave a Reply

Your email address will not be published. Required fields are marked *