Given the code fragments:
Which code fragment, when inserted at line n1, enables the code to print Hank?
A.
checkAge (iList, ( ) -> p. get Age ( ) > 40);
B.
checkAge(iList, Person p -> p.getAge( ) > 40);
C.
checkAge (iList, p -> p.getAge ( ) > 40);
D.
checkAge(iList, (Person p) -> { p.getAge() > 40; });
Answer: C
package q072;
public class Person {
String name;
int age;
public Person(String n, int a) {
name = n;
age = a;
}
public int getAge() {
return age;
}
}
package q072;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class Test {
public static void checkAge(List list, Predicate predicate) {
for (Person p : list) {
if (predicate.test(p)) {
System.out.println(p.name + ” “);
}
}
}
public static void main(String[] args) {
List iList = Arrays.asList(new Person(“Hank”, 45),
new Person(“Charlie”, 40),
new Person(“Smith”, 38));
//line n1
//checkAge(iList, () -> p.getAge() > 40); //A
//checkAge(iList, Person p -> p.getAge() > 40); //B
checkAge (iList, p -> p.getAge() > 40); //C
//checkAge(iList, (Person p) -> {p.getAge() > 40; });
}
}
/* Output
Hank
*/
C