Java OCA OCP Practice Question 2167

Question

Given the following code:

public class Main {
  public static void main(String[] args) {
    Integer[] integerArray = {10, 100, 1000, 10000};
    int[] intArray = {10, 100, 1000, 10000};
    // (1) INSERT STATEMENT HERE
  }
}

Which statement, when inserted at (1), will result in the program printing:

|1000|+10 | 100|

    

Select the one correct answer.

(a) System.out.printf("|%3$4d|%4d|%2$04d|%n", integerArray);
(b) System.out.printf("|%3$4d|%-+4d|%2$4d|%n", integerArray);
(c) System.out.printf("|%+4d|%4d|%4d|%n", integerArray);
(d) System.out.printf("|%4d|%4d|%4d|%n", intArray);
(e) None of the above.


(b)

Note

(a) will print:  |1000|  10|0100|
(b) will print:  |1000|+10 | 100|
(c) will print:  | +10| 100|1000|
(d) will throw a java.util.IllegalFormatConversionException, 
    as  intArray is passed as new Object[] { intArray } to the  printf() method. 

An array is certainly not an int.

Remember that int[] is not a subtype of Object[], but Integer[] is.




PreviousNext

Related