Govt. Exams
Entrance Exams
filter(x -> x > 2) keeps 3, 4, 5. map(x -> x * 2) transforms them to 6, 8, 10. forEach prints each value.
range(1, 4) generates [1, 2, 3]. After map(x -> x * 2): [2, 4, 6]. Sum = 2 + 4 + 6 = 12.
The Comparator<T> functional interface has the method compare(T a, T b) which returns an int, matching this lambda's behavior.
peek() is an intermediate operation used for debugging; it doesn't end the stream. forEach() is a terminal operation that consumes the stream.
Method reference (::) is syntactic sugar for lambda expressions. System.out::println is equivalent to x -> System.out.println(x).
Both collect() and forEach() are valid terminal operations. collect() returns a list, forEach() performs an action. reduce() is also valid but would sum the squares.
In option C, you cannot specify types in lambda parameters when using implicit typing. It should be either (x, y) or explicitly define all parameters.
Optional is used to handle absence of values gracefully. Methods like ifPresent(lambda) and orElse() help prevent null pointer exceptions.
Function<Integer, Integer> takes an Integer and returns an Integer. f.apply(5) computes 5 * 5 = 25.
The map() with lambda x -> x * 2 doubles each element: 1*2=2, 2*2=4, 3*2=6, 4*2=8, resulting in [2, 4, 6, 8].