Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 55

Given:

Which statement on line 1 enables this code to compile?

    Correct Answer: A

    The forEach method accepts a Consumer as its argument. A proper implementation of a Consumer that prints each element of the list is: Consumer<String> function = (String f) -> System.out.println(f); The other options involve different functional interfaces such as Supplier, Predicate, and Function, which are not appropriate for use with forEach.

Discussion
Galen86Option: A

In fact, the above answers are all wrong A seems correct, but the writing in lambda is wrong If rewritten as # Consumer function = s -> System.out.println(s); to compile normally

d7bb0b2Option: A

A is correct cause foreach accept consumer interface

d7bb0b2

NOTICE: A is the correct but consumer is bad implement in the option because explicit paramater for lambda is (String f)->... and the compiler expect Object, the correct is : 1- Consumer x = x -> System.out.print(x) or: 2- Consumer<String> x = (String f) -> ..... o 3- Consumer<String> x= f -> sout...

[Removed]Option: B

Actually answer A is also incorrect. As Consumer is actually Consumer<Object> so it expects an Object rather than a String. It would've worked if it were: f -> System.out.println(f);

tmuralimanoharOption: A

Answer:A

StavokOption: A

forEach() accepts a Consumer, correct answer is A.

Mukes877Option: A

forEach() accepts a Consumer, so correct answer is A.

Ankit1010Option: A

forEach() accepts a Consumer, so correct answer is A. Consumer function = f -> System.out.println(f);