Question
Consider the following Java code snippet: Â Â
import java.util.PriorityQueue; Â Â class CustomObject { Â Â Â Â int id; Â Â Â Â String name; Â Â Â Â public CustomObject(int id, String name) { Â Â Â Â Â Â this.id = id; Â Â Â Â Â Â this.name = name; Â Â Â Â } Â Â } Â Â public class HeapQuestion6 { Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â PriorityQueue pq = new PriorityQueue(); Â Â Â Â Â Â pq.add(new CustomObject(1, "Alice")); Â Â Â Â Â Â pq.add(new CustomObject(2, "Bob")); Â Â Â Â Â Â System.out.println(pq.poll().name); Â Â Â Â } Â Â } Â Â What will be the result of compiling and running this program?Solution
A PriorityQueue needs to know how to order its elements. For custom objects, there are two ways:   1. The custom class implements the Comparable interface, defining its natural ordering.   2. A Comparator is provided to the PriorityQueue constructor.   In this code, CustomObject does not implement Comparable, and no Comparator is provided. The Java compiler does not enforce this at compile time for PriorityQueue (it's a generic type parameter E extends Comparable super E> or a Comparator is needed). However, when the PriorityQueue attempts to compare two CustomObject instances (e.g., during add() operations to maintain the heap property), it will try to cast them to Comparable, which will fail, resulting in a java.lang.ClassCastException at runtime.
Who was a leading figure in the Narmada Bachao Andolan, a movement focused on the rights of people affected by dam constructions in India?
Which of the following organizations issues the rules of global trade?
Which country has the highest number of Ramsar wetland sites in Asia as of 2025?
Who among the following is an Indian Olympic archer and Padma Shri winner?
Which article of the Indian Constitution includes the term "Cabinet"?
The International Monetary Fund (IMF) was established in:
In which year did Subhash Chandra Bose join the Indian National Army (INA) in Singapore?
The purchase of one company by another company is called
Prime Minister will attend the annual DGP conference to be held in:
Euro the currency is not used by which European country?