Java OCA OCP Practice Question 2155

Question

What will the following print when compiled and run?

public class Main {
  public static void main(String[] args) {
    Double[] dArray = {10.987, -100.678, 1000.345};
    System.out.format("|");
    for (int i = 0; i < dArray.length; i++) {
      System.out.format("%(,+-" + (i+1) + "." + (i+1) + "f|", dArray[i]);
    }
  }
}

Select the one correct answer.

  • (a) |(11.0)|(-100.68)|(+1,000.345)|
  • (b) |+11.0|-100.68|+1,000.345|
  • (c) |+11.0|(100.68)|+1,000.345|
  • (d) The program will not compile.
  • (e) The program will compile, but throw a java.util.IllegalFormatFlagsException when run.


(c)

Note

The program will use the following formats %(,+-1.1, %(,+-2.2, and %(,+-3.3 to print the values in the array dArray.

The flag '(' results in negative values being enclosed in ().

The flag ',' specifies that the locale-specific grouping character should be used.

The flag '+' specifies that positive values should be prefixed with the + sign.

The flag '-' specifies that values should be printed left-justified.

The width is less than the required width, and therefore overridden.

The precision can result in rounding, as is the case for the first two values.




PreviousNext

Related