Which will compile and run without exception? (Choose all that apply.) A. System.out.format("%b", 123); B. System.out.format("%c", "x"); C. System.out.printf("%d", 123); D. System.out.printf("%f", 123); E. System.out.printf("%d", 123.45); F. System.out.printf("%f", 123.45); G. System.out.format("%s", new Long("123"));
Answer given: A,C,F and G.
Can someone explain why and how A and G are correct?
'b', 'B' - If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is "true".
's', 'S' - If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().
The A is a little bit confusing as a Boolean(String) constructor will return true only for a string "true" (ingnoring case). At least they agree on null. [ April 13, 2008: Message edited by: Irina Goble ]
Joined: Jan 16, 2008
Thanks Irina ..yeah i found this a little strange. Probably take sometime to sink in.