Skip to content
Sharp

Core Java · interview question

What happens if you use 'System.out.printf()' with '%d' to print a double value?

Multiple choiceMediumchapter-1-curatedpremiumcore-javaconsole-input-and-output
java
double price = 19.99;
System.out.printf("Price: %d", price); // Prints: Price: 19
Choose one answer
Answer and explanation

Correct answer

B. It prints the double as an integer.

Why

Using '%d' with a double in 'printf' will not throw an exception but rather print the double value as an integer by truncating the decimal part, which might not be the intended behavior.

Test yourself on Core Java

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

Start free test
All Core Java questions →