Skip to content
Sharp

Object-oriented programming · interview question

How can you ensure that a collection inside an immutable class cannot be modified?

Multiple choiceHardchapter-3-curatedpremiumoopimmutability-basics
Choose one answer
Answer and explanation

Correct answer

A. Return a new unmodifiable collection in the getter.

Why

To maintain immutability, you should return a new unmodifiable collection when a collection is exposed via a getter. This prevents external code from altering the collection's contents.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →