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

Given the code fragments:

interface CourseFilter extends Predicate {

public default boolean test (String str) {

return str.equals ("Java");

}

}

and

List strs = Arrays.asList("Java", "Java EE", "Java ME");

Predicate cf1 = s - > s.length() > 3;

Predicate cf2 = new CourseFilter() { //line n1

public boolean test (String s) {

return s.contains ("Java");

}

};

long c = strs.stream()

.filter(cf1)

.filter(cf2 //line n2

.count();

System.out.println(c);

What is the result?

    Correct Answer: A

    The provided code defines an interface 'CourseFilter' that extends 'Predicate<String>' and overrides the 'test' method to return true if the string equals 'Java'. However, an anonymous class is created from 'CourseFilter' and overrides the 'test' method to return true if the string contains 'Java'. Given the list ['Java', 'Java EE', 'Java ME'], the first filter 'cf1' checks for strings longer than 3 characters, which are 'Java EE' and 'Java ME'. The second filter 'cf2' checks if the string contains 'Java', both 'Java EE' and 'Java ME' pass this filter as well. So, the stream will have two elements that pass both filters, resulting in a count of 2.

Discussion
Ritesh_Option: B

It will print 3

adnano1234Option: B

The answer is : 3

InnovationOption: B

output 3 is correct, tested

r1muka5Option: D

Compilation error occurs at line n2 as it's obviously missing parenthesis ')' so the actual answer is D. If it had compiled then the outpub would be 3 and the answer - C.

DangNHH

I agree with you

GaelBernard

I believe it's just a typo. ExamTopics has several mistakes of this type for the OCP exam.

asdfjhfgjuaDCVOption: B

B is the correct answer. import java.util.Arrays; import java.util.List; import java.util.function.Predicate; interface CourseFilter extends Predicate<String> { public default boolean test(String str) { return str.equals("Java"); } } public class Main { public static void main(String[] args) { List<String> strs = Arrays.asList("Java", "Java EE", "Java ME"); Predicate<String> cf1 = s -> s.length() > 3; Predicate cf2 = new CourseFilter() { // line n1 public boolean test(String s) { return s.contains("Java"); } }; long c = strs.stream() .filter(cf1) .filter(cf2) // line n2 .count(); System.out.println(c); } }

asdfjhfgjuaDCV

I think there is one typo in in //line2

steefaandOption: B

After fixing typos answer is B.

MudzingwaOption: C

C what about C can we create an instance of an Interface

WilsonKKerllOption: B

Answer is 3.........