Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 102
Question ID: UKOCP38454
Given code:
- package com.udayankhattry.ocp;
- /*INSERT*/
- public class Test {
- public static void main(String[] args) {
- MyInterface mi = () -> "";
- }
- }
Which of the following interface definitions can replace /*INSERT*/ such that there is no compilation error?
Choose 4 options.
-
A
- interface MyInterface {
- String toStr();
- }
-
B
- interface MyInterface {
- String toStr();
- String toString();
- }
-
C
- interface MyInterface {
- Object toStr();
- String toString();
- static void print(String str) {
- System.out.println(str);
- }
- }
-
D
- interface MyInterface {
- Object toStr();
- boolean equals(MyInterface mi);
- }
-
E
- interface MyInterface {
- String toString();
- }
-
F
- interface MyInterface {
- CharSequence test();
- }
-
G
- interface MyInterface {
- StringBuilder m();
- }
Reveal correct answers
Correct answers: A, B, C, F
Explanation
UKOCP38454:
Target type of lambda expression must be a Functional interface.
Functional interface must have only one non-overriding abstract method but Functional interface can have constant variables, static methods, default methods, private methods and overriding abstract methods [equals(Object) method, toString() method etc. from Object class].
Let's check which of the given options are Functional interfaces:
- interface MyInterface {
- String toStr();
- }
✓ MyInterface has only one non-overriding abstract method [toStr()] and hence a Functional interface.
Given lambda expression () -> "" means method accepts no argument and returns a String object. It is a correct implementation of toStr() method.
- interface MyInterface {
- String toStr();
- String toString();
- }
✓ MyInterface has only one non-overriding abstract method [toStr()] and toString() method is overriding abstract method, therefore it is also a valid Functional interface.
() -> ""; is the correct implementation of toStr() method.
- interface MyInterface {
- Object toStr();
- String toString();
- static void print(String str) {
- System.out.println(str);
- }
- }
✓ MyInterface has only one non-overriding abstract method [toStr()] and toString() method is overriding abstract method & print(String) is the static method, therefore it is also a valid Functional interface.
() -> ""; is the correct implementation of toStr() method.
- interface MyInterface {
- Object toStr();
- boolean equals(MyInterface);
- }
✗ My interface has two non-overriding abstract methods. Please note that boolean equals(Object); is overriding abstract method but boolean equals(MyInterface); is non-overriding abstract method.
- interface MyInterface {
- String toString();
- }
✗ String toString(); is overriding abstract method and hence in this case, MyInterface doesn't contain non-overriding abstract method. Therefore it is not a Functional interface.
- interface MyInterface {
- CharSequence test();
- }
✓ MyInterface has only one non-overriding abstract method [test()] and hence a Functional interface.
As String class implements CharSequence, therefore value returned by Lambda expression [""] (String type) can easily be assigned to CharSequence reference.
() -> ""; is the correct implementation of CharSequence test();
- interface MyInterface {
- StringBuilder m();
- }
✗ MyInterface has only one non-overriding abstract method [m()] and hence a Functional interface.
Value returned by Lambda expression [""] (String type) can not be assigned to StringBuilder reference.
() -> ""; is NOT the correct implementation of StringBuilder m();
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
