Analyze the following code. 1. public class Test { 2. public static void main(String[] args) { 3. Fruit[] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)}; 4. java.util.Arrays.sort(fruits); 5. } 6. } class Fruit { private double weight; public Fruit(double weight) { this.weight = weight; } }

Analyze the following code.


1. public class Test  {2.   public static void main(String[] args) {3.     Fruit[] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)};4.     java.util.Arrays.sort(fruits);5.   }6. }
class Fruit {  private double weight;    public Fruit(double weight) {    this.weight = weight;  }}


A. The program has a compile error because the Fruit class does not have a default constructor.
B. The program has a runtime error on Line 3 because the Fruit class does not have a default constructor.
C. The program has a compile error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.
D. The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.

The correct answer is D

Explanation: (A) and (B) are incorrect since it is OK to define a class without a default constructor. (C) is incorrect since it is OK to pass fruits to Arrays.sort(Object[]) without compile errors. (D) is correct because the Arrays.sort method requires the objects in the array to be comparable and their class must implement the java.lang.Comparable interface.