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 -> true means the test method returns true for all the passed strings.

  • p -> !false means the test method returns true for all the passed strings.

  • p -> p.length() >= 1 means the test method 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() < 10 means the test method 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.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need