Object-oriented programming · interview question
A product can appear in many carts with different quantities. Where does the quantity for one cart line belong most naturally?
Multiple choiceMediumchapter-2-curatedpremiumoopfields-and-state
Answer and explanation
Correct answer
D. In CartItem, because it belongs to one product-plus-quantity relationship
Why
Quantity for one specific cart line is not universal product state. It belongs to the cart-item relationship, which is why a CartItem object is a better model than scattering quantity across unrelated places.
Test yourself on Object-oriented programming
A free test with instant, explained results. No account needed.
More Object-oriented programming questions
- Why does declaring a method as 'final' in a superclass change runtime behavior?
- Why might a developer choose to use a 'restock' method rather than a public setter for inventory?
- How do unmodifiable views differ from defensive copies in protecting internal collections?
- In an e-commerce application, what is the advantage of using a 'processOrder' method in a Product class?
- Which statement is true about interfaces in Java?
- Why are public mutable fields risky in a store model?