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.82Medium
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.83Medium
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.84Medium
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.85Easy
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.
Advertisement
Q.86Medium
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.87Medium
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.
Q.88Easy
What is the correct syntax for a lambda expression with no parameters that returns a fixed value?
Answer: B
When a lambda has no parameters, empty parentheses () are required before the arrow. Options A and D are syntactically incorrect.
Q.89Easy
Given: Function<Integer, Integer> f = x -> x * 2; Integer result = f.apply(5); What is the value of result?
Answer: B
The lambda expression x -> x * 2 multiplies the input by 2. When applied to 5, it returns 10.
Q.90Medium
Which stream operation uses a lambda expression to transform elements from one type to another?
Answer: B
map() is used to transform elements. It takes a Function lambda that converts each element from one type/value to another.
Q.91Hard
Consider a scenario where you need to sort a list of strings by length. Which lambda expression is most appropriate for Comparator?
Answer: D
Both expressions correctly sort by length. Option B uses Integer.compare (safer for int overflow), while Option C uses direct subtraction. Both work correctly here.
Q.92Hard
What is the difference between a lambda expression and an anonymous inner class in Java?
Answer: D
Lambda expressions can only be used with functional interfaces (single abstract method), whereas anonymous classes can implement any interface. Lambda expressions are also more concise.
Q.93Hard
In the context of exception handling with lambda expressions, what happens if a lambda body throws a checked exception?
Answer: B
If a lambda body throws a checked exception, it must be declared in the functional interface's method signature. Otherwise, compilation will fail.
Q.94Hard
Which of the following best demonstrates the use of Optional with lambda expressions for safe null handling?
Answer: A
Optional with map() using a lambda expression is the functional programming approach. It's safe, readable, and chains operations effectively. Options B, C, and D are imperative approaches.
Q.95Easy
Which functional interface is used to create a lambda expression that takes two integers and returns a boolean value?
Answer: A
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.
Q.96Easy
What will be the output of the following code?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
Answer: B
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.
Q.97Medium
Which of the following lambda expressions is INVALID in Java?
Answer: D
In a lambda expression, if the body uses curly braces, 'return' keyword is required. But without curly braces, 'return' keyword cannot be used. Option D is invalid because it has 'return' without curly braces.
Q.98Medium
A stream processes a list of strings and applies a lambda to convert them to uppercase. Which intermediate operation should be used?
Answer: B
The map() intermediate operation transforms each element using the provided lambda function. filter() returns boolean, peek() doesn't transform, and collect() is a terminal operation. map() is the correct choice for transformation.
Q.99Medium
Consider the following code:
List<String> fruits = Arrays.asList("apple", "banana", "cherry");
fruits.removeIf(s -> s.length() > 5);
What will be the contents of 'fruits' list after execution?
Answer: B
removeIf() removes all elements that satisfy the lambda predicate. Strings with length > 5 are "banana" (6 chars) and "cherry" (6 chars). Only "apple" (5 chars) remains as its length is not greater than 5.
Q.100Hard
What is the return type of a lambda expression used with IntStream.range(1, 5).map(x -> x * 2)?
Answer: C
map() is an intermediate operation that returns an IntStream. The lambda expression (x -> x * 2) transforms each int, but map() itself returns IntStream, not the transformed value type.