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

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?

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());
    }
}
Xem đáp án

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.

Quiz này có gì

  • 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

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.