Given the code fragment:
List
Predicate
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?
Given the code fragment:
List
Predicate
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?
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'.
Answer is A
Correct Answer is A(tested)
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.
The correct answer is A.List<String> str = Arrays.asList ("my", "pen", "is", "your', "pen"); Check this once if it is correct then 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.
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"
Answer A
Answer is A. Because Predicate test() is one test with any element in List.