Stack using Queues | Data Structure MCQs

  • 1. 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

Answer: Option 'B'

Either the push or the pop has to be a costly operation, and the costlier operation requires two queues.

  • 2. 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()>0)
        {
            q1.poll();
        }
}
   B.)
public void pop()
{
        if(q1.size()>0)
        {
            q1.poll();
        }
        else if(q2.size()>0)
        {
            q2.poll();
        }
}
   C.)
public void pop()
{
        q1.poll();
	q2.poll();
}
   D.) None of these

Answer: Option 'B'

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.

  • 3. 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)
                {
                    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--;
                }
            }
        }
    }
   B.)
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--;
                }
            }
        }
}
   C.)
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--;
                }
            }
        }
}
   D.) None of these

Answer: Option 'A'

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.

  • 4. 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()>0)
       {
            return q2.poll();
       }
       return 0;
}
   B.)
public int top()
{
       if(q1.size()==0)
       {
            return q1.peek();
       }
       else if(q2.size()==0)
       {
            return q2.peek();
       }
        return 0;
    }
   C.)
public int top()
{
       if(q1.size()>0)
       {
            return q1.peek();
       }
       else if(q2.size()>0)
       {
            return q2.peek();
       }
       return 0;
}
   D.) None of these

Answer: Option 'D'

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.

  • 5. 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();
}

Answer: Option 'D'

If both the queues are empty, then the stack also is empty.

  • 6. What is the functionality of the following piece of code?
    public void fun(int x)
    {
    	q1.offer(x);
    }
   A.) Perform push() with push as the costlier operation
   B.) Perform push() with pop as the costlier operation
   C.) Perform pop() with push as the costlier operation
   D.) Perform pop() with pop as the costlier operation

Answer: Option 'B'

offer() suggests that it is a push operation, but we see that it is performed with only one queue, hence the pop operation is costlier.

Stack using Queues | Data Structure MCQs Download Pdf