Quiz 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.

Trung bình 10 câu hỏi ~6 phút Đạt từ 80% Java 17

Chọn chế độ

Bạn đang chơi với tư cách khách: đăng nhập để lưu XP và streak.

Câu hỏi mẫu

Câu 1/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);
    }
}
Xem đáp án

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 ==.

Quiz này có gì

  • == (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

Câu hỏi thường gặp

Có cần tài khoản không?

Không. Ai cũng chơi được. Đăng nhập để lưu XP, streak, badge và lịch sử.

Có làm lại được không?

Được. Điểm cao nhất của bạn được giữ lại, câu hỏi và đáp án được xáo trộn mỗi lần.