Java OCA OCP Practice Question 2634

Question

Consider the following program:

import java.util.ArrayList;

class Main {
        public static void main(String []args) {
                Class c1 = new ArrayList<String>().getClass();          // LINE A
                Class c2 = ArrayList.class;                             // LINE B
                System.out.println(c1 == c2);
        }
}

Which one of the following options correctly describes the behavior of this program?

  • a) The program will result in a compiler error in the line marked with the comment LINE A.
  • b) The program will result in a compiler error in the line marked with the comment LINE B.
  • c) When executed, the program prints the following: true.
  • d) When executed, the program prints the following: false.


c)

Note

With type erasure, details of the generic type are lost when the program is compiled.

Hence, at runtime, types of the instance ArrayList<String> and the raw type ArrayList are the same.




PreviousNext

Related