Skip to content
Sharp

Object-oriented programming · interview question

Consider a class Order with a private field quantity. Which design choice ensures the quantity is always positive?

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

Correct answer

D. Enforce a check in the setter method

Why

Implementing a check in the setter method ensures that every time the quantity is changed, the new value is validated. This encapsulation technique secures the invariant that the quantity must always be positive by centralizing the validation logic.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →