Skip to content
Sharp

Object-oriented programming · interview question

In a shopping cart application, how can you prevent a 'Cart' from directly modifying a 'Product'?

Multiple choiceHardchapter-2-curatedpremiumoopobject-references
Choose one answer
Answer and explanation

Correct answer

C. Use encapsulation by setting Product fields to private and providing no setters.

Why

Encapsulation restricts access to the fields of a class. By making fields private and providing no setters, you ensure that Products cannot be directly modified by a Cart.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →