Skip to content
Sharp

Core Java · interview question

You have a method that calculates a discount on a product price. How can you ensure this method adheres to the single responsibility principle?

Multiple choiceHardchapter-1-curatedpremiumcore-javamethods
Choose one answer
Answer and explanation

Correct answer

A. Ensure it only calculates the discount and nothing else.

Why

The single responsibility principle states that a method should have one and only one reason to change, meaning it should perform one task. By ensuring the method only calculates the discount, you adhere to this principle, keeping the code modular and easier to maintain.

Test yourself on Core Java

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

Start free test
All Core Java questions →