Given:
Which two interfaces can be used in lambda expressions? (Choose two.)
Given:
Which two interfaces can be used in lambda expressions? (Choose two.)
Lambda expressions can be used with functional interfaces, which are interfaces with only one abstract method. An interface with private or static methods still qualifies as long as it has exactly one abstract method. Analyzing the interfaces: MyInterface4 has an implementation error in dMethod() suggesting it should be a default method. MyInterface5 has two abstract methods, hence not a functional interface. MyInterface1 has one abstract method (method()) and private methods are allowed in interfaces. MyInterface3 has two abstract methods (method and method(String str)), disqualifying it as a functional interface. MyInterface2 contains no valid abstract methods for a functional interface. Therefore, MyInterface1 (C) and the correct implementation of MyInterface4 (A) are suitable for lambda expressions.
The answer is A . private and static method in interface must be contain a body
A. MyInterface4=> method abstract cannot contain body B. MyInterface5 => OK C. MyInterface1 => OK D. MyInterface3=> has two abstract methods NOT SAM E. MyInterface2=> no abstract methods, booleans equeals is no count as abstract method because is from object class
and NOTE: FOR E private methods and static must containt body
!and : B is bad sstatic must be body
Tested: A and C. It compiles in combination, C can not be alone.
Correct answer: JUST A. @FunctionalInterface //OK public interface MyInterface1 { public int method() throws Exception; private void pMethod(){} } // NO @FunctionalInterface interface MyInterface2 { public static void sMethod() {} private boolean equals(); //is private!! must be abstract| have body } // NO @FunctionalInterface - has two abstract methods interface MyInterface3 { public void method(); public void method(String str); } //@FunctionalInterface: NO interface MyInterface4 { public void dMethod() {}//must be static or default: NOT COMPILE public void Method(); }
To see that an interface is a functional interface, add the annotation @FunctionalInterface. Only functional interfaces can be added to lambda expressions. MyInterface2 and MyInterface4 give compilation error, and MyInterface3 has two abstract methods. So only MyInterface1 is correct, I think...
C is correct
The only correct answer is A . C is incorrect because all public non-static interface methods should not have a body .
The only correct answer is C . A is incorrect because all public non-static interface methods should not have a body .
Lambda expressions can only be used with functional interfaces, which are interfaces with a single abstract method (SAM) but can have any other type of functions: A. MyInterface4 (Bad syntax dMethod must have default before return type) B. MyInterface5 (Not valid, has 2 abstract method) C. MyInterface1 (Valid has one abstract method) D. MyInterface3 (Not valid, has 2 abstract method) E. MyInterface2 (Not valid does not have an abstract method) So, the correct answers are: C. MyInterface1 If there was a default in option A. MyInterface4 then it will be correct
A and D is the answer.
B AND C !
Correct, only C : A. MyInterface4=> method abstract cannot contain body B. MyInterface5 => static method must be a body C. MyInterface1 => OK D. MyInterface3=> has two abstract methods NOT SAM E. MyInterface2=> no abstract methods, booleans equeals is no count as abstract
Lambda expressions can only be used with functional interfaces, which are interfaces with a single abstract method (SAM). Let's analyze the provided interfaces: A. MyInterface4 (not provided in the initial interfaces) B. MyInterface5 (valid, as it has a single abstract method) C. MyInterface1 (not provided in the initial interfaces) D. MyInterface3 (valid, as it has a single abstract method) E. MyInterface2 (not valid, as it has more than one abstract method) So, the correct answers are: B. MyInterface5 D. MyInterface3
MyInterface3 has two abstract methods
Lambda expressions can only be used with functional interfaces, which are interfaces with a single abstract method (SAM). Let's analyze the provided interfaces: A. MyInterface4 (not provided in the initial interfaces) B. MyInterface5 (valid, as it has a single abstract method) C. MyInterface1 (not provided in the initial interfaces) D. MyInterface3 (valid, as it has a single abstract method) E. MyInterface2 (not valid, as it has more than one abstract method) So, the correct answers are: B. MyInterface5 D. MyInterface3
MyInterface3 CANNOT BE A FUNCTIONAL INTERFACE. A functional interface can only have 1 abstract method. Myinterface2 is incorrect because an abstract method cannot be private. MyInterface4 is incorrect as the compiler will try to insert abstract into the first method which doesn't compile as an abstract method can't have a body. Myinterface5 won't compile since an abstract method can't be static. MyInterface1 is the only correct answer as it has exactly 1 abstract method. An interface can contain private methods.
CORRECT: C and D (interfaces 1 and 3)
C & D are correct
C. MyInterface1 can be used in a lambda expression because it has exactly one abstract method, method(), which is the requirement for an interface to be a functional interface. A functional interface is an interface that has exactly one abstract method and can be used as the target type for a lambda expression. D. MyInterface3 can also be used in a lambda expression because it also has exactly one abstract method, method(). The second method, method(String str), is an overloaded version of the first method and does not count as a separate abstract method. The other options are incorrect because they do not meet the requirement of having exactly one abstract method.
A & D are correct Because In lambda expression Interface should have only one abstract method. There should not be any Static method and private method. So MyInteface3 & MyInterface4 are correct. others are wrong
D Pass -> Interface abstract methods cannot have body public void method(); public void method(String str); as well as C public int method() throws Exception; private void pMethod() { /* an implementation of pMethod */ }
Only C is correct, in A the static method needs a body
A,C correct