Java OCA OCP Practice Question 2852

Question

Which of the following are valid Callable expressions? (Choose all that apply.)

  • A. a -> {return 10;}
  • B. () -> {String s = "";}
  • C. () -> 5
  • D. () -> {return null}
  • E. () -> "The" + "Kava"
  • F. (int count) -> count+1
  • G. () -> {System.out.println("Hi"); return 10;}


C, E, G.

Note

A Callable lambda expression takes no values and returns a generic type; therefore C, E, and G are correct.

A and F are incorrect because they both take an input parameter.

B is a Runnable lambda expression, because it does not return a value, but it is not a Callable one, so B is incorrect.

D is not a valid lambda expression, because it is missing a semicolon at the end of the return statement, which is required when inside braces {}.




PreviousNext

Related