Flashcards

Quick study sessions to strengthen memory and retain key concepts.

Flashcard Study

Java: Code Reading and State Safety

Front

Why is using setters cautiously important?

Too many setters can lead to fragile code and broken invariants. Excessive use of setters can make it…

View Card →
Flashcard Study

Java: Code Reading and State Safety

Front

How would you explain a safer approach to handle mutable collections in a class in an interview?

Return a copy or unmodifiable view of the collection. To prevent modifications from outside the class, return a…

View Card →
Flashcard Study

Java: Code Reading and State Safety

Front

What is immutability in Java?

An immutable object cannot change its state after creation. Immutability means once an object is created, its state…

View Card →
Flashcard Study

Java: Code Reading and State Safety

Front

How can exposing mutable static fields be problematic?

It can lead to shared state modifications across instances and threads. Mutable static fields are shared across all…

View Card →
Flashcard Study

Java: Code Reading and State Safety

Front

What are compile-safe but runtime-unsafe designs?

Designs that compile but allow invalid states or operations at runtime. A compile-safe design passes syntax checks but…

View Card →
Flashcard Study

Java: Code Reading and State Safety

Front

How can a developer refactor a class to improve encapsulation?

Use private fields with public getters and setters. Refactoring a class to use private fields with public getters…

View Card →
Flashcard Study

Java: Code Reading and State Safety

Front

Why is it problematic to mix public fields and methods in a class?

Mixing public fields and methods breaks encapsulation. When a class uses both public fields and public methods, it…

View Card →
Flashcard Study

Java: Code Reading and State Safety

Front

What does encapsulation achieve in object-oriented design?

Encapsulation hides internal details and exposes only necessary parts. Encapsulation allows an object to manage its own state…

View Card →
Flashcard Study

Java: Code Reading and State Safety

Front

How would you explain a common mistake when exposing collections in a class in an interview?

Exposing collections can lead to unintended modifications. If a class exposes a collection directly, external code can modify…

View Card →
Flashcard Study

Java: Code Reading and State Safety

Front

How can immutability enhance thread safety?

Immutable objects can't change state, preventing race conditions. Immutability ensures that once an object is created, its state…

View Card →