Java OCA OCP Practice Question 3281

Question

Given the following code:

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

Which statements, when inserted at (1), will print the following:.


|false|

Select the six correct answers.

  • (a) System.out.printf("|%-4b|", false);
  • (b) System.out.printf("|%5b|", false);
  • (c) System.out.printf("|%.5b|", false);
  • (d) System.out.printf("|%4b|", "false");
  • (e) System.out.printf("|%3b|", 123);
  • (f) System.out.printf("|%b|", 123.45);
  • (g) System.out.printf("|%5b|", new Boolean("911"));
  • (h) System.out.printf("|%2b|", new Integer(2007));
  • (i) System.out.printf("|%1b|", (Object[])null);
  • (j) System.out.printf("|%1b|", (Object)null);
  • (k) System.out.printf("|%3$b|", null, 123, true);


(a), (b), (c), (g), (i), (j)

Note

(a) 5 character positions will be used.

The width 4 is overridden, and the - sign is superfluous.

(b) 5 character positions will be used.

The width 5 is superfluous.

(c) The precision 5 is the same as the length of the resulting string, and therefore superfluous.

(d) The reference value of the string literal "false" is not null, resulting in "true" being printed.

Analogous reasoning for (e) and (f).

(g) Since the argument to the Boolean constructor is not "true", the value represented by the wrapper object is false.

(h) Same as (d).

(i) Since the argument is null, the value printed is "false".

Width specification is overridden by the length of the string "false".

Same for (j).

(k) The third argument is true, therefore "true" is printed.




PreviousNext

Related