Home Subjects Java Programming Lambda Expressions

Java Programming
Lambda Expressions

Java OOP, collections, multithreading

17 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–17 of 17
Topics in Java Programming
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.

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>.

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.

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.

Test
What is the type inference mechanism in lambda expressions?
A Compiler infers parameter types from functional interface and context
B Parameters must always have explicit types
C Type inference only works with single parameter
D Lambda expressions don't support type inference
Correct Answer:  A. Compiler infers parameter types from functional interface and context
EXPLANATION

Compiler uses target type (functional interface) to infer parameter types. If context is clear, explicit types not needed: list.forEach(x -> ...).

Test
What will happen if you try to compile this code?
BiFunction concat = (a, b) -> a + b;
concat.apply("Java", "8", "Features");
A It will compile and run successfully
B Runtime error - extra argument
C Compilation error - too many arguments
D No output produced
Correct Answer:  C. Compilation error - too many arguments
EXPLANATION

BiFunction takes exactly 2 parameters. Passing 3 arguments is compilation error. apply() only accepts 2 arguments.

Test
What will be the output of this nested lambda?
Function add = x -> y -> x + y;
System.out.println(add.apply(3).apply(5));
A 8
B 3
C 5
D Compilation error
Correct Answer:  A. 8
EXPLANATION

Curried function: add.apply(3) returns a Function that adds 3. .apply(5) on that adds 5 to 3, returning 8.

Test
IGET
IGET AI
Online · Exam prep assistant
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