Data Structure Questions and Answers-Stack using Queues

YOU CAN DOWNLOAD 200+ SUBJECTS PDF BOOK FOR COMPETITIVE EXAMINATIONS

CLICK HERE TO DOWNLOAD

Data Structure Questions and Answers-Stack using Queues

Question 1 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
To implement a stack using queue(with only enqueue and dequeue operations), how many queues will you need?
A
1
B
2
C
3
D
4
Question 1 Explanation: 
Either the push or the pop has to be a costly operation, and the costlier operation requires two queues.

Question 2 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Making the push operation costly, select the code snippet which implements the same.(let q1 and q2 be two queues)
A

public void push(int x) { if(empty()) { q1.offer(x); } else{ if(q1.size()>0
B

public void push(int x) { if(empty()) { q1.offer(x); } else { if(q1.size()>0
C

public void push(int x) { if(empty()) { q1.offer(x); } else { if(q1.size()>0
D
None of the mentioned
Question 2 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.

Question 3 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Making the push operation costly, select the code snippet which implements the pop operation.
A

public void pop() { if(q1.size()>0) { q2.poll(); } else if(q2.size()
public void pop() { if(q1.size()>0) { q1.poll(); } else if(q2.size()>
C

public void pop() { q1.poll(); 	q2.poll(); }
D
None of the mentioned

Question 3 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.

Question 4 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Select the code snippet which returns the top of the stack.
A

public int top() { if(q1.size()>0) { return q1.poll(); } else if(q2.size()
B

public int top() { if(q1.size()==0) { return q1.peek(); } else if(q2.size
C

public int top() { if(q1.size()>0) { return q1.peek(); } else if(q2.size()
D
None of the mentioned

Question 4 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.

Question 5 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Select the code snippet which return true if the stack is empty, false otherwise.
A

public boolean empty() { return q2.isEmpty(); }
B

public boolean empty() { return q1.isEmpty() || q2.isEmpty(); }
C

public boolean empty() { return q1.isEmpty(); }
D

public boolean empty() { return q1.isEmpty() & q2.isEmpty(); }
Question 5 Explanation: 
If both the queues are empty, then the stack also is empty.

There are 5 questions to complete.