Exams Knowledge Hub

MCQs for Competitive Exams, School & College Exams

Module: | Multithreading, Collections & I/O Streams

Q69: Consider the following statements regarding the Iterable forEach method:

1. Java 8 heavily modified the fundamental java.lang.Iterable interface by adding a default forEach() method, granting internal iteration capabilities directly to standard Collections.
2. The forEach() method strictly mandates the developer to pass a Predicate functional interface to mathematically dictate the exact iteration sequence.
3. Utilizing forEach() in conjunction with a method reference (e.g., list.forEach(System.out::println)) is structurally identical to using a lambda expression but offers superior syntactical conciseness.

Which of the above statements is/are correct?
A
Only 1 and 2
B
Only 1 and 3
C
Only 2 and 3
D
1, 2, and 3 [AnswerInfo: Direct Answer: The correct combination is 1 and 3. Statement 2 is incorrect because the forEach() method strictly requires a Consumer functional interface, not a Predicate. It simply needs a block of code to accept the element and perform an action on it, without returning a boolean evaluation. Concept Definition: Internal Iteration shifts the responsibility of traversing a collection from the developer (who historically used external for loops) directly to the Collection class itself via the forEach() method. Structural Breakdown: The signature in the Iterable interface is default void forEach(Consumer\
✅ Correct Answer: B