Skip to content
Sharp

Core Java · interview question

Consider the class Product with a method applyDiscount(double discount). If the discount calculation logic is complex and used in multiple places, what is a recommended design pattern to apply?

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

Correct answer

A. Extract the logic into a separate class.

Why

Extracting the discount calculation logic into a separate class improves reusability and adheres to the single responsibility principle. This approach allows for easier maintenance and testing.

Test yourself on Core Java

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

Start free test
All Core Java questions →