Which code fragment, inserted at line 4, produces the output | 12.345|?

Given:

1. public class LineUp {
2. public static void main(String[] args) {
3. double d = 12.345;
4. // insert code here
5. }
6. }

Which code fragment, inserted at line 4, produces the output | 12.345|?

Given:

1. public class LineUp {
2. public static void main(String[] args) {
3. double d = 12.345;
4. // insert code here
5. }
6. }

Which code fragment, inserted at line 4, produces the output | 12.345|?

A.
System.out.printf(“|%7d| \n”, d);

B.
System.out.printf(“|%7f| \n”, d);

C.
System.out.printf(“|%3.7d| \n”, d);

D.
System.out.printf(“|%3.7f| \n”, d);

E.
System.out.printf(“|%7.3d| \n”, d);

F.
System.out.printf(“|%7.3f| \n”, d);

Explanation:
A: |
Exception in thread “main” java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3999)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2709)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2661)
at java.util.Formatter.format(Formatter.java:2433)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at LineUp.main(Main.java:4)

B: |12.345000|

C:
Exception in thread “main” java.util.IllegalFormatPrecisionException: 7
at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2892)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2643)
at java.util.Formatter.parse(Formatter.java:2480)
at java.util.Formatter.format(Formatter.java:2414)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at LineUp.main(Main.java:4)

D: |12.3450000|

E:
Exception in thread “main” java.util.IllegalFormatPrecisionException: 3
at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2892)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2643)
at java.util.Formatter.parse(Formatter.java:2480)
at java.util.Formatter.format(Formatter.java:2414)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at LineUp.main(Main.java:4)

F: | 12.345|



Leave a Reply 0

Your email address will not be published. Required fields are marked *