- Executors
-
Learn
Learn Python Runnable examples, NumPy, pandas, and more Learn AI / Machine LearningNeural networks, ML and LLMs from scratch Learn C & C++ Pointers, memory, STL, and modern C++ Learn C#C# basics, OOP, LINQ, async, and .NET exercises Learn MatplotlibLine, bar, scatter and pie charts with live visualizers Learn RustOwnership, traits, error handling, and threads Learn GoSlices, interfaces, goroutines, and channels Python Algorithms Search, sorting, recursion, and visualizers O(n) C & C++ Algorithms Compare algorithms implemented in C and C++ Coding for Kids Learn to code by playing!
- QuizzesNew
- ChallengesNew
- For TeachersNew
- Updates
- Buy me a coffee
Java Comparable & Comparator Quiz
This Java quiz covers sorting objects. You will read compareTo() results, build comparators with Comparator.comparing(), reversed() and thenComparing(), and find out what happens when a class has no natural order or a TreeSet thinks two values are equal.
Sample question
Question 1 of 10a.compareTo(b) returns a negative number. What does that mean?
Show the answer
D. a comes before b compareTo() returns a negative number if a sorts before b, zero if they are equal and a positive number if a sorts after. Only the sign matters, not the exact value.
What this quiz covers
- What
compareTo()returns and what it means - Implementing
Comparablefor natural order Comparator.comparing(),reversed(),thenComparing()- Sorting objects that are not
Comparable - The
a - bcomparator bug andTreeSetduplicates
Questions people ask
Do I need an account?
No. Anyone can play. Sign in to keep your XP, streak, badges and history.
Can I retake it?
Yes. Your best score is kept, and the questions and answers are shuffled each time.