nerdexam
Oracle

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…

Working with Streams and Lambda Expressions

Question

Given interface MyInterface1 { public int method1() throws Exception; private void pMethod() { /* an implementation of pMethod */ } } interface MyInterface2 { public static void sMethod() { /* an implementation of sMethod */ } public boolean equals(Object o); } interface MyInterface3 { public void method(); public void method(String str); } interface MyInterface4 { public void dMethod() { /* an implementation of dMethod */ } } interface MyInterface5 { public static void sMethod(); public void method(String str); } Which two interfaces can be used in lambda expressions? (Choose two.)

Options

  • AMyInterface1
  • BMyInterface3
  • CMyInterface2
  • DMyInterface4
  • EMyInterface5

How the community answered

(25 responses)
  • A
    72% (18)
  • B
    16% (4)
  • C
    8% (2)
  • E
    4% (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

#functional interface#lambda expressions#abstract methods#interface contract

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice