Challenge - 5 Problems
Output Formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Look at how %s and %d are replaced by the values in order.
✗ Incorrect
The printf method replaces %s with the string "Alice" and %d with the integer 30, printing them formatted.
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
The number after % indicates minimum width, right aligned by default.
✗ Incorrect
The string "Bob" is printed right aligned in a field 10 characters wide, and 7 is printed right aligned in 5 spaces.
❓ Predict Output
advanced2:00remaining
What is the output with floating-point precision formatting?
What does this Java code print?
Java
System.out.printf("%.2f\n", 3.14159);
Attempts:
2 left
💡 Hint
%.2f means print a floating number with 2 digits after decimal.
✗ Incorrect
The number 3.14159 is rounded to two decimal places, printing 3.14.
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
%-10s means left-align the string in 10 spaces.
✗ Incorrect
The string "Cat" is printed left aligned in 10 spaces, and 42 is right aligned in 5 spaces.
❓ Predict Output
expert2: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);
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.
✗ Incorrect
The string "Dog" is left aligned in 8 spaces, 7 is padded with zeros to 0007, and 2.71828 is rounded to 2.718.