Java-Quiz: equals() & hashCode()

This Java quiz goes deep into equals() and hashCode(), a favorite interview topic. You will predict how objects behave in a HashSet, catch an equals that overloads instead of overrides, and meet the Integer cache and records.

Schwer 10 Fragen ~8 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

Which hashCode() for a class with int fields x and y follows the contract but makes a HashMap with many keys very slow?

Lösung zeigen

C. return 42; A constant hash is legal, since equal objects still get equal hash codes, but every key lands in the same bucket and lookups degrade to scanning. Good hash codes spread different objects out.

Darum geht es in diesem Quiz

  • Reference equality vs equals()
  • The equals/hashCode contract
  • Why HashSet and HashMap need both methods
  • Mutable keys and overloaded equals
  • The Integer cache and records

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.