Skip to content
Sharp

Object-oriented programming · interview question

What design principle is violated if a Cart class directly manipulates a Product's price?

Multiple choiceMediumchapter-3-curatedpremiumoopobject-responsibility
Choose one answer
Answer and explanation

Correct answer

B. Encapsulation

Why

Directly manipulating a Product's price from the Cart class violates encapsulation, as it bypasses the Product's control over its own state. Each class should manage its own data and behavior to maintain encapsulation.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →