Java-Quiz: HashSet

This Java quiz covers HashSet and its relatives LinkedHashSet and TreeSet. You will predict what add() returns, how sets treat your own classes and records, how set operations work and why changing an element inside a set breaks it.

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

What does this print?

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<String> s = new HashSet<>();
        System.out.println(s.add("a") + " " + s.add("a") + " " + s.size());
    }
}
Lösung zeigen

B. true false 1 add() returns true only when the element was not already in the set. The second a is a duplicate, so it returns false and the size stays 1.

Darum geht es in diesem Quiz

  • add() returns false for duplicates
  • How equals() and hashCode() define duplicates
  • Records as set elements
  • retainAll() and other set operations
  • HashSet vs LinkedHashSet vs TreeSet

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.