ProgrammingVery hardProduct-company placement · Java
Which of these is a valid functional interface (usable as a lambda target)?
- A. interface Two { void a(); void b(); }
- B. interface Cmp<T> { int compare(T a, T b); boolean equals(Object o); }
- C. interface None { default void a() { } }
- D. abstract class Fn { abstract void run(); }
Correct answer: B. interface Cmp<T> { int compare(T a, T b); boolean equals(Object o); }
A functional interface has exactly one abstract method; abstract methods that match public methods of Object (like equals) do not count, which is why java.util.Comparator qualifies. Abstract classes are never lambda targets.
More OOP with C++ and Java questions
- 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() { } }
- Which lazily-initialised singleton is thread-safe WITHOUT any explicit synchronization or volatile field?
- What is printed? java // static generic method of the main class: static <T extends Comparable<T>> T max(T a, T b) { return a.compareTo(b)…