Quiz di Java: String Pool & Immutability

This Java quiz tests one of the most asked interview topics, the string pool and string immutability. You will predict what == and equals() print for literals, new String(), intern() and compile-time constants, and spot the code that throws.

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

What does this print?

public class Main {
    public static void main(String[] args) {
        String a = "hi";
        String b = "hi";
        System.out.println(a == b);
    }
}
Mostra la risposta

B. true Identical string literals are stored once in the string pool, so a and b point to the same object and == is true. That is an implementation detail of literals, not a reason to compare strings with ==.

Cosa copre questo quiz

  • == (same object) vs equals() (same text)
  • String literals and the string pool
  • new String() and intern()
  • Compile-time constant concatenation and final
  • Why String methods return new strings

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.