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.
The monocotyledonous plants can be distinguished from dicotyledonous plants by:
Which of the following is not a stem modification?
Which of the following cucurbits is richest in iron content?
Pea, gulmohur, bean, Cassia have ___ type of symmetry and hance they are ____ flowers.
The organelle responsible for protein synthesis in plant cells is the:Â
When the leaflets are joined together at a common point at the petiole, the type of leaf is
Photosynthesis occurs in which cell organelle of the plant?Â
In monocotyledonous seeds the outer covering of endosperm separates the embryo by a proteinous layer called as
Pungency in radish is due to the presence of
Which of these green manuring crop is stem nodulating?