A

Admin • 828.03K Points
Coach

Q. What will be the output of the following code

Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Employee implements Comparator{
String name;
String id;
public Employee()
{}
public Employee(String name, String id)
{
this.name=name;
this.id=id;
}
@Override
public int compare(Employee obj1, Employee obj2) {
return obj2.name.compareTo(obj1.name);
}
@Override
public String toString() {
return “{“+”name “+name+”, id ” + id+’}’;
}
}
public class ComparatorUsage {
public static void main(String args[])
{
Employee emp1=new Employee(“sam”,”4″);
Employee emp2=new Employee(“amy”,”2″);
ArrayList<Employee> list=new ArrayList<Employee>();
list.add(emp1);
list.add(emp2);
Collections.sort(list,new Employee());
System.out.println(list);
}

}
  • (A) [2, 3]
  • (B) Runtime Exception
  • (C) Compile time exception
  • (D) None of the above
  • Correct Answer - Option(C)
  • Views: 9
  • Filed under category Java
  • Hashtags:

No solution found for this question.
Add Solution and get +2 points.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.