Skip to content
Sharp

Object-oriented programming · interview question

How can a class ensure that its objects remain immutable?

Multiple choiceMediumchapter-3-curatedpremiumoopcontrolled-mutation
Choose one answer
Answer and explanation

Correct answer

B. Make all fields final and provide no setters.

Why

To create an immutable class, all fields should be declared as 'final' and initialized via a constructor, with no setters provided. This ensures that once an object is created, its state cannot change.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →