21 / 42
Consider you have a stack whose elements in it are as follows.
5 4 3 2 << top
Where the top element is 2.
You need to get the following stack
6 5 4 3 2 << top
The operations that needed to be performed are (You can perform only push and pop):
APush(pop()), push(6), push(pop())
BPush(pop()), push(6)
CPush(pop()), push(pop()), push(6)
DPush(6)
Answer: Option A
Explanation:By performing push(pop()) on all elements on the current stack to the next stack you get 2 3 4 5 << top.Push(6) and perform push(pop()) you’ll get back 6 5 4 3 2 << top. You have actually performed enQueue operation using push and pop.
Workspace
22 / 42
Given only a single array of size 10 and no other memory is available. Which of the following operation is not feasible to implement (Given only push and pop operation)?
APush
BPop
CEnqueue
DReturntop
Answer: Option C
Explanation:To perform Enqueue using just push and pop operations, there is a need of another array of same size. But as there is no extra available memeory, the given operation is not feasible.
Workspace
23 / 42
Given an array of size n, let’s assume an element is ‘touched’ if and only if some operation is performed on it(for example, for performing a pop operation the top element is ‘touched’). Now you need to perform Dequeue operation. Each element in the array is touched atleast?
AOnce
BTwice
CThrice
DFour times
Answer: Option D
Explanation:First each element from the first stack is popped, then pushed into the second stack, dequeue operation is done on the top of the stack and later the each element of second stack is popped then pushed into the first stack. Therfore each element is touched four times.
Workspace
24 / 42
To implement a stack using queue(with only enqueue and dequeue operations), how many queues will you need?
A1
B2
C3
D4
Answer: Option B
Explanation:Either the push or the pop has to be a costly operation, and the costlier operation requires two queues.
Workspace
25 / 42
Making the push operation costly, select the code snippet which implements the same.(let q1 and q2 be two queues)
I. public void push(int x)
{
if(empty())
{
q1.offer(x);
}
else{
if(q1.size()>0)
{
q2.offer(x);
int size = q1.size();
while(size>0)
{
q2.offer(q1.poll());
size--;
}
}
else if(q2.size()>0)
{
q1.offer(x);
int size = q2.size();
while(size>0)
{
q1.offer(q2.poll());
size--;
}
}
}
}
II. public void push(int x)
{
if(empty())
{
q1.offer(x);
}
else
{
if(q1.size()>0)
{
q1.offer(x);
int size = q1.size();
while(size>0)
{
q2.offer(q1.poll());
size--;
}
}
else if(q2.size()>0)
{
q2.offer(x);
int size = q2.size();
while(size>0)
{
q1.offer(q2.poll());
size--;
}
}
}
}
III. public void push(int x)
{
if(empty())
{
q1.offer(x);
}
else
{
if(q1.size()>0)
{
q2.offer(x);
int size = q1.size();
while(size>0)
{
q1.offer(q2.poll());
size--;
}
}
else if(q2.size()>0)
{
q1.offer(x);
int size = q2.size();
while(size>0)
{
q2.offer(q1.poll());
size--;
}
}
}
}
IV. public void push(int x)
{
if(empty())
{
q1.offer(x);
}
else
{
if(q1.size()>0)
{
q2.offer(x);
int size = q1.size();
while(size>0)
{
q2.offer(q2.poll());
size--;
}
}
else if(q2.size()>0)
{
q1.offer(x);
int size = q2.size();
while(size>0)
{
q2.offer(q1.poll());
size--;
}
}
}
}
AI
BII
CIII
DIV
Answer: Option A
Explanation:Stack follows LIFO principle, hence a new item added must be the first one to exit, but queue follows FIFO principle, so when a new item is entered into the queue, it will be at the rear end of the queue. If the queue is initially empty, then just add the new element, otherwise add the new element to the second queue and dequeue all the elements from the second queue and enqueue it to the first one, in this way, the new element added will be always in front of the queue. Since two queues are needed to realize this push operation, it is considered to be costlier.
Workspace
26 / 42
Making the push operation costly, select the code snippet which implements the pop operation.
I. public void pop()
{
if(q1.size()>0)
{
q2.poll();
}
else if(q2.size()>0)
{
q1.poll();
}
}
II. public void pop()
{
if(q1.size()>0)
{
q1.poll();
}
else if(q2.size()>0)
{
q2.poll();
}
}
III. public void pop()
{
q1.poll();
q2.poll();
}
IV. public void pop()
{
if(q2.size()>0)
{
q1.poll();
}
else
{
q2.poll();
}
}
AI
BII
CIII
DIV
Answer: Option B
Explanation:As the push operation is costly, it is evident that the required item is in the front of the queue, so just dequeue the element from the queue.
Workspace
27 / 42
Select the code snippet which returns the top of the stack.
I. public int top()
{
if(q1.size()>0)
{
return q1.poll();
}
else if(q2.size()>0)
{
return q2.poll();
}
return 0;
}
II. public int top()
{
if(q1.size()==0)
{
return q1.peek();
}
else if(q2.size()==0)
{
return q2.peek();
}
return 0;
}
III. public int top()
{
if(q1.size()>0)
{
return q1.peek();
}
else if(q2.size()>0)
{
return q2.peek();
}
return 0;
}
IV. public int top()
{
if(q1.size()>0)
{
return q2.peek();
}
else if(q2.size()>0)
{
return q1.peek();
}
return 0;
}
AI
BII
CIII
DIV
Answer: Option C
Explanation:Assuming its a push costly implementation, the top of the stack will be in the front end of the queue, note that peek() just returns the front element, while poll() removes the front element from the queue.
Workspace
28 / 42
With what data structure can a priority queue be implemented?
AArray
BList
CHeap
DTree
Answer: Option D
Explanation:Priority queue can be implemented using an array, a list, a binary search tree or a heap, although the most efficient one being the heap.
Workspace
29 / 42
What is not a disadvantage of priority scheduling in operating systems?
AA low priority process might have to wait indefinitely for the CPU
BIf the system crashes, the low priority systems may be lost permanently
CInterrupt handling
DIndefinite blocking
Answer: Option C
Explanation:The lower priority process should wait until the CPU completes the processing higher priority process. Interrupt handling is an advantage as interrupts should be given more priority than tasks at hand so that interrupt can be serviced to produce desired results.
Workspace
30 / 42
Which of the following is not an advantage of priority queue?
AEasy to implement
BProcesses with different priority can be efficiently handled
CApplications with differing requirements
DEasy to delete elements in any case
Answer: Option D
Explanation:In worst case, the entire queue has to be searched for the element having highest priority. This will take more time than usual. So deletion of elements is not an advantage.
Workspace
In this practice section, you can practice Data Structures Questions based on "Queue" and improve your skills in order to face the interview, competitive examination, IT companies Written exam, and various other entrance tests (CAT, GATE, GRE, MAT, Bank Exam, Railway Exam etc.) with full confidence.
Q4Interview provides you lots of fully solved Data Structures (Queue) questions and answers with Explanation. Solved examples with detailed answer description, explanation are given and it would be easy to understand. You can download Data Structures Queue quiz questions with answers as PDF files and eBooks.
Here you can find objective type Data Structures Queue questions and answers for interview and entrance examination. Multiple choice and true or false type questions are also provided.