Skip to content
Sharp

Core Java · interview question

How can you correct the following code to ensure the discount is applied correctly?

Multiple choiceMediumchapter-1-curatedpremiumcore-javacode-reading-and-debugging
java
double total = 100.0;
double discount = 0.2;
double finalPrice = total - discount;
Choose one answer
Answer and explanation

Correct answer

A. Change 'total - discount' to 'total * (1 - discount)'

Why

To apply a discount correctly, you need to calculate the percentage of the total to subtract, which is done by multiplying by (1 - discount).

Test yourself on Core Java

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

Start free test
All Core Java questions →