Java-Quiz: Comparable & Comparator

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.

Mittel 10 Fragen ~6 Minuten Bestanden ab 80% Java 17

Modus wählen

Du spielst als Gast: Melde dich an, damit XP und Serie gespeichert werden.

Beispielfrage

Frage 1 von 10

a.compareTo(b) returns a negative number. What does that mean?

Lösung zeigen

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.

Darum geht es in diesem Quiz

  • What compareTo() returns and what it means
  • Implementing Comparable for natural order
  • Comparator.comparing(), reversed(), thenComparing()
  • Sorting objects that are not Comparable
  • The a - b comparator bug and TreeSet duplicates

Häufige Fragen

Brauche ich ein Konto?

Nein. Jeder kann spielen. Mit Anmeldung werden XP, Serie, Abzeichen und Verlauf gespeichert.

Kann ich es wiederholen?

Ja. Dein Bestwert bleibt erhalten, und Fragen und Antworten werden jedes Mal neu gemischt.