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