Java OCA OCP Practice Question 2416

Question

Which options will output the following code?

Hello true 123456
  • a System.out.print("%s %b %d", new StringBuilder("Hello"), "false", 123456);
  • b System.out.printf("%s %b %d", new String("Hello"), "false", 123456);
  • c System.out.format("%s %b %d", new StringBuilder("Hello"), "false", 123456);
  • d System.out.println("%s %b %d", new StringBuilder("Hello"), "false", 123456.70);
  • e System.out.printf("%s %b %d", new StringTokenizer("Hello"), "false", 123456);
  • f System.out.format("%s %b %d", ("Hello"), "FALSE", new Integer(123456));


b, c, f

Note

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().




PreviousNext

Related