Skip to content
Sharp

Core Java · interview question

How can you correct the following code to ensure 'finalPrice' is calculated correctly?

Multiple choiceHardchapter-1-curatedpremiumcore-javacode-reading-and-debugging
java
float discount = 0.15;
int price = 100;
float finalPrice = price - (price * discount);
System.out.println(finalPrice);
Choose one answer
Answer and explanation

Correct answer

A. Initialize discount with a float

Why

By initializing 'discount' with a float value (e.g., 0.15f), you avoid truncating the decimal part, thus calculating 'finalPrice' correctly.

Test yourself on Core Java

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

Start free test
All Core Java questions →