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?
A%s
B%.3f
C%d
D%.3d
✗ Incorrect
The specifier %.3f formats a floating-point number to 3 decimal places.
What does System.out.printf("%10s", "Hi") do?
APrints 'Hi' right-aligned in a 10-character wide space
BPrints 'Hi' left-aligned in a 10-character wide space
CPrints 'Hi' with 10 spaces after it
DPrints 'Hi' with 10 spaces before and after
✗ 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?
ASystem.out.print()
BSystem.out.printf()
CSystem.out.println()
DSystem.out.write()
✗ Incorrect
println() prints text and then moves the cursor to the next line.
How would you format an integer variable named num in printf?
ASystem.out.printf("%c", num);
BSystem.out.printf("%f", num);
CSystem.out.printf("%s", num);
DSystem.out.printf("%d", num);
✗ Incorrect
%d is used to format integers in printf.
What will System.out.printf("%.1f", 2.78); output?
A2.8
B2.7
C2.78
D3.0
✗ 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.
Practice
(1/5)
1. Which Java method is commonly used to format output with placeholders like %d or %s?
easy
A. System.out.read
B. System.out.printf
C. System.out.println
D. System.out.print
Solution
Step 1: Identify formatting methods in Java
System.out.printf and System.out.format are used for formatted output, unlike println or print which just print raw text.
Step 2: Recognize the method that uses placeholders
System.out.printf uses format specifiers like %d for integers and %s for strings to format output.
Final Answer:
System.out.printf -> Option B
Quick Check:
Formatted output uses printf [OK]
Hint: Look for method with % placeholders for formatting [OK]
Common Mistakes:
Confusing println with printf
Using print which lacks formatting
Thinking read formats output
2. Which of the following is the correct syntax to print an integer variable num with two digits padded with zeros using printf?
easy
A. System.out.printf("%02d", num);
B. System.out.printf("%2d", num);
C. System.out.printf("%d2", num);
D. System.out.printf("%d02", num);
Solution
Step 1: Understand zero-padding format specifier
In printf, %02d means print an integer padded with zeros to width 2.
Step 2: Check each option's correctness
%2d pads with spaces, %02d pads with zeros, %d2 and %d02 are invalid formats.
Final Answer:
System.out.printf("%02d", num); -> Option A
Quick Check:
Zero padding uses %0Nd [OK]
Hint: Zero pad with %0 before width number [OK]
Common Mistakes:
Using %2d pads with spaces, not zeros
Placing digits after %d incorrectly
Confusing padding syntax
3. What is the output of the following Java code?
int x = 7;
System.out.printf("Value: %4d!", x);
medium
A. Value: 7!
B. Value: 7 !
C. Value: 07!
D. Value:7!
Solution
Step 1: Understand %4d formatting
%4d means print integer right-aligned in a field width of 4 characters, padding with spaces on the left.
Step 2: Apply formatting to value 7
7 is one digit, so 3 spaces are added before it, resulting in " 7".
Final Answer:
Value: 7! -> Option A
Quick Check:
Right-align integer in 4 spaces [OK]
Hint: Width number pads spaces on left for integers [OK]
Common Mistakes:
Assuming padding is on the right
Confusing zero padding with space padding
Ignoring field width
4. The following code is intended to print a floating-point number with 2 decimal places:
double pi = 3.14159;
System.out.printf("%.2f", pi);
What is the error in this code?
medium
A. Missing newline character causes error.
B. %.2f is invalid format specifier for double.
C. No error; it prints 3.14 correctly.
D. Must cast pi to float before printing.
Solution
Step 1: Check format specifier for double
%.2f formats floating-point numbers with 2 decimal places, valid for double type.
Step 2: Verify code correctness
Code prints 3.14 without error; newline is optional and casting is unnecessary.
Final Answer:
No error; it prints 3.14 correctly. -> Option C
Quick Check:
%.2f formats double with 2 decimals [OK]
Hint: %.Nf formats float/double to N decimals [OK]
Common Mistakes:
Thinking casting to float is needed
Assuming newline is mandatory
Believing %.2f is invalid
5. You want to print a table of names and ages aligned in columns using printf. Which format string correctly aligns the name left in 10 spaces and age right in 3 spaces?
String name = "Anna";
int age = 9;
System.out.printf("???", name, age);
hard
A. "%-3s %-10d"
B. "%10s %-3d"
C. "%10s %3d"
D. "%-10s %3d"
Solution
Step 1: Understand alignment flags in printf
%-10s means left-align string in 10 spaces; %3d means right-align integer in 3 spaces.
Step 2: Match format to requirement
"%-10s %3d" matches left-align name and right-align age correctly; others swap or misalign.
Final Answer:
"%-10s %3d" -> Option D
Quick Check:
Left-align string with - flag, right-align number default [OK]
Hint: Use %- for left-align strings, default right-align numbers [OK]
Common Mistakes:
Swapping alignment flags
Using wrong width numbers
Misplacing % signs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.