Given:
and the code fragment:Which modification enables the code fragment to print Speaker?
Given:
and the code fragment:Which modification enables the code fragment to print Speaker?
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).'
B,tested
B is the correct answer
B, since you can't call nonstatic method from static context
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)); } } }