In Java 8, a lambda expression can access local variables from its enclosing scope. What is the constraint on these variables?
Answer: A
Lambda expressions can only access local variables that are final or effectively final to ensure thread safety and immutability.
Q.22Easy
What is the return type of the map() method when used with lambda expressions in Java Streams?
Answer: A
The map() method in Stream API returns a new Stream with transformed elements of type R, maintaining the stream pipeline.
Q.23Easy
Consider the code: List<String> names = Arrays.asList("Raj", "Priya", "Amit"); names.forEach(n -> System.out.println(n)); What does this code do?
Answer: A
forEach with the lambda expression n -> System.out.println(n) iterates through each element and prints it with a newline.
Q.24Easy
Which functional interface is used for operations that take no arguments and return a value?
Answer: A
Supplier<T> is a functional interface with method get() that takes no parameters and returns a value of type T.
Q.25Medium
What will be the result of executing: List<Integer> nums = Arrays.asList(1, 2, 3, 4); nums.stream().map(x -> x * 2).collect(Collectors.toList());
Answer: A
The map() with lambda x -> x * 2 doubles each element: 1*2=2, 2*2=4, 3*2=6, 4*2=8, resulting in [2, 4, 6, 8].
Advertisement
Q.26Medium
Consider: Function<Integer, Integer> f = x -> x * x; What will f.apply(5) return?
Answer: A
Function<Integer, Integer> takes an Integer and returns an Integer. f.apply(5) computes 5 * 5 = 25.
Q.27Medium
What is the purpose of the Optional class when used with lambda expressions in Stream operations?
Answer: A
Optional is used to handle absence of values gracefully. Methods like ifPresent(lambda) and orElse() help prevent null pointer exceptions.
Q.28Medium
Which of the following lambda expressions is INCORRECT?
Answer: C
In option C, you cannot specify types in lambda parameters when using implicit typing. It should be either (x, y) or explicitly define all parameters.
Q.29Medium
Consider: Stream.of(1, 2, 3, 4, 5).filter(x -> x > 2).map(x -> x * x). What will the terminal operation need to be?
Answer: C
Both collect() and forEach() are valid terminal operations. collect() returns a list, forEach() performs an action. reduce() is also valid but would sum the squares.
Q.30Medium
What is method reference (::) in Java and how does it relate to lambda expressions?
Answer: A
Method reference (::) is syntactic sugar for lambda expressions. System.out::println is equivalent to x -> System.out.println(x).
Q.31Medium
What is the difference between peek() and forEach() when used with lambda expressions in Streams?
Answer: A
peek() is an intermediate operation used for debugging; it doesn't end the stream. forEach() is a terminal operation that consumes the stream.
Q.32Medium
In the lambda expression (a, b) -> a.compareTo(b), what is the implicit functional interface?
Answer: A
The Comparator<T> functional interface has the method compare(T a, T b) which returns an int, matching this lambda's behavior.
Q.33Medium
What will be the output of: IntStream.range(1, 4).map(x -> x * 2).sum();
Answer: A
range(1, 4) generates [1, 2, 3]. After map(x -> x * 2): [2, 4, 6]. Sum = 2 + 4 + 6 = 12.
Q.34Hard
Consider a lambda that needs to throw a checked exception. Which approach is correct?
Answer: A
Lambda expressions don't propagate checked exceptions. They must be caught within the lambda body using try-catch.
Q.35Hard
What is the time complexity of reducing a stream with a lambda expression using reduce()?
Answer: A
Sequential reduce is O(n). Parallel reduction can be O(log n) with a properly associative and commutative operation, enabling divide-and-conquer approach.
Q.36Hard
Consider: List<String> list = Arrays.asList("a", "b", "c"); list.stream().collect(Collectors.toMap(s -> s, s -> s.length())). What type is returned?
Answer: A
Collectors.toMap with key mapper (s -> s) produces String keys and value mapper (s -> s.length()) produces Integer values, resulting in Map<String, Integer>.
Q.37Hard
What issue can arise when using lambda expressions with flatMap() in nested streams?
Answer: A
Each flatMap creates intermediate streams. Deeply nested flatMap with lambdas can consume significant memory, especially with large datasets.
Q.38Hard
Consider: Function<Integer, Function<Integer, Integer>> curry = x -> y -> x + y; What does curry.apply(5).apply(3) return?
Answer: A
This is a curried function. curry.apply(5) returns a function that adds 5. Applying that with 3 gives 5 + 3 = 8.
Q.39Easy
What is the primary advantage of using lambda expressions in Java 8+?
Answer: A
Lambda expressions reduce boilerplate code by allowing inline implementation of functional interfaces without creating anonymous inner classes, enabling a more functional programming style.
Q.40Easy
What will be the output of: List<String> list = Arrays.asList("a", "bb", "ccc"); list.forEach(s -> System.out.print(s.length() + " "));
Answer: A
The lambda expression prints the length of each string. Lengths are 1, 2, and 3 respectively, with spaces between them.