Quiz di Java: 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.

Medio 10 domande ~6 minuti Superato con il 80% Java 17

Scegli una modalità

Stai giocando come ospite: accedi per salvare XP e serie.

Domanda di esempio

Domanda 1 di 10

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

Mostra la risposta

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.

Cosa copre questo 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

Domande frequenti

Serve un account?

No. Chiunque può giocare. Accedi per salvare XP, serie, badge e cronologia.

Posso rifarlo?

Sì. Il tuo miglior punteggio resta salvato, e domande e risposte vengono mescolate ogni volta.