Skip to content
Sharp

Object-oriented programming · interview question

If a 'Product' class has a method that modifies its price, how can you ensure this modification is visible to all 'CartItem' instances holding a reference to this 'Product'?

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

Correct answer

D. Ensure all 'CartItem' instances hold references to the same 'Product' object.

Why

When all 'CartItem' instances hold a reference to the same 'Product' object, any modification to the 'Product' will be visible to all because they reference the same memory location.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →