Skip to content
Sharp

Object-oriented programming · interview question

Why is it beneficial for a CartItem class to have a method that calculates its own line total?

Multiple choiceMediumchapter-2-curatedpremiumoopinstance-methods
Choose one answer
Answer and explanation

Correct answer

A. It ensures that calculations are always up-to-date with the item's state.

Why

Having a method like calculateLineTotal() within CartItem ensures that calculations are based on the current state of the object, maintaining accuracy and integrity of the data.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →