ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
Object a = new ArrayList<String>();
List<Integer> b = new LinkedList<>();
List<Integer> c = new ArrayList<>();
System.out.print((a.getClass() == c.getClass()) + " " + (a instanceof List) + " "
    + (b.getClass() == c.getClass()));
  1. A. false true false
  2. B. true true true
  3. C. false false false
  4. D. true true false

Correct answer: D. true true false

getClass() returns the runtime class; ArrayList<String> and ArrayList<Integer> are both just ArrayList after erasure. LinkedList and ArrayList are different classes even though both are Lists.

More OOP with C++ and Java questions

Exams that ask this topic