1Z0-819 · Question #39
Given interface MyInterface1 { public int method1() throws Exception; private void pMethod() { / an implementation of pMethod / } } interface MyInterface2 { public static void sMethod() { / an…
The correct answer is A. MyInterface1 D. MyInterface4. For a lambda expression to work, its target must be a functional interface - an interface with exactly one abstract method (called a SAM: Single Abstract Method). A (MyInterface1) is correct: method1() is the single abstract method, while pMethod() carries a private method body…
Question
Options
- AMyInterface1
- BMyInterface3
- CMyInterface2
- DMyInterface4
- EMyInterface5
How the community answered
(25 responses)- A72% (18)
- B16% (4)
- C8% (2)
- E4% (1)
Explanation
For a lambda expression to work, its target must be a functional interface - an interface with exactly one abstract method (called a SAM: Single Abstract Method). A (MyInterface1) is correct: method1() is the single abstract method, while pMethod() carries a private method body (permitted since Java 9), which is never counted toward the abstract-method total. D (MyInterface4) is correct as well: dMethod() is intended as the single abstract method; the implementation body shown in the question is almost certainly a typo - interface methods with bodies require either the default, static, or private keyword, and the exam intends dMethod() to be abstract.
The distractors each fail for a distinct reason: B (MyInterface3) has two abstract methods (method() and method(String str)), which breaks the one-SAM rule outright; C (MyInterface2) has zero qualifying abstract methods because equals(Object o) merely re-declares a public java.lang.Object method (which the spec explicitly excludes from the count) and sMethod() is a concrete static method; E (MyInterface5) won't even compile since static interface methods must have a body, and a bodyless sMethod() is a compile-time error.
Memory tip: Apply the "SAM filter" mentally - subtract any static, default, private, and Object-override methods from the list, then count what's left. Exactly one? Lambda-eligible. Zero or two or more? Not eligible.
Topics
Community Discussion
No community discussion yet for this question.