Question
What will the following print when compiled and run?
public class Main {
public static void main(String[] args) {
System.out.printf("|%0.0f|", 12.5);
System.out.printf("|%0.s|", 12.5);
System.out.printf("|%( +-10.2f|", -12.5);
System.out.format("|%10.2f|%d", 12.5 );
System.out.format("|%!10.2f|", 12.5 );
}
}
Select the one correct answer.
- (a) |12|12.|(12.50) | 12.50| -12.50|
- (b) |13|13.|(-12.50) | 12.50| -12.50|
- (c) The program will not compile.
- (d) The program will compile, but throw an exception when run.
(d)
Note
The respective exceptions thrown by the statements above are:
- java.util.MissingFormatWidthException (requires positive width)
- java.util.UnknownFormatConversionException (because of width 0)
- java.util.IllegalFormatFlagsException (because of the flag combination '( +-')
- java.util.MissingFormatArgumentException (the second format has no matching argument)
- java.util.UnknownFormatConversionException ( '!' is an unknown flag)
PreviousNextRelated