Java OCA OCP Practice Question 2647

Question

Which lambda can replace the MyPrintable class to return the same value? (Choose all that apply.)

public interface Printable { 
  String print(double d); 
} 
 
public class MyPrintable implements Printable { 
  public String print(double d) { 
     return "Printing"; 
  } 
} 
  • A. caller((e) -> "Printing");
  • B. caller((e) -> {"Printing"});
  • C. caller((e) -> { String e = ""; "Printing" });
  • D. caller((e) -> { String e = ""; return "Printing"; });
  • E. caller((e) -> { String e = ""; return "Printing" });
  • F. caller((e) -> { String f = ""; return "Printing"; });


A, F.

Note

B is incorrect because it does not use the return keyword.

C, D, and E are incorrect because the variable e is already in use from the lambda and cannot be redefined.

Additionally, C is missing the return keyword and E is missing the semicolon.

A and F are the only correct lambda expressions that match the functional interface.




PreviousNext

Related