Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard
Question 9
import java.util.function.Predicate;
public class PredicateTest {
public static void main(String[] args) {
String[] arr = {"A", "ab", "bab", "Aa", "bb", "baba", "aba", "Abab"};
processStringArray(arr, p -> true);
}
private static void processStringArray(String[] arr, Predicate<String> predicate) {
for (String str : arr) {
if (predicate.test(str)) {
System.out.println(str);
}
}
}
}
Which of the following options can replace p -> true in the PredicateTest class such that all the array elements are displayed in the output? Select ALL that apply.
-
A
p -> !false
-
B
p -> p.length() >= 1
-
C
p -> true
-
D
p -> p.length() < 10
Reveal correct answers
Correct answers: A, B, C, D
Explanation
p -> truemeans thetestmethod returns true for all the passed strings.p -> !falsemeans thetestmethod returns true for all the passed strings.p -> p.length() >= 1means thetestmethod returns true if the passed string's length is greater than or equal to 1, which is true for all the array elements.p -> p.length() < 10means thetestmethod returns true if the passed string's length is less than 10, which is true for all the array elements.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
