Skip to content
Sharp

Object-oriented programming · interview question

In the context of encapsulation, what is the primary benefit of validating data in a constructor?

Multiple choiceMediumchapter-3-curatedpremiumoopinvariants-and-validation
Choose one answer
Answer and explanation

Correct answer

B. It enforces object invariants at the time of creation.

Why

Validating data in a constructor ensures that the object is created in a valid state, maintaining invariants from the very beginning.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →