Skip to content
Sharp

Core Java · interview question

Which of the following code snippets will correctly format a double value to two decimal places when printing?

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

Correct answer

C. System.out.printf("%.2f", price);

Why

The 'printf' method allows for formatted output in Java. Using '%.2f' will format a floating-point number to two decimal places, which is commonly used for displaying prices.

Test yourself on Core Java

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

Start free test
All Core Java questions →