🔒 Section 13 · 29 questions

Immutability

29 interview questions and answers in the Immutability section.

Questions in this section

  1. What is an Immutable Object
  2. What Advantages Do Immutable Objects Provide
  3. How to Create an Immutable Class in Java
  4. Why is 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 Class Field References a Mutable Object
  10. What is Defensive Copy
  11. When Should You Make Defensive Copy
  12. How to Protect a Collection from Modification
  13. What is Collections.unmodifiableList() and How Does It Work
  14. 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 and 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

Comprehensive guide containing 29 core interview questions across Junior 🟢, Middle 🟡, and Senior 🔴 levels.


📋 All Questions

# Question Difficulty
01 What is an Immutable Object ⭐
02 What Advantages Do Immutable Objects Provide ⭐
03 How to Create an Immutable Class in Java ⭐⭐
04 Why is String Class Immutable ⭐⭐
05 What are the Consequences of String Immutability ⭐⭐
06 Why Are Immutable Objects Thread-Safe ⭐⭐
07 What is the final Keyword and How Does It Help Create Immutable Classes ⭐⭐
08 Is It Enough to Make All Fields final for Immutability ⭐⭐
09 What to Do If Class Field References a Mutable Object ⭐⭐⭐
10 What is Defensive Copy ⭐⭐
11 When Should You Make Defensive Copy ⭐⭐
12 How to Protect a Collection from Modification ⭐⭐
13 What is Collections.unmodifiableList() and How Does It Work ⭐⭐
14 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 and 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 ⭐⭐⭐

🗺️ Topic Dependency Map

                    ┌──────────────────────────────────────────┐
                    │       IMMUTABILITY BASICS (01-03)        │
                    │   01. What is Immutable                  │
                    │   02. Advantages                         │
                    │   03. How to Create                      │
                    └──────────────────┬───────────────────────┘
                                       │
            ┌──────────────────────────┼──────────────────────────┐
            ▼                          ▼                          ▼
    ┌───────────────┐        ┌───────────────┐        ┌────────────────────┐
    │ STRING & TIME │        │ FINAL &       │        │ DEFENSIVE COPYING  │
    │ (04-05,18,21) │        │ INHERITANCE   │        │ & COLLECTIONS      │
    │ 04. String    │        │ (07-08, 15-17)│        │ (09-14, 29)        │
    │ 05. Conseq.   │        │ 07. final     │        │ 09. Mutable Fields │
    │ 18. Str Pool  │        │ 08. All final?│        │ 10. Defensive Copy │
    │ 21. LocalDate │        │ 15. Inherit   │        │ 11. When to Copy   │
    │               │        │ 16. Final cls │        │ 12. Protect Coll.  │
    └───────┬───────┘        │ 17. Getter    │        │ 13. unmodifiable   │
            │                └───────┬───────┘        │ 14. Shallow vs Deep│
            │                        │                │ 29. Collections    │
            └────────────────────────┼────────────────└────────┬───────────┘
                                     │                        │
            ┌────────────────────────┼────────────────────────┘
            ▼                        ▼
    ┌───────────────┐        ┌───────────────┐
    │ THREAD SAFETY │        │ ADVANCED      │
    │ & CACHING     │        │ (19-20, 22-28)│
    │ (06, 22-23)   │        │ 19. Reflection│
    │ 06. ThreadSafe│        │ 20. Records   │
    │ 22. Caching   │        │ 24. Persistent│
    │ 23. Perform.  │        │ 25. Tradeoffs │
    │               │        │ 26. Builder   │
    │               │        │ 27. Map Keys  │
    │               │        │ 28. Mut. Key  │
    └───────────────┘        └───────────────┘

🟢 Junior Track (Foundations)

🟡 Middle Track (Defensive Copying & Collections)

🔴 Senior Track (JMM, Memory, Reflection & Persistent DS)