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()));- A. false true false
- B. true true true
- C. false false false
- 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
- What happens when this interface is compiled? java interface Named { String name(); default boolean equals(Object o) { return o instanceof…
- Which of these is a valid functional interface (usable as a lambda target)?
- What is printed? java class T { String name = "outer"; void run() { Runnable a = () -> System.out.print(this.name + " "); Runnable b = new…
- What is printed? java // Java 16 or later record P(int x, int y) { } // in main: P a = new P(1, 2), b = new P(1, 2); System.out.print((a…
- What happens when this is compiled? java class P { P(int x) { } P() { } } class C extends P { } // in main: C c = new C(5);
- What happens when C is compiled? java class P { static void s() { } } class C extends P { @Override static void s() { } }