Skip to content
Sharp

Core Java · interview question

Which line of code will cause a logical flaw in the following snippet?

Multiple choiceMediumchapter-1-curatedpremiumcore-javacode-reading-and-debugging
java
int cartTotal = 120;
if (cartTotal < 100) {
    cartTotal -= 5;
} else {
    cartTotal -= 10;
}
System.out.println(cartTotal);
Choose one answer
Answer and explanation

Correct answer

D. Line 2

Why

Line 2 has a logical flaw because 'cartTotal < 100' should be 'cartTotal >= 100' to apply the discount correctly when the total is 100 or more.

Test yourself on Core Java

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

Start free test
All Core Java questions →