Java OCA OCP Practice Question 2741

Question

Which of the following compiles and print outs the entire set? (Choose all that apply.)

Set<String> s = new HashSet<>(); 
s.add("lion"); 
s.add("tiger"); 
s.add("bear"); 
s.forEach(                   ); 
  • A. () -> System.out.println(s)
  • B. s -> System.out.println(s)
  • C. (s) -> System.out.println(s)
  • D. System.out.println(s)
  • E. System::out::println
  • F. System.out::println


F.

Note

Choice A is incorrect because forEach takes a Consumer parameter, which requires one parameter.

Choices B and C are close.

The syntax for a lambda is correct.

However, s is already defined as a local variable and therefore the lambda can't redefine it.

Choices D and E use incorrect syntax for a method reference.

Choice F is correct.




PreviousNext

Related