🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← All topics

Quiz: Comparable vs Comparator: questions with answers

10 questions, each with the answer and a short explanation.

Take the timed quiz + get a certificate →
  1. Q1. What does a negative return value from compare(a, b) mean?
  2. Q2. What does this print? (code)
  3. Q3. Which interface would you implement INSIDE a class to give it a natural order?
  4. Q4. Why is return a.price - b.price; a risky compare method for int prices?
  5. Q5. What does this print? (code)
  6. Q6. comparing(Student::name).thenComparing(Student::marks).reversed() sorts by:
  7. Q7. How do you sort a List<String containing nulls with nulls at the end?
  8. Q8. What does this print? (code)
  9. Q9. Which statement about List.sort is true?
  10. Q10. You cannot change a library class but need to sort its objects by a field. What do you use?