Skip to content
Sharp

Core Java · interview question

Consider a class Customer with a method addToOrderHistory(Product product). Which aspect of this design could indicate a need for refactoring?

Multiple choiceMediumchapter-2-curatedpremiumcore-javacode-reading-and-refactoring-to-objects
Choose one answer
Answer and explanation

Correct answer

B. The method modifies the customer's private order history list directly.

Why

Accessing and modifying private fields directly within a method can lead to fragile code. Encapsulation should be used to manage access, which may involve using getters and setters or other methods.

Test yourself on Core Java

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

Start free test
All Core Java questions →