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 ]
sridhar row
Ranch Hand
Joined: Jan 16, 2008
Posts: 162
posted
0
Thanks Irina ..yeah i found this a little strange. Probably take sometime to sink in.
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
posted
0
'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().
Can you please explain me these lines with help of example. i am not getting it...
Thanks<br />Dinesh
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
posted
0
Why not b is executed , it is looking for character which we have passed ie. "x". Can some one expalin me this
Ali Khalfan
Ranch Hand
Joined: Nov 03, 2007
Posts: 126
posted
0
"X" is a string not a character...it can't be handled. 'x' could be
Irina Goble
Ranch Hand
Joined: May 09, 2004
Posts: 75
posted
0
Dinesh, you can use the example from the first post. Try to compile it and see what happens.
Siri Naray
Ranch Hand
Joined: May 19, 2006
Posts: 105
posted
0
I am still not understanding why A is right. Can anyone pl explain when %b is true or false??
If you worry you cannot work... If you work you need not worry
sweety sinha
Ranch Hand
Joined: Jul 07, 2008
Posts: 76
posted
0
why can't option D is right? i didn't get the option A and G. please if anyone can explain
Siri Naray
Ranch Hand
Joined: May 19, 2006
Posts: 105
posted
0
G is correct because when you pass new Long("123"), System.out.format evaluates new Long("123").toString()
Remember: %s if argument is null, result is null. if argument is a primitive type , result=primitive if argument is a string, result=string if argument is an object, result=object.toString().
Nabila Mohammad
Ranch Hand
Joined: Nov 05, 2007
Posts: 661
posted
0
Originally posted by Sirishaaaaa Ghatty: I am still not understanding why A is right. Can anyone pl explain when %b is true or false??
Any non null value will return true.
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.