Skip to content
Sharp

Core Java · interview question

What will be the value of 'totalCost' if executed with cartTotal = 100 and discountApplied = false in this code? int totalCost = cartTotal - (discountApplied? 20 : 0);?

Multiple choiceHardchapter-1-curatedpremiumcore-javaoperators-and-expressions
java
int totalCost = cartTotal - (discountApplied ? 20 : 0);
Choose one answer
Answer and explanation

Correct answer

B. 100

Why

Since 'discountApplied' is false, the ternary operator results in '0'. Therefore, 'totalCost' is 'cartTotal - 0', which equals 100.

Test yourself on Core Java

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

Start free test
All Core Java questions →