🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Comparable vs Comparator: all 10 questions

Quiz: Comparable vs Comparator · Question 10 of 10

You cannot change a library class but need to sort its objects by a field. What do you use?

You cannot change a library class but need to sort its objects by a field. What do you use?

  1. A.Make the class implement Comparable
  2. B.Write a Comparator outside the class
  3. C.Override equals
  4. D.Use a HashSet
Answer & explanation

Correct answer: Write a Comparator outside the class

A Comparator sorts any class without changing its source code.

Can you get the rest right without peeking?

Take the timed Quiz: Comparable vs Comparator quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →