ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
List<String> strings = new ArrayList<>(); List<Integer> ints = new ArrayList<>(); System.out.println(strings.getClass() == ints.getClass());
- A. false
- B. Compilation error
- C. Throws a ClassCastException at runtime
- D. true
Correct answer: D. true
Type erasure means List<String> and List<Integer> both compile down to the same raw ArrayList class at runtime; there is no separate runtime class per generic instantiation, so the getClass() comparison is true.
More OOP with C++ and Java questions
- What is printed? java Integer a = 127; Integer b = 127; Integer c = 200; Integer d = 200; System.out.println((a == b) + " " + (c == d));
- What is printed? java class Base { public void show() { System.out.print("Base "); } } class Derived extends Base { protected void show()…
- What is printed? java class Base { void greet(Object o) { System.out.print("Object "); } } class Derived extends Base { void greet(String…
- What is printed? java class Base { static void show() { System.out.print("Base.static "); } } class Derived extends Base { static void…
- What is printed? java class Base { protected int code = 1; } class Derived extends Base { int code = 2; } // in main: Base b = new…
- What is printed? java class Vehicle { void start() { System.out.print("Vehicle starts "); } } interface Electric { default void start() {…