Java OCA OCP Practice Question 2159

Question

Which statements, when inserted at (1), will result in the program throwing an exception when run?

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

    

Select the two correct answers.

  • (a) System.out.printf("|%-10c|", 'L');
  • (b) System.out.printf("|%5.1c|", 125);
  • (c) System.out.printf("|%c|", 33);
  • (d) System.out.printf("|%+c|", 33);
  • (e) System.out.printf("|%c|", new Character('h'));
  • (f) System.out.printf("|%-4c|", new Integer("33"));
  • (g) System.out.printf("|%c|", null);
  • (h) System.out.printf("|%2$2c|", 123, 'V', true);


(b) and (d)

Note

(b) will throw a java.util.IllegalFormatPrecisionException because of the precision specification.

(d) will throw a java.util.FormatFlagsConversionMismatchException because of the + flag.

The other statements will produce the following output, respectively, excluding (b) and (d): |L |, |!|, |h|, |! |, |null|, | V|.




PreviousNext

Related