Make your own free website on Tripod.com
1-

import java.util.*;

class DemoPQ {
	public static void main(String[] args) {
		PriorityQueue<String> pq = new PriorityQueue<String>();
		pq.add("Ravi");
		pq.add("Kumar");
		pq.add("Sumit");
		pq.add("Vineet");
		pq.add("Amit");

		for(String s:pq) {
			System.out.print(pq.peek() + ", " + pq.poll());

		}	
	}

}

A: Prints: Ravi, Kumar, Sumit, Vineet, Amit followed by Runtime exception
B: Prints: Amit, Kumar, Ravi, Sumit, Vineet followed by Runtime Exception
C: Prints: Amit, Kumar, Ravi, Sumit, Vineet
D: Prints: Ravi, Kumar, Sumit, Vineet, Amit
E: D: Compile time error
F: Prints: Amit, followed by Runtime exception
G: Prints: Amit, Gurmeet followed by Runtime exception
H: Run time exception

2-

import java.util.*;

public class DemoPQ { 

	public static void main(String[] args) {
		PriorityQueue<String> pq = new PriorityQueue<String>();
		pq.add("Sumit");
		pq.add("Amit");
		pq.add("Vineet");
		pq.add("Gurmeet");
		pq.add("Harbhjan");
		
		for(String s : pq) {
			System.out.println(s);
		}
	}
}

A: Prints: Sumit, Amit, Vineet, Gurmeet, Harbhajan
B: Prints: Amit, Gurmeet, Harbhajan, Sumit, Vineet  
C: Output comes but order is not deterministic
D: Compilation fails, error in for loop
E: Run time exception

3- 
import java.util.*;

class DemoPQ {
	public static void main(String[] args) {
		PriorityQueue<String> pq = new PriorityQueue<String>();
		pq.add("Ravi");
		pq.add("Kumar");
		pq.add("Sumit");
		pq.add("Vineet");
		pq.add("Amit");

		for(String s:pq) {
			System.out.print(pq.poll() + ", " + pq.poll() + ", " + pq.poll() + ", " 				+ pq.poll() + ", " + pq.poll());

		}	
	}

}

A: Prints: Ravi, Kumar, Sumit, Vineet, Amit followed by Runtime exception
B: Prints: Amit, Kumar, Ravi, Sumit, Vineet followed by Runtime Exception
C: Prints: Amit, Kumar, Ravi, Sumit, Vineet
D: Prints: Ravi, Kumar, Sumit, Vineet, Amit
E: D: Compile time error
F: Prints: Amit, followed by Runtime exception


4-

Little tricky:

import java.util.*;

class DemoPQ {
	public static void main(String[] args) {
		PriorityQueue<String> pq = new PriorityQueue<String>();
		pq.add("Ravi");
		pq.add("Kumar");
		pq.add("Sumit");
		pq.add("Vineet");
		pq.add("Amit");

		for(String s:pq) {
			System.out.print(pq.poll() + ", " + pq.poll() + ", " + pq.poll() + ", " 				+ pq.poll());

		}	
	}

}
A: Prints: Ravi, Kumar, Sumit, Vineet followed by Runtime exception
B: Prints: Amit, Kumar, Ravi, Sumit followed by Runtime Exception
C: Prints: Amit, Kumar, Ravi, Sumit
D: Prints: Ravi, Kumar, Sumit, Vineet
E: D: Compile time error

5- import java.util.*;

public class DemoPQ1 {

	public static void main(String[] args) {
		PriorityQueue<String> pq = new PriorityQueue<String>();
		pq.add("Mika");
		pq.add("Daler");
		pq.add("Mehndi");
		pq.add("Himesh");
		
		while(pq.size()!=0) {
			System.out.println(pq.peek());
			pq.poll();
		}
		
	}

}
A: Compiles and output comes but order can't be determined.
B: Prints: Mika, Daler, Mehndi, Himesh
C: Prints: Daler, Himesh, Mehndi, Mika
D: Prints Daler follwed by ConcurrentModificationException exception
E: PriorityQueue doesn't have size() method, so compilation fails

6- 
Little tricky:

import java.util.*;

class DemoPQ {
	public static void main(String[] args) {
		PriorityQueue<String> pq = new PriorityQueue<String>();
		pq.add("Ravi");
		pq.add("Kumar");
		pq.add("Sumit");
		pq.add("Vineet");
		pq.add("Amit");

		for(String s:pq) {
			System.out.print(pq.poll() + ", " + pq.poll() + ", " + pq.poll() + ", " 				+ pq.poll());
			pq.offer("XXX");

		}	
	}

}
A: Prints: Ravi, Kumar, Sumit, Vineet followed by Runtime exception
B: Prints: Amit, Kumar, Ravi, Sumit followed by Runtime Exception
C: Prints: Amit, Kumar, Ravi, Sumit
D: Prints: Ravi, Kumar, Sumit, Vineet
E: D: Compile time error