Skip to content
Sharp

Core Java · interview question

Why does the following code snippet result in a logical error?

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

Correct answer

D. Incorrect variable type

Why

The variable 'discount' is declared as an integer, which results in the decimal part being truncated. This causes a logical error if the intent is to apply a percentage discount.

Test yourself on Core Java

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

Start free test
All Core Java questions →