Java OCA OCP Practice Question 2149

Question

Given the following code:

public class Main {
  public static void main(String[] args) {
     // (1) INSERT CODE HERE
  }
}

Which code, when inserted at (1), will print the following in the terminal window:

Formatted output: 1234.04

Select the one correct answer.

(a)  String output = String.format("Formatted output: %.2f%n", 1234.0354);
     System.out.print(output);//from w w  w .ja va 2 s  . c o  m

(b)  System.out.format("Formatted output: %.2f%n", 1234.0354);

(c)  StringBuilder stb = new StringBuilder();
     Formatter fmt = new Formatter(stb);
     fmt.format("Formatted output: %.2f%n", 1234.0354);
     System.out.print(stb);

(d)  Formatter fmt2 = new Formatter(System.out);
     fmt2.format("Formatted output: %.2f%n", 1234.0354);

(e)  All of the above


(e)

Note

In (a) the resulting string is created by String.format() and printed by System.out.

A PrintStream (such as System.out) can format and print to its output stream.

A Formatter can be connected to an Appendable (which is implemented by the StringBuilder) as the destination for the formatted output.

The StringBuilder is then printed to the terminal window.

A Formatter can also be connected to a PrintStream (in this case System.out) which is the destination of the formatted output.




PreviousNext

Related