Priority Queue | Data Structure MCQs

  • 1. With what data structure can a priority queue be implemented?
   A.) Array
   B.) List
   C.) Heap
   D.) All of these

Answer: Option 'D'

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.

  • 2. Select the appropriate code that inserts elements into the list based on the given key value.
    (head and trail are dummy nodes to mark the end and beginning of the list, they do not contain any priority or element)
   A.)
public void insert_key(int key,Object item) 
{
	if(key<0)
	{
		Systerm.our.println("invalid");
		System.exit(0);
	}
	else
	{
		Node temp = new Node(key,item,null);
		if(count == 0)
		{
			head.setNext(temp);
			temp.setNext(trail);
		}
		else
		{
			Node dup = head.getNext();
			Node cur = head;
			while((key>dup.getKey()) && (dup!=trail))
			{
				dup = dup.getNext();
				cur = cur.getNext();
			}
			cur.setNext(temp);
			temp.setNext(dup);
		}
		count++;
	}
}
   B.)
public void insert_key(int key,Object item) 
{
	if(key<0)
	{
		Systerm.our.println("invalid");
		System.exit(0);
	}
	else
	{
		Node temp = new Node(key,item,null);
		if(count == 0)
		{
			head.setNext(temp);
			temp.setNext(trail);
		}
		else
		{
			Node dup = head.getNext();
			Node cur = dup;
			while((key>dup.getKey()) && (dup!=trail))
			{
				dup = dup.getNext();
				cur = cur.getNext();
			}
			cur.setNext(temp);
			temp.setNext(dup);
		}
		count++;
	}
}
   C.)
public void insert_key(int key,Object item) 
{
	if(key<0)
	{
		Systerm.our.println("invalid");
		System.exit(0);
	}
	else
	{
		Node temp = new Node(key,item,null);
		if(count == 0)
		{
			head.setNext(temp);
			temp.setNext(trail);
		}
		else
		{
			Node dup = head.getNext();
			Node cur = head;
			while((key>dup.getKey()) && (dup!=trail))
			{
				dup = dup.getNext();
				cur = cur.getNext();
			}
			cur.setNext(dup);
			temp.setNext(cur);
		}
		count++;
	}
}
   D.) None of these

Answer: Option 'A'

Have two temporary pointers ‘dup’ and ‘cur’ with ‘cur’ trailing behind ‘dup’. Traverse through the list until the given key is greater than some element with a lesser key, insert the new node ‘temp’ in that position.

  • 3. Which of the following is not an application of priority queue?
   A.) Huffman codes
   B.) Interrupt handling in operating system
   C.) Undo operation in text editors
   D.) Bayesian spam filter

Answer: Option 'C'

Undo operation is achieved using a stack.

  • 4. What is the time complexity to insert a node based on key in a priority queue?
   A.) O(nlogn)
   B.) O(logn)
   C.) O(n)
   D.) O(n2)

Answer: Option 'D'

In the worst case, you might have to traverse the entire list.

  • 5. What is the functionality of the following piece of code?
    public Object delete_key() 
    {
    	if(count == 0)
    	{
    		System.out.println("Q is empty");
    		System.exit(0);
    	}
    	else
    	{
    		Node cur = head.getNext();
    		Node dup = cur.getNext();
    		Object e = cur.getEle();
    		head.setNext(dup);
    		count--;
    		return e;
    	}
    }
   A.) Delete the second element in the list
   B.) Return but not delete the second element in the list
   C.) Delete the first element in the list
   D.) Return but not delete the first element in the list

Answer: Option 'C'

A pointer is made to point at the first element in the list and one more to point to the second element, pointer manipulations are done such that the first element is no longer being pointed by any other pointer, its value is returned.

  • 6. What is not a disadvantage of priority scheduling in operating systems?
   A.) A low priority process might have to wait indefinitely for the CPU
   B.) If the system crashes, the low priority systems may be lost permanently
   C.) Interrupt handling
   D.) None of these

Answer: Option 'C'

​Interrupt handling
It is in fact an advantage, interrupts should be given more priority than the task at hand so that the interrupt can be serviced.

  • 7. What are the advantages of priority queues?
   A.) Easy to implement
   B.) Processes with different priority can be efficiently handled
   C.) Applications with differing requirements
   D.) All of these

Answer: Option 'D'

All of these properties of priority queue help in achieving its applications like Huffman coding, priority scheduling and the like.

  • 8. What is the time complexity to insert a node based on position in a priority queue?
   A.) O(nlogn)
   B.) O(logn)
   C.) O(n)
   D.) O(n2)

Answer: Option 'C'

In the worst case, you might have to traverse the entire list.