Java Programming — Lambda Expressions
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 61–70 of 100 questions in Lambda Expressions
What will be the output of: List list = Arrays.asList("a", "bb", "ccc"); list.forEach(s -> System.out.print(s.length() + " "));
A 1 2 3
B a bb ccc
C 3 2 1
D 1 1 1
Correct Answer:  A. 1 2 3
EXPLANATION

The lambda expression prints the length of each string. Lengths are 1, 2, and 3 respectively, with spaces between them.

Take Test
What is the primary advantage of using lambda expressions in Java 8+?
A They reduce boilerplate code and enable functional programming style
B They increase execution speed significantly
C They eliminate the need for classes
D They are required for all stream operations
Correct Answer:  A. They reduce boilerplate code and enable functional programming style
EXPLANATION

Lambda expressions reduce boilerplate code by allowing inline implementation of functional interfaces without creating anonymous inner classes, enabling a more functional programming style.

Take Test
Consider: Function curry = x -> y -> x + y; What does curry.apply(5).apply(3) return?
A 8
B 15
C 5
D 3
Correct Answer:  A. 8
EXPLANATION

This is a curried function. curry.apply(5) returns a function that adds 5. Applying that with 3 gives 5 + 3 = 8.

Take Test
What issue can arise when using lambda expressions with flatMap() in nested streams?
A Excessive memory consumption from creating multiple intermediate streams
B flatMap() cannot accept lambda expressions
C Type erasure causes ClassCastException
D Lambda expressions become unreadable
Correct Answer:  A. Excessive memory consumption from creating multiple intermediate streams
EXPLANATION

Each flatMap creates intermediate streams. Deeply nested flatMap with lambdas can consume significant memory, especially with large datasets.

Take Test
Consider: List list = Arrays.asList("a", "b", "c"); list.stream().collect(Collectors.toMap(s -> s, s -> s.length())). What type is returned?
A Map
B Map
C List
D Set
Correct Answer:  A. Map
EXPLANATION

Collectors.toMap with key mapper (s -> s) produces String keys and value mapper (s -> s.length()) produces Integer values, resulting in Map<String, Integer>.

Take Test
What is the time complexity of reducing a stream with a lambda expression using reduce()?
A O(n) in sequential, potentially O(log n) in parallel with proper associative operation
B Always O(1)
C O(n²) regardless of parallelism
D O(n log n) always
Correct Answer:  A. O(n) in sequential, potentially O(log n) in parallel with proper associative operation
EXPLANATION

Sequential reduce is O(n). Parallel reduction can be O(log n) with a properly associative and commutative operation, enabling divide-and-conquer approach.

Take Test
Consider a lambda that needs to throw a checked exception. Which approach is correct?
A Wrap the checked exception in a try-catch block within the lambda
B Create a custom functional interface with throws clause
C Use @FunctionalInterface with throws declaration
D Lambda expressions cannot throw checked exceptions
Correct Answer:  A. Wrap the checked exception in a try-catch block within the lambda
EXPLANATION

Lambda expressions don't propagate checked exceptions. They must be caught within the lambda body using try-catch.

Take Test
Q.68 Medium Lambda Expressions
What will be the output of: IntStream.range(1, 4).map(x -> x * 2).sum();
A 12
B 9
C 6
D 24
Correct Answer:  A. 12
EXPLANATION

range(1, 4) generates [1, 2, 3]. After map(x -> x * 2): [2, 4, 6]. Sum = 2 + 4 + 6 = 12.

Take Test
Q.69 Medium Lambda Expressions
In the lambda expression (a, b) -> a.compareTo(b), what is the implicit functional interface?
A Comparator
B BiFunction
C BiPredicate
D BinaryOperator
Correct Answer:  A. Comparator
EXPLANATION

The Comparator<T> functional interface has the method compare(T a, T b) which returns an int, matching this lambda's behavior.

Take Test
Q.70 Medium Lambda Expressions
What is the difference between peek() and forEach() when used with lambda expressions in Streams?
A peek() is intermediate and doesn't consume the stream, forEach() is terminal and consumes it
B forEach() is faster than peek()
C peek() can only be used with Collectors
D forEach() returns a value while peek() returns void
Correct Answer:  A. peek() is intermediate and doesn't consume the stream, forEach() is terminal and consumes it
EXPLANATION

peek() is an intermediate operation used for debugging; it doesn't end the stream. forEach() is a terminal operation that consumes the stream.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips