ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
// in main: List<String> ls = new ArrayList<>(); List<Integer> li = new ArrayList<>(); System.out.println(ls.getClass() == li.getClass());
- A. false
- B. true
- C. Compilation error: incompatible generic types
- D. It depends on the JVM
Correct answer: B. true
Due to type erasure, both List<String> and List<Integer> compile down to the same raw ArrayList class at run time — the generic type parameter exists only at compile time for static checking and is gone from the bytecode, so getClass() returns the identical Class object for both: true.
More OOP with C++ and Java questions
- Which statement about this class is correct? java class Example<T> { void method(List<String> list) { System.out.println("string-list"); }…
- What is printed? java class Util { static <T> T firstNonNull(T a, T b) { return a != null ? a : b; } } // in main: String s =…
- What is printed? java class Container<T extends Comparable<T>> { T max(T a, T b) { return a.compareTo(b) >= 0 ? a : b; } } // in main…
- What is printed? java class Node<T> { T data; static int count; { count++; } Node(T d) { data = d; } } // in main: new Node<Integer>(1)…
- What is printed? java class Base { protected void show() { System.out.println("Base.show"); } } class Derived extends Base { @Override…
- What is printed? java class Calculator { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } int…