Java Comparator vs Comparable

Last Updated :
Discuss
Comments

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 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 4

Which of the following correctly implements a Comparator for sorting integers in descending order?

  • Java
    class DescendingComparator implements Comparator<Integer> {
        public int compare(Integer a, Integer b) {
            return a - b;
        }
    }
    


  • Java
    class DescendingComparator implements Comparator<Integer> {
        public int compare(Integer a, Integer b) {
            return b - a;
        }
    }
    


  • Java
    class DescendingComparator implements Comparator<Integer> {
        public int compare(Integer a, Integer b) {
            return 0;
        }
    }
    


  • Java
    class DescendingComparator implements Comparator<Integer> {
        public boolean compare(Integer a, Integer b) {
            return a < b;
        }
    }
    


Question 5

What will be the output of the following code?

Java
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 6

If a class implements Comparable, which method must it override?

  • equals(Object obj)

  • compareTo(Object o)

  • compare(Object o1, Object o2)

  • hashCode()

Question 7

Which statement is TRUE about Comparator in Java?

  • A comparator is used to define the natural ordering of objects.

  • Comparator is implemented inside the class itself.

  • Comparator can be used to define multiple sorting orders.

  • Comparator can only sort Strings.

Question 8

In Comparable, the compareTo() method should return:

  • Positive integer if current object is less than the specified object


  • Zero if current object is greater

  • Negative integer if current object is less

  • Positive integer if current object is greater

There are 8 questions to complete.

Take a part in the ongoing discussion