Skip to content
Sharp

Object-oriented programming · interview question

How can you design a 'Cart' class to maintain a list of 'CartItem' objects and ensure the total price is always up to date?

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

Correct answer

C. Use an observer pattern to update the total price.

Why

Using an observer pattern allows the 'Cart' class to automatically update the total price whenever an individual 'CartItem' changes. This ensures consistency and reduces the likelihood of errors.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →