What is the result of this code?
UnaryOperator<Integer> square = x -> x * x;
System.out.println(square.apply(5));
Answer: A
UnaryOperator applies the operation x * x where x = 5, resulting in 25.
Q.862Easy
Which of the following best describes a functional interface?
Answer: B
A functional interface must have exactly one abstract method, which can be implemented using lambda expressions.
Q.863Easy
What will be the output of:
Supplier<String> greeting = () -> "Hello";
System.out.println(greeting.get());
Answer: A
Supplier.get() returns the value produced by the lambda expression, which is "Hello".
Q.864Medium
Consider: Comparator<String> comp = (s1, s2) -> s2.compareTo(s1);
What does this lambda expression do?
Answer: B
By reversing the compareTo order (s2 compared to s1), it sorts in descending order.
Q.865Medium
Which lambda expression is equivalent to the method reference Integer::parseInt?
Answer: A
Method reference Integer::parseInt is equivalent to lambda (s) -> Integer.parseInt(s).
Advertisement
Q.866Hard
What is the correct way to chain lambda expressions using Function interface?
Function<Integer, Integer> f1 = x -> x * 2;
Function<Integer, Integer> f2 = x -> x + 5;
How to apply f1 first, then f2?
Answer: D
Both andThen and explicit composition order result in applying f1 first (3*2=6, then 6+5=11), equivalent to option C.
Q.867Medium
Analyze: What is the output?
BiConsumer<String, String> printer = (a, b) -> System.out.println(a + b);
printer.accept("Java", "8");
Answer: A
BiConsumer accepts two parameters and performs the action. String concatenation of "Java" + "8" produces "Java8".
Q.868Easy
Which of the following is a valid functional interface that can be used with lambda expressions?
Answer: A
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.
Q.869Medium
What is the return type of a lambda expression (x, y) -> x + y where x and y are integers?
Answer: C
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.
Q.870Easy
Which annotation is used to mark an interface as a functional interface in Java?
Answer: B
@FunctionalInterface is the standard annotation introduced in Java 8 to explicitly mark an interface as a functional interface. It helps in compile-time checking.
Q.871Medium
Consider a lambda expression: (String s) -> s.length(). What is the correct functional interface for this?
Answer: A
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().
Q.872Medium
What will happen if you try to access a local variable from an enclosing scope that is not final or effectively final in a lambda expression?
Answer: B
Lambda expressions can only access local variables that are final or effectively final. This is because lambda expressions are translated to methods that need access to stable variable values.
Q.873Easy
Which of the following lambda expressions is syntactically incorrect?
Answer: C
The syntax (x, y,) is incorrect because there's a trailing comma after y. The correct syntax would be (x, y) -> x + y.
Q.874Easy
What is the purpose of a Predicate functional interface in Java?
Answer: C
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.
Q.875Medium
Consider the code: List<String> names = Arrays.asList("Alice", "Bob"); names.forEach(name -> System.out.println(name)); What type of functional interface is used in forEach?
Answer: B
forEach accepts a Consumer functional interface. Consumer<T> takes an input and performs an action without returning anything, which matches the System.out.println action.
Q.876Medium
What is the output of: List<Integer> nums = Arrays.asList(1,2,3); nums.stream().filter(x -> x > 1).forEach(System.out::println);
Answer: B
filter(x -> x > 1) keeps only elements greater than 1, which are 2 and 3. These are then printed on separate lines using forEach.
Q.877Medium
Which lambda expression correctly implements a custom functional interface: public interface Math { int calculate(int a, int b); }
Answer: D
Both expressions are valid. Option A uses type inference while Option C explicitly specifies types. Both can implement the calculate method.
Q.878Easy
In a lambda expression, what does the arrow (->) operator represent?
Answer: B
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.
Q.879Medium
Consider: Comparator<Integer> comp = (a, b) -> b - a; List<Integer> list = Arrays.asList(3,1,2); Collections.sort(list, comp); What is the result?
Answer: B
The comparator (a, b) -> b - a returns negative values when b < a, causing elements to be sorted in descending order. Result is [3, 2, 1].
Q.880Medium
Which of the following correctly uses a method reference as an alternative to a lambda expression?
Answer: D
All three are valid method references that can replace their equivalent lambda expressions. Method references are a shorthand notation introduced in Java 8.