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

Given the code fragment:

List str = Arrays.asList ("my", "pen", "is", "your', "pen");

Predicate test = s -> {

int i = 0;

boolean result = s.contains ("pen");

System.out.print(i++) + ":");

return result;

};

str.stream()

.filter(test)

.findFirst()

.ifPresent(System.out ::print);

What is the result?

    Correct Answer: A

    The result is '0 : 0 : pen'. The lambda expression in the Predicate increments the local variable 'i', which is initialised to 0 each time the Predicate is called. Because of this, 'i' always prints 0. The 'filter' method tests each element of the list and stops when it finds the first match, which is 'pen' in the second position. Thus, the 'findFirst()' method finds the element on the second check and prints '0 : 0 : pen'.

Discussion
DestroyerOption: A

Answer is A

SaurabhagOption: A

Correct Answer is A(tested)

wheredajuiceOption: A

It's A because of findFirst(), it only does the test for the first two elements, finds "pen" in the second position and terminates. Also notice "int i" is a local variable in the lambda, it always prints 0.

asdfjhfgjuaDCVOption: A

The correct answer is A.List<String> str = Arrays.asList ("my", "pen", "is", "your', "pen"); Check this once if it is correct then A

steefaandOption: A

Assuming that "your' is typo (using ' instead of ") answer is A, since variable i is created every time predicate is run and its value is always 0 because of post increment. And since value is found on 2nd iteration 0 is printed twice.

iSnoverOption: D

The answer is D, because in the list "str" ​​the fourth String is instantiated as "your' without double quotes as it should be. Correcting the error, the correct answer is A, pay attention to the test as the question may come with the model with compilation error or may come with the result "0 : 0 : pen"

r1muka5Option: A

Answer A

WilsonKKerllOption: A

Answer is A. Because Predicate test() is one test with any element in List.