Quiz Java: HashMap

This Java quiz tests how HashMap really behaves. You will predict what put(), get(), getOrDefault(), merge() and computeIfAbsent() return, see why keys need both equals() and hashCode(), and handle null values safely.

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.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> m = new HashMap<>();
        System.out.println(m.put("a", 1) + " " + m.put("a", 2) + " " + m.get("a"));
    }
}
Xem đáp án

C. null 1 2 put() returns the value previously stored for the key: null the first time, then 1. The second put() replaces the value, so get() returns 2.

Quiz này có gì

  • put() returns the old value, get() returns null when missing
  • Counting and grouping with merge() and computeIfAbsent()
  • Keys need matching equals() and hashCode()
  • null values, containsKey() and unboxing
  • Iteration order: HashMap, LinkedHashMap, TreeMap

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.