File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question 9, Chapter 6 in K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question 9, Chapter 6 in K&B" Watch "Question 9, Chapter 6 in K&B" New topic
Author

Question 9, Chapter 6 in K&B

sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
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?
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
See java.util.Formatter:

'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
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
'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
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
"X" is a string not a character...it can't be handled. 'x' could be
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
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
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
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
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
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://aspose.com/file-tools
 
subject: Question 9, Chapter 6 in K&B