ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
List<String> strings = new ArrayList<>(); List<Integer> ints = new ArrayList<>(); System.out.print(strings.getClass() == ints.getClass());
- A. Compilation error: cannot compare getClass() of different generic types
- B. false
- C. It depends on the JVM
- D. true
Correct answer: D. true
Generics are erased at compile time: at runtime, List<String> and List<Integer> are both represented by the exact same raw class, ArrayList. Their getClass() objects are identical, so == is true — the classic evidence of type erasure.
More OOP with C++ and Java questions
- What is printed? java class Holder<T extends Number> { T value; Holder(T v) { value = v; } double asDouble() { return value.doubleValue()…
- What is printed? java class Util { static <T> void swap(T[] arr, int i, int j) { T tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } public…
- What is printed? java public class Main { static <T extends Comparable<T>> T max(T a, T b) { return a.compareTo(b) >= 0 ? a : b; } public…
- Which statement is correct about this class? java public class Base { public void show() { } } class Sub extends Base { private void…
- Which statement is correct? java class Base { protected void configure() { } } class Derived extends Base { public void configure() { } }
- Which statement is correct? java final class Immutable { void f() { } } class Sub extends Immutable { }