Skip to content
Sharp

Object-oriented programming · interview question

Why might a Product class use a setPrice(double price) method?

Multiple choiceMediumchapter-2-curatedpremiumoopobject-interaction
Choose one answer
Answer and explanation

Correct answer

A. To allow the price to be updated while maintaining encapsulation.

Why

A setPrice(double price) method allows controlled updates to a product's price, maintaining encapsulation by controlling how and when changes occur.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →