0% found this document useful (0 votes)
93 views11 pages

Queue Data Structure MCQs with Answers

The document contains 10 multiple choice questions about queues implemented using arrays. Some key points covered are: - Queues follow the FIFO (First In First Out) principle - For a circular queue, the rear index is incremented using (rear+1)%capacity to wrap around the array - The time complexity of enqueue is O(1) as it simply adds an element to the rear of the array - A linear queue's space complexity is O(n) as it uses an array of size n to store n elements

Uploaded by

Chotu111
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views11 pages

Queue Data Structure MCQs with Answers

The document contains 10 multiple choice questions about queues implemented using arrays. Some key points covered are: - Queues follow the FIFO (First In First Out) principle - For a circular queue, the rear index is incremented using (rear+1)%capacity to wrap around the array - The time complexity of enqueue is O(1) as it simply adds an element to the rear of the array - A linear queue's space complexity is O(n) as it uses an array of size n to store n elements

Uploaded by

Chotu111
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

Data Structure Questions and Answers – Queue


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

1. 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
View Answer

2. 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–
View Answer

3. 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
View Answer

advertisement

[Link] 1/11
28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

4. What is the time complexity of enqueue operation?


a) O(logn)
b) O(nlogn)
c) O(n)
d) O(1)
View Answer

5. What does the following Java code do?

Subscribe Now: Data Structure Newsletter | Important Subjects Newsletters

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
View Answer

advertisement

[Link] 2/11
28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

6. 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
View Answer

7. Which of the following represents a dequeue operation? (count is the number of elements in the
queue)
a)

advertisement

public Object dequeue()


{
if(count == 0)
{
[Link]("Queue underflow");
return 0;
}
else
{
Object ele = q[front];

[Link] 3/11
28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

q[front] = null;
front = (front+1)%CAPACITY;
count--;
return ele;
}
}

b)

public Object dequeue()


{
if(count == 0)
{
[Link]("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)
{
[Link]("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)
{

[Link] 4/11
28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

[Link]("Queue underflow");
return 0;
}
else
{
Object ele = q[front];
q[front] = null;
front = (front+1)%CAPACITY;
return ele;
count--;
}
}

View Answer

8. 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()


{

[Link] 5/11
28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

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;
}

View Answer

9. What is the space complexity of a linear queue having n elements?


a) O(n)
b) O(nlogn)
c) O(logn)
d) O(1)
View Answer

10. 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)
{

[Link] 6/11
28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

size = n;
front = 0;
rear = 0;
q = new Object[size];
}

public void enqueue(Object item)


{
if(count == size)
{
[Link]("Queue overflow");
return;
}
else
{
q[rear] = item;
rear = (rear+1)%size;
count++;
}
}
public Object dequeue()
{
if(count == 0)
{
[Link]("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;

[Link] 7/11
28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

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();
[Link](10);
[Link](3);
var = [Link]();
[Link]();
[Link](6);
var = [Link]();
[Link](var+" "+var);
}
}

a) 3 3
b) 3 6
c) 6 6
d) 10 6
View Answer

Sanfoundry Global Education & Learning Series – Data Structure.

To practice all areas of Data Structure, here is complete set of 1000+ Multiple Choice Questions and
Answers.

« Prev - Data Structure Questions and Answers » Next - Data Structure Questions and Answers –
– Stack using Linked List Queue using Linked List

Related Posts:

Practice Programming MCQs


Practice Design & Analysis of Algorithms MCQ
Practice Computer Science MCQs
Apply for Data Structure Internship

[Link] 8/11
28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

Apply for Information Technology Internship

Data Structure MCQ, DS MCQ - Abstract Datatype

advertisement

Recommended Articles:
1. Data Structure Questions and Answers – Queue using Linked List
2. Data Structure Questions and Answers – Queue Operations
3. Data Structure Questions and Answers – Stack using Array
4. Data Structure Questions and Answers – Queue using Stacks
5. C Program to Implement Queue using Array
6. Java Questions & Answers – Data Structures-Queue
7. Data Structure Questions and Answers – Double Ended Queue (Dequeue)
8. Java Program to Implement Queue
9. Data Structure Questions and Answers – Array and Array Operations
10. C++ Program to Implement Queue using Linked List

advertisement

[Link] 9/11
28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

Additional Resources:
Data Structure MCQ Questions
Data Structures in C
Data Structures in Java
Data Structures in C++
Java Array Programs

Popular Pages:
C# Array Programs
Data Science MCQ Questions
C++ STL
Java Programs on Collections
C++ Algorithm Library

Subscribe: Data Structure Newsletter

Name

Email

Subscribe

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to


get free Certificate of Merit. Join our social networks below and stay updated with latest contests,
[Link] 10/11
28/11/2023, 09:27 Queue using Array Questions and Answers - Sanfoundry

videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is
Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on
development of Linux Kernel, SAN Technologies, Advanced C, Data
Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram


SanfoundryClasses.

About | Certifications | Internships | Jobs | Privacy Policy | Terms | Copyright | Contact

     

© 2011-2023 Sanfoundry. All Rights Reserved.

[Link] 11/11

Common questions

Powered by AI

First In First Out (FIFO) refers to the order in which elements are processed: the first element added to the structure is the first one to be removed. This principle is used in queue data structures, ideal for tasks that require order, such as scheduling and buffering operations. Last In First Out (LIFO), used in stacks, processes the most recently added element first. This is effective for backtracking solutions or maintaining a history, such as in undo mechanisms in software applications. Each principle addresses different problem domains effectively, making their understanding crucial for selecting the appropriate data structure .

A circular queue provides more efficient use of memory compared to a linear queue. In a linear queue, once the rear reaches the end of the queue, the queue cannot accept more elements even if there is space available at the front. A circular queue solves this problem by connecting the end to the front, allowing for efficient reuse of space. This leads to better memory utilization and can help prevent overflow in scenarios where data is continuously added and removed .

The implementation of a circular queue in Java uses modular arithmetic to wrap the indices, specifically when updating the rear as `(rear+1) % size` and when updating the front as `(front+1) % size`. This use of modular arithmetic is crucial for efficiently managing the queue so that it operates effectively as a continuous loop within a fixed size array. The practical benefit is that it allows the queue to efficiently use available memory by enabling the reuse of emptied slots, rather than requiring elements to shift or creating a larger array when only a portion is full. This is essential for maintaining optimal performance and preventing memory waste .

Using modular arithmetic in queue operations ensures that queue indices wrap around when reaching the array's end, creating a circular pattern. This approach leads to efficient use of the fixed memory allotted to the queue, reducing the computational overhead of reallocating space or shifting elements, thereby improving software performance. It minimizes the potential for errors like out-of-bounds exceptions and reduces latency in queue operations, contributing to the overall reliability and speed of software that relies heavily on queue structures .

In the provided Java function for dequeuing, the if condition checks whether the queue is empty by evaluating if the count of elements is zero (`if(count == 0)`). This condition is necessary to prevent an underflow error, which would occur if an attempt was made to dequeue from an empty queue. By checking this condition first, the function ensures that it only tries to remove an element if the queue is non-empty, thereby maintaining the integrity and safety of the data structure .

One potential drawback of using a circular queue is the complexity it introduces into queue management. Operations such as enqueue and dequeue require calculations for wrapping around based on the capacity, which can increase implementation complexity and potential for errors. Debugging circular queues can also be more challenging, as issues with pointer updating (for front and rear) are more likely. Additionally, determining the full or empty state requires careful tracking of front and rear positions, which can lead to errors such as false overflow or underflow scenarios if not managed correctly .

The provided Java code in CircularQueueDemo gives the output "6 6." After enqueueing elements 10 and 3, the variable `var` is set to the rear element, which is 3. After dequeuing, 10 is removed, and 6 is then enqueued. `var` is updated to the front element, which is 6, and this value is printed twice. The logic reflects that after the dequeue and enqueue operations, the front of the queue is the newly added element 6, demonstrating the expected behavior of a queue .

Overflow and underflow are two types of errors in queue data structures caused by boundary violations. Overflow occurs when an attempt is made to add an element to a queue that is already at full capacity, leading to a potential loss of data since the queue cannot accommodate more elements. Underflow occurs when an attempt is made to remove an element from an empty queue, which can lead to undefined behavior as there are no elements to retrieve. These errors highlight the importance of checking the queue's state before performing operations to prevent runtime errors .

In a circular queue, the increment of the rear is done using the formula (rear + 1) % CAPACITY. This formula ensures that once the end of the queue's array is reached, the rear wraps around to the beginning of the queue array if there is space, effectively creating a circular loop. This is important because it maximizes the utilization of allocated space, reduces the need for shifting elements, and avoids unnecessary overflow errors that are common in linear queues where the rear simply increments linearly until the maximum array size is reached .

The time complexity of the enqueue operation in a queue implemented using arrays is O(1). This is because the enqueue operation adds an element directly to the rear of the array without requiring any shifts or iterations over the array elements, provided the queue is not full. This operation is a standard, constant-time update to the data structure .

You might also like