Govt. Exams
Entrance Exams
Lambda expressions can only access local variables that are final or effectively final to ensure thread safety and immutability.
Consumer print = s -> System.out.println(s.toUpperCase());
print.accept("java");
Consumer accepts one argument and returns nothing. print.accept("java") calls lambda which prints s.toUpperCase() = "JAVA".
UnaryOperator square = x -> x * x;
System.out.println(square.apply(5));
UnaryOperator applies a function on its argument and returns result of same type. square.apply(5) returns 5*5 = 25.
The java.util.function package contains functional interfaces like Predicate, Consumer, Function, Supplier, and BiFunction introduced in Java 8.
A functional interface has exactly one abstract method. It can have multiple default methods. The @FunctionalInterface annotation can be used to mark it.
Valid Java lambda syntax uses arrow operator (->). Parameters can be typed or untyped, and the expression or block follows the arrow.
Lambda expressions provide a concise way to implement single abstract method (functional interfaces) without verbose anonymous class syntax.
Java uses ampersand (&) to combine multiple bounds. The class must appear first, followed by interfaces. Commas, pipes, and 'implements' keyword are not valid syntax for type parameter bounds.
Option A uses single type parameter T for entity type, which is the standard pattern for generic repositories. Option C is redundant (all classes extend Object), and Option D adds unnecessary complexity.
Generics only work with reference types, not primitive types. Integer is the wrapper class for int, making option B valid.