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

Given the code fragments:

interface CourseFilter extends Predicate {

public default boolean test (String str) {

return str.contains ("Java");

}

}

and

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

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

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

public boolean test (String s) {

return s.startsWith ("Java");

}

};

long c = strs.stream()

.filter(cf1)

.filter(cf2 //line n2

.count();

System.out.println(c);

What is the result?

    Correct Answer: A

    The code defines an interface CourseFilter that extends Predicate and overrides the test method to return true if the string contains 'Java'. A list of strings is given and two Predicate objects are created. The first Predicate cf1 filters strings longer than three characters. The second Predicate cf2 is an anonymous class that checks if the string starts with 'Java'. When the stream is filtered, it will first filter out strings with a length of 3 or less ('Java EE' and 'Embedded Java' both pass). The second filter will pass 'Java' and 'Java EE' since both start with 'Java'. So, the count of matching strings is 2. Therefore, the result is 2.

Discussion
HuimOption: A

When no bracket is missing, then A.

vancuver

Tested, true

WilsonKKerllOption: A

Answer is A.

steefaandOption: A

A assuming missing bracket is typo.

duydnOption: A

A is correct, the bracket just a typo

Svetleto13Option: A

Ok.I tested the code with JDK8 and JDK14.Both gave answer A.So I dont know how others got answer B?

maslacOption: B

Answer is B. Tested

MilkBiscuitOption: D

Seriously? because line n2 misses a closing bracket?

mevlt

I believe that is just a typo.