Skip to content
Sharp

Object-oriented programming · interview question

How can a ShoppingCart efficiently calculate the total cost of all items?

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

Correct answer

A. By iterating through CartItem objects and summing their total prices.

Why

A ShoppingCart should iterate through CartItem objects, summing their total prices using their methods. This approach keeps the calculation dynamic and in sync with the cart's contents.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →