0
0
Javaprogramming~10 mins

Output formatting basics in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print "Hello, World!" to the console.

Java
System.out.[1]("Hello, World!");
Drag options to blanks, or click blank then click option'
Aoutput
Bprintln
Cwrite
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of println, so output stays on the same line.
Trying to use a method that doesn't exist like write or output.
2fill in blank
medium

Complete the code to format and print a floating-point number with two decimal places.

Java
System.out.printf("%.[1]f", 3.14159);
Drag options to blanks, or click blank then click option'
A3
B1
C4
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or 3 decimals instead of 2.
Forgetting the 'f' in the format specifier.
3fill in blank
hard

Fix the error in the code to print an integer padded with zeros to width 5.

Java
System.out.printf("%[1]d", 42);
Drag options to blanks, or click blank then click option'
A05
B5
C-5
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 5 without zero, which pads with spaces.
Using negative width which left-aligns but doesn't pad zeros.
4fill in blank
hard

Fill both blanks to print a string left-aligned in a field of width 10.

Java
System.out.printf("%[1][2]s", "Hi");
Drag options to blanks, or click blank then click option'
A-
B+
C10
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' which is not valid for string alignment.
Using width 5 which is too small for the example.
5fill in blank
hard

Fill all three blanks to print a floating-point number right-aligned with width 8 and 3 decimals.

Java
System.out.printf("%[1][2][3]f", 7.12345);
Drag options to blanks, or click blank then click option'
A-
B8
C.3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' which left-aligns instead of right-aligning.
Forgetting the dot before the decimal count.