Govt. Exams
Entrance Exams
List numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
The filter operation with lambda expression (n -> n % 2 == 0) filters even numbers only. So 2 and 4 are printed, each on a new line due to println.
BiPredicate<T, U> is a functional interface that takes two parameters and returns a boolean. This matches the requirement of taking two integers and returning a boolean value.
The lambda expression x -> x * 2 multiplies the input by 2. When applied to 5, it returns 10.
When a lambda has no parameters, empty parentheses () are required before the arrow. Options A and D are syntactically incorrect.
The arrow (->) in lambda expressions separates the parameter list on the left from the method body on the right. It's a syntax element specific to lambda expressions.
Predicate<T> is a functional interface that takes a single input of type T and returns a boolean. It's commonly used for filtering operations in streams.
The syntax (x, y,) is incorrect because there's a trailing comma after y. The correct syntax would be (x, y) -> x + y.
@FunctionalInterface is the standard annotation introduced in Java 8 to explicitly mark an interface as a functional interface. It helps in compile-time checking.
A functional interface must have exactly one abstract method. This is the defining characteristic that allows it to be used with lambda expressions and method references.
Supplier greeting = () -> "Hello";
System.out.println(greeting.get());
Supplier.get() returns the value produced by the lambda expression, which is "Hello".