0
0
Javaprogramming~20 mins

Output formatting basics in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Output Formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this formatted print statement?
Consider the following Java code snippet. What will it print to the console?
Java
System.out.printf("Name: %s, Age: %d\n", "Alice", 30);
AName: Alice, Age: 30
BName: Alice Age: 30
CName: %s, Age: %d
DName: Alice, Age: %d
Attempts:
2 left
💡 Hint
Look at how %s and %d are replaced by the values in order.
Predict Output
intermediate
2:00remaining
What does this code print with width formatting?
What is the output of this Java code?
Java
System.out.printf("|%10s|%5d|\n", "Bob", 7);
A|Bob |7 |
B| Bob| 7|
C|Bob|7|
D| Bob| 7|
Attempts:
2 left
💡 Hint
The number after % indicates minimum width, right aligned by default.
Predict Output
advanced
2:00remaining
What is the output with floating-point precision formatting?
What does this Java code print?
Java
System.out.printf("%.2f\n", 3.14159);
A3.14159
B3.1
C3.15
D3.14
Attempts:
2 left
💡 Hint
%.2f means print a floating number with 2 digits after decimal.
Predict Output
advanced
2:00remaining
What is the output of this left-aligned formatted print?
What will this Java code print?
Java
System.out.printf("|%-10s|%5d|\n", "Cat", 42);
A|Cat|42|
B| Cat| 42|
C|Cat | 42|
D|Cat |42 |
Attempts:
2 left
💡 Hint
%-10s means left-align the string in 10 spaces.
Predict Output
expert
2:30remaining
What is the output of this complex formatted print?
What does this Java code print?
Java
System.out.printf("%-8s %04d %.3f\n", "Dog", 7, 2.71828);
ADog 0007 2.718
BDog 7 2.718
CDog 7 2.718
DDog 0007 2.72
Attempts:
2 left
💡 Hint
%-8s left aligns string in 8 spaces, %04d pads integer with zeros to 4 digits, %.3f prints float with 3 decimals.