Skip to content
Sharp

Object-oriented programming · interview question

Why should setters in a class be avoided or minimized?

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

Correct answer

D. They can lead to uncontrolled changes to the object's state

Why

Setters allow external code to modify an object's state directly, which can lead to inconsistent states and bugs. It's better to provide specific methods that ensure the object remains in a valid state.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →