Skip to content
Sharp

Object-oriented programming · interview question

In Java, why should you avoid returning a reference to a mutable internal collection?

Multiple choiceEasychapter-3-curatedpremiumoopdefensive-copying
Choose one answer
Answer and explanation

Correct answer

A. To prevent external modifications that break class invariants

Why

Returning a reference to a mutable internal collection can lead to external modifications that break class invariants, compromising encapsulation. This could result in an inconsistent state of the object.

Test yourself on Object-oriented programming

A free test with instant, explained results. No account needed.

Start free test
All Object-oriented programming questions →