Skip to content
Sharp

Object-oriented programming · interview question

Why is lineTotal() usually a better method on CartItem than one giant procedural utility that takes many separate parameters?

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

Correct answer

A. Because the object that owns the data can answer the question that naturally belongs to it

Why

When behavior belongs close to the relevant state, the code becomes easier to read and maintain. CartItem.lineTotal() is often clearer than passing product name, unit price, and quantity around as loose values.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →