Java OCA OCP Practice Question 3139

Question

What is the result of compiling and running the following program?

import java.util.Arrays;
import java.util.List;

public class Main {
  public static <T> void doIt(List<T>... aols) {          // (1)
    for(int i = 0; i < aols.length; i++) {
      System.out.print(aols[i] + " ");
    }/*  w  w  w . j  av a2  s  .  c o m*/
  }

  public static void main(String... args) {               // (2)
    List<String> ls1 = Arrays.asList("one", "two");
    List<String> ls2 = Arrays.asList("three", "four");
    List<String>[] aols = new List[] {ls1, ls2};          // (3)
    doIt(aols);                                           // (4)
  }
}

Select the one correct answer.

  • (a) The program does not compile because of errors in (1).
  • (b) The program does not compile because of errors in (2).
  • (c) The program does not compile because of errors in (3).
  • (d) The program does not compile because of errors in (4).
  • (e) The program compiles and prints: [one, two] [three, four]


(e)

Note

The method header in (1) is valid.

The type of the varargs parameter can be generic.

The type of the formal parameter aols is an array of Lists of T.

The method prints each list.

The main() method in (2) can be declared as String..., as it is equivalent to String[].

The statement at (3) creates an array of Lists of Strings.

The type parameter T is inferred to be String in the method call in (4).




PreviousNext

Related