0
0
Javaprogramming~5 mins

Output formatting basics in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A%s
B%.3f
C%d
D%.3d
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
Which method prints text and moves to a new line automatically?
ASystem.out.print()
BSystem.out.printf()
CSystem.out.println()
DSystem.out.write()
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);
What will System.out.printf("%.1f", 2.78); output?
A2.8
B2.7
C2.78
D3.0
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.