Section 13 · 29 questions

Immutability

29 interview questions and answers in the Immutability section.

English Immutability Source Markdown
Language versions: English Russian Ukrainian

Questions in this section

  1. What is an immutable (unchangeable) object?
  2. What advantages do immutable objects provide?
  3. How to create an immutable class in Java?
  4. Why is the String class immutable?
  5. What are the consequences of String immutability?
  6. Why are immutable objects thread-safe?
  7. What is the final keyword and how does it help create immutable classes?
  8. Is it enough to make all fields final for immutability?
  9. What to do if a class field of an immutable class references a mutable object?
  10. What is a defensive copy?
  11. When should you make a defensive copy?
  12. How to protect a collection from modification?
  13. What is Collections.unmodifiableList() and how does it work?
  14. What is the difference between shallow copy and deep copy?
  15. Can you inherit from an immutable class?
  16. Why should an immutable class be final?
  17. What happens if you override getter in subclass of immutable class?
  18. How does String pool work and how is it related to immutability?
  19. Can you change String value via reflection?
  20. What is Record and how does it help create immutable classes?
  21. Why are LocalDate, LocalDateTime immutable?
  22. What are the advantages of immutable objects for caching?
  23. How does immutability affect performance?
  24. What are persistent data structures?
  25. Are there disadvantages to immutable objects?
  26. How to implement Builder pattern for an immutable class?
  27. Can you use immutable objects as keys in HashMap?
  28. What happens if you modify a mutable key in HashMap?
  29. How to properly work with collections in immutable classes?

Study navigator

29 questions for Middle Java Developer interview preparation.


All Questions

# Question Difficulty
1 What is an immutable object Basic
2 What advantages do immutable objects provide Basic
3 How to create an immutable class in Java Intermediate
4 Why is String class immutable Intermediate
5 What are the consequences of String immutability Intermediate
6 Why are immutable objects thread-safe Intermediate
7 What is the final keyword and how does it help Intermediate
8 Is it enough to make all fields final for immutability Intermediate
9 What to do if a class field references a mutable object Advanced
10 What is a defensive copy Intermediate
11 When should you make a defensive copy Intermediate
12 How to protect a collection from modification Intermediate
13 What is Collections.unmodifiableList() and how does it work Intermediate
14 What is the difference between shallow copy and deep copy Intermediate
15 Can you inherit from an immutable class Intermediate
16 Why must an immutable class be final Intermediate
17 What happens if you override a getter in a subclass Advanced
18 How does String pool work and how is it related to immutability Intermediate
19 Can you change a String value via reflection Advanced
20 What is a Record and how does it help create immutable classes Intermediate
21 Why are LocalDate and LocalDateTime immutable Intermediate
22 What advantages do immutable objects have for caching Advanced
23 How does immutability affect performance Advanced
24 What are persistent data structures Advanced
25 Are there disadvantages to immutable objects Intermediate
26 How to implement the Builder pattern for an immutable class Intermediate
27 Can you use immutable objects as keys in HashMap Intermediate
28 What happens if you mutate a mutable key in HashMap Advanced
29 How to properly work with collections in immutable classes Advanced

Topic Dependency Map

                    ┌──────────────────────────────────────────┐
                    │   IMMUTABILITY BASICS (1-3)              │
                    │   1. What is immutable                   │
                    │   2. Advantages                          │
                    │   3. How to create                       │
                    └──────────────────┬───────────────────────┘
                                       │
            ┌──────────────────────────┼──────────────────────────┐
            ▼                          ▼                          ▼
    ┌───────────────┐        ┌───────────────┐        ┌────────────────────┐
    │ STRING & DATE  │        │ FINAL &        │        │ DEFENSIVE COPY     │
    │ (4-5, 18, 21)  │        │ INHERITANCE    │        │ & COPYING          │
    │ 4. String immut│        │ (7-8, 15-17)   │        │ (9-14, 29)         │
    │ 5. Consequences│        │ 7. final       │        │ 9. Mutable fields  │
    │ 18. String pool│        │ 8. All final?  │        │ 10. Defensive copy │
    │ 21. LocalDate  │        │ 15. Inheritance│        │ 11. When to copy   │
    │                │        │ 16. Final class│        │ 12. Protect coll.  │
    └───────┬───────┘        │ 17. Getter     │        │ 13. unmodifiable   │
            │                └───────┬───────┘        │ 14. Shallow vs Deep│
            │                        │                │ 29. Collections      │
            └────────────────────────┼────────────────┴────────┬───────────┘
                                     │                        │
            ┌────────────────────────┼────────────────────────┘
            ▼                        ▼
    ┌───────────────┐        ┌───────────────┐
    │ THREAD-SAFE    │        │ ADVANCED       │
    │ & CACHING      │        │ (19-20, 24-28) │
    │ (6, 22-23)     │        │ 19. Reflection │
    │ 6. Thread-safe │        │ 20. Record     │
    │ 22. Caching    │        │ 24. Persistent │
    │ 23. Perform.   │        │ 25. Disadvant. │
    │                │        │ 26. Builder    │
    │                │        │ 27. HashMap key│
    │                │        │ 28. Mutable mut.│
    └───────────────┘        └─────────────────┘

Level: Basic (Weeks 1-2)

Step Topic Files Goal
1 Basics Q1, Q2 What is immutable, why it matters
2 Creating Q3 How to create an immutable class
3 final Q7 Three contexts of final
4 String Q4, Q5 Why String is immutable, consequences
5 Thread-safe Q6 Why immutable = thread-safe

Level: Intermediate (Weeks 3-4)

Step Topic Files Goal
1 Defensive copy Q9, Q10, Q11 Copying mutable fields
2 Collections Q12, Q13, Q29 unmodifiableList, List.copyOf
3 Copy types Q14 Shallow vs deep copy
4 Builder Q26 Pattern for complex immutable objects
5 Record Q20 Record vs Lombok @Value vs final class
6 LocalDate Q21 Why Java Time is immutable

Level: Advanced (Weeks 5-6)

Step Topic Files Goal
1 Reflection attack Q19 Can you break immutability, module system
2 Inheritance Q15, Q16, Q17 Inheritance, sealed classes, overridden getters
3 Caching Q22 Why immutable objects are ideal for caching
4 Performance Q23 Trade-offs, allocation vs contention
5 Persistent DS Q24 Structural sharing, Vavr
6 HashMap keys Q27, Q28 Immutable keys, mutable key mutation bug

Key Topic Connections

Topic: Basics

Q1 (Immutable) → Q2 (Advantages) → Q3 (How to create)

Topic: final and inheritance

Q7 (final) → Q8 (All final?) → Q15 (Inheritance) → Q16 (Final class) → Q17 (Getter)

Key connections:

  • Q7 ↔ Q8: final fields ≠ immutable class (mutable references)
  • Q15 ↔ Q16: final class prohibits inheritance, sealed — restricts
  • Q17 ↔ Q29: Getter returns a mutable object — violation of immutable contract

Topic: Defensive Copy and collections

Q9 (Mutable fields) → Q10 (Defensive copy) → Q11 (When to copy)
     ↓
Q12 (Protect collection) → Q13 (unmodifiableList) → Q14 (Shallow vs Deep) → Q29 (Collections)

Topic: String and Date

Q4 (String immutable) → Q5 (Consequences) → Q18 (String Pool)
Q21 (LocalDate immutable)

Topic: Advanced

Q19 (Reflection) → Q20 (Record) → Q24 (Persistent DS)
Q27 (HashMap keys) → Q28 (Mutable key mutation)

Quick Reference: What to Know at Each Level

Basic

  • Immutable = state doesn’t change after creation
  • Advantages: thread-safe, cacheable, predictable
  • final for class, fields, methods — three different meanings
  • String immutable — security, String Pool, hashing

Intermediate

  • Defensive copy: copy mutable fields in constructor and getters
  • List.copyOf() vs Collections.unmodifiableList() — independent copy vs live wrapper
  • Shallow copy = copies container, deep copy = recursively entire graph
  • Record = immutable value class with minimal boilerplate
  • Builder pattern for objects with 4+ fields

Advanced

  • Reflection on final fields is blocked (Java 16+, JEP 396)
  • Sealed classes (Java 17) as an alternative to final for immutable hierarchies
  • Persistent data structures: structural sharing, O(log n) modification
  • Mutable key in HashMap = logical inaccessibility (not a leak!)
  • Immutable objects = linear scaling on read-heavy workloads
  • String Pool is tied to immutability: one object → many references safely

Format of Each File

Each file contains:

  • Basic — fundamental understanding, simple analogies, examples
  • Intermediate — internals, common mistakes, practical examples
  • Advanced — deep dive, edge cases, production experience, monitoring
  • Interview Cheat Sheet — key points, frequent questions, red flags, related topics