Java OCA OCP Practice Question 1676

Question

What will be the result of compiling and running the following program?.

public class Main {
 static void compute(int... is) {                             // (1)
   System.out.print("|");
   for(int i : is) {
     System.out.print(i + "|");
   }/*from w w  w  .  j  a va 2  s  . c o  m*/
   System.out.println();
 }
 static void compute(int[] ia, int... is) {                   // (2)
   compute(ia);
   compute(is);
 }
 static void compute(int[] inta, int[]... is) {               // (3)
   for(int[] ia : is) {
     compute(ia);
   }
 }
 public static void main(String[] args) {
   compute(new int[] {90, 99}, new int[] {92, 93, 94});       // (4)
   compute(95, 96);                                           // (5)
   compute(new int[] {97, 98}, new int[][] {{89}, {20}});     // (6)
   compute(null, new int[][] {{21}, {22}});                   // (7)
 }
}

Select the one correct answer.

(a) The program does not compile because of errors in one or more calls to the compute() method.
(b) The program compiles, but throws a NullPointerException when run.
(c) The program compiles and prints:// w  ww.  java 2 s  .c om
    |90|99|
    |92|93|94|
    |95|96|
    |89|
    |20|
    |21|
    |22|
(d) The program compiles and prints:
    |92|93|94|
    |95|96|
    |90|99|
    |89|
    |20|
    |21|
    |22|


(c)

Note

The method call in (4) calls the method in (2).

The method call in (5) calls the method in (1).

The method call in (6) calls the method in (3), as does the call in (7).

Note the type of the varargs parameter in (3): an array of arrays of int.




PreviousNext

Related