Recall & Review
beginner
What is the purpose of
System.out.printf() in Java?It is used to print formatted output to the console, allowing you to control how variables and text appear, such as setting decimal places or alignment.
Click to reveal answer
beginner
How do you format a floating-point number to show 2 decimal places using
printf?Use
%.2f in the format string. For example: System.out.printf("%.2f", 3.14159); prints 3.14.Click to reveal answer
beginner
What does the format specifier
%d represent in Java's printf?It represents a decimal integer. It is used to format integer values in the output.
Click to reveal answer
intermediate
How can you align text to the right in Java's
printf?By specifying a width in the format specifier, like
%10s, which right-aligns the string in a field 10 characters wide.Click to reveal answer
beginner
What is the difference between
System.out.print() and System.out.println()?print() outputs text without moving to a new line, while println() outputs text and then moves the cursor to the next line.Click to reveal answer
Which format specifier would you use to print a floating-point number with 3 decimal places?
✗ Incorrect
The specifier %.3f formats a floating-point number to 3 decimal places.
What does
System.out.printf("%10s", "Hi") do?✗ Incorrect
The %10s specifier right-aligns the string in a field 10 characters wide.
Which method prints text and moves to a new line automatically?
✗ Incorrect
println() prints text and then moves the cursor to the next line.
How would you format an integer variable named
num in printf?✗ Incorrect
%d is used to format integers in printf.
What will
System.out.printf("%.1f", 2.78); output?✗ Incorrect
It rounds the number to 1 decimal place, so 2.78 becomes 2.8.
Explain how to use
System.out.printf to format a number with two decimal places and right-align it in a 10-character wide field.Think about combining width and precision in the format string.
You got /4 concepts.
Describe the difference between
print, println, and printf in Java output.Consider how each method handles text and line breaks.
You got /3 concepts.