Which options will output the following code?
Hello true 123456
b, c, f
Options (a) and (d) won't compile.
The type of the variable out in class System is PrintStream.
PrintStream's methods print()
and println()
accept just one method parameter.
They're overloaded to accept parameters of all the primitive types and object references.
But, they don't accept format strings and arguments.
Option (b) is correct.
The format specifier %b returns the value true for non-null values for object references other than Boolean or boolean.
Option (c) is correct.
The format specifier %s calls method toString()
on an object reference to get its String representation.
Method toString()
of class StringBuilder creates and returns a new String by using the sequence stored by the StringBuilder.
So printing a StringBuilder doesn't print a value similar to StringBuilder@135d.
Option (e) is incorrect.
%s calls StringTokenizer's method toString()
to access its String representation.
This option outputs a value similar to this:.
java.util.StringTokenizer@123159 true 123456
Option (f) is correct. Methods printf()
and format()
provide exactly the same functionality. Behind the scenes, printf()
calls format()
.