Here are 10 essential multiple-choice questions on Java Comparator vs Comparable, covering key concepts.
Question 1
What is the primary difference between Comparable and Comparator?
Comparable is used for natural ordering, while Comparator is used for custom ordering
Comparator is faster than Comparable
Comparable allows multiple sorting logics, while Comparator allows only one
Comparator can only be used with primitive data types
Question 2
Which method must be implemented when using the Comparable interface?
compareTo(Object obj)
compare(Object obj1, Object obj2)
equals(Object obj)
hashCode()
Question 3
What is the return type of the compareTo() method in Comparable?
boolean
int
double
void
Question 4
What happens if two objects are equal according to compareTo()?
compareTo() should return 1
compareTo() should return -1
compareTo() should return 0
compareTo() should throw an exception
Question 5
Which interface allows defining multiple sorting orders in Java?
Comparable
Comparator
Serializable
Cloneable
Question 6
Given the following class, what will be the output?
import java.util.*;
class Employee implements Comparable<Employee> {
int id;
String name;
Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int compareTo(Employee e) {
return this.id - e.id;
}
public String toString() {
return id + " - " + name;
}
}
public class Test {
public static void main(String[] args) {
List<Employee> list = new ArrayList<>();
list.add(new Employee(3, "Alice"));
list.add(new Employee(1, "Bob"));
list.add(new Employee(2, "Charlie"));
Collections.sort(list);
System.out.println(list);
}
}
[3 - Alice, 1 - Bob, 2 - Charlie]
[1 - Bob, 2 - Charlie, 3 - Alice]
[3 - Alice, 2 - Charlie, 1 - Bob]
Compilation Error
Question 7
Which of the following statements about Comparator is true?
Comparator can only compare objects of the same type
Comparator must be implemented inside the class being compared
Comparator requires overriding compareTo() method
Comparator can be implemented as a lambda expression
Question 8
Which of the following correctly implements a Comparator for sorting integers in descending order?
class DescendingComparator implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return a - b;
}
}
class DescendingComparator implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return b - a;
}
}
class DescendingComparator implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return 0;
}
}
class DescendingComparator implements Comparator<Integer> {
public boolean compare(Integer a, Integer b) {
return a < b;
}
}
Question 9
What will be the output of the following code?
import java.util.*;
class Student {
int roll;
String name;
Student(int roll, String name) {
this.roll = roll;
this.name = name;
}
public String toString() {
return roll + " - " + name;
}
}
public class Test {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student(10, "Geek1"));
students.add(new Student(101, "Geek2"));
students.add(new Student(105, "Geek3"));
Collections.sort(students, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.roll - s2.roll;
}
});
System.out.println(students);
}
}
[10 - Geek1, 101 - Geek2, 105 - Geek3]
[105 - Geek3, 10 - Geek1, 101 - Geek2]
[101 - Geek2, 10 - Geek1, 105 - Geek3]
Compilation Error
Question 10
Which is the best choice when sorting objects based on multiple attributes dynamically?
Comparable
Comparator
Equals method
Serialization
There are 10 questions to complete.