Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 180

Given the code fragment:

List colors = Arrays.asList("red", "green", "yellow");

Predicate test = n - > {

System.out.println("Searching"¦");

return n.contains("red");

};

colors.stream()

.filter(c -> c.length() >= 3)

.allMatch(test);

What is the result?

    Correct Answer: B

    The code initializes a list with three strings and then creates a predicate to test if a string contains 'red'. The stream is filtered to include only strings with a length of at least 3 and then checks if all elements match the predicate. The output of 'allMatch' is short-circuiting, meaning that it stops evaluating as soon as a mismatch is found. In this case, 'red' will match, but 'green' will cause a mismatch and evaluation will stop before checking 'yellow'. Therefore, 'Searching' will be printed twice.

Discussion
Abdullah_RahahleahOption: B

Answer is B-tested

Svetleto13Option: B

B,tested

asdfjhfgjuaDCVOption: B

B is the correct answer

steefaandOption: B

Answer is B. On second iteration of stream it fails and third iteration isn't run.

iSnoverOption: B

Answer is B-tested

WilsonKKerllOption: B

Answer is B.