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

Given:

and the code fragment:

Which modification enables the code fragment to print Speaker?

    Correct Answer: B

    To enable the code fragment to print 'Speaker', the method 'isAvailable' inside the 'ProductFilter' class must be static. This is because you're using a method reference to 'ProductFilter::isAvailable' in the stream filter, which requires 'isAvailable' to be a static method. Therefore, you need to replace line n1 with 'public static boolean isAvailable(Product p).'

Discussion
Svetleto13Option: B

B,tested

asdfjhfgjuaDCVOption: B

B is the correct answer

steefaandOption: B

B, since you can't call nonstatic method from static context

push05Option: C

import java.util.Arrays; import java.util.List; class Product { String name; int qty; public String toString(){ return name; } public Product(String name, int qty) { this.name = name; this.qty = qty; } static class ProductFilter { public static boolean isAvailable(Product p) { // line nl return p.qty >= 10; } public static void main (String [] args) { List<Product> products = Arrays.asList( new Product("MotherBoard", 5), new Product("Speaker", 20)) ; products.stream() .filter(Product.ProductFilter::isAvailable) // line n2 .forEach(p -> System.out.println(p)); } } }