Govt. Exams
Entrance Exams
Function<String, Integer> takes a String input and returns an Integer (the length). This matches the lambda that takes a String parameter and returns s.length().
The return type of a lambda expression is inferred from the functional interface it's assigned to. The same lambda can return Integer or int depending on the context.
BiConsumer printer = (a, b) -> System.out.println(a + b);
printer.accept("Java", "8");
BiConsumer accepts two parameters and performs the action. String concatenation of "Java" + "8" produces "Java8".
Method reference Integer::parseInt is equivalent to lambda (s) -> Integer.parseInt(s).
What does this lambda expression do?
By reversing the compareTo order (s2 compared to s1), it sorts in descending order.
Lambda expressions can only access local variables that are effectively final (not modified after initialization).
Parameter type must be inferred from the functional interface it's assigned to, as it's not explicitly declared.
Both A and C are valid. Option B fails because variable x is already declared as parameter. Option C uses var keyword (Java 10+).
Both method reference (::) and lambda expression are valid ways to print list elements. Option B has incorrect syntax.
Function<T, R> takes parameter T and returns R. Consumer<T> takes parameter T and returns void.