Java OCA OCP Practice Question 2324

Question

What will the program print when compiled and run?

public class Main {
  public static void main(String[] args) {
    List<String> lst1 = new ArrayList<String>();
    List<Integer> lst2 = new ArrayList<Integer>();
    List<List<Integer>> lst3 = new ArrayList<List<Integer>>();
    System.out.print(lst1.getClass() + " ");
    System.out.print(lst2.getClass() + " ");
    System.out.println(lst3.getClass());
  }/*from ww  w.ja  v  a2s .  c om*/
}

Select the one correct answer.

  • (a) class java.util.ArrayList<String> class java.util.ArrayList<Integer> class java.util.ArrayList<List<Integer>>
  • (b) class java.util.ArrayList class java.util.ArrayList class java.util.ArrayList
  • (c) class java.util.List class java.util.List class java.util.List
  • (d) class java.util.List<String> class java.util.List<Integer> class java.util.List<List<Integer>>
  • (e) The program will not compile.
  • (f) The program compiles, but throws an exception when run.


(b)

Note

It is the fully qualified name of the class after erasure that is printed at runtime.

Note that it is the type of the object, not the reference, that is printed.

The erasure of all the lists in the program is ArrayList.




PreviousNext

Related