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 1–10 of 17
Topics in Java Programming
What is the return type of a lambda expression used with IntStream.range(1, 5).map(x -> x * 2)?
A void
B int
C IntStream
D Function
Correct Answer:  C. IntStream
EXPLANATION

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.

Test
Which of the following best demonstrates the use of Optional with lambda expressions for safe null handling?
A Optional.of(value).map(x -> x.toUpperCase()).orElse("default")
B if (value != null) { value.toUpperCase(); }
C try { value.toUpperCase(); } catch (NullPointerException e) { }
D value != null ? value.toUpperCase() : "default"
Correct Answer:  A. Optional.of(value).map(x -> x.toUpperCase()).orElse("default")
EXPLANATION

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.

Test
In the context of exception handling with lambda expressions, what happens if a lambda body throws a checked exception?
A The lambda expression automatically wraps it in RuntimeException
B It causes a compilation error unless the functional interface declares the exception
C It's always caught and ignored
D The functional interface must declare throws clause
Correct Answer:  B. It causes a compilation error unless the functional interface declares the exception
EXPLANATION

If a lambda body throws a checked exception, it must be declared in the functional interface's method signature. Otherwise, compilation will fail.

Test
What is the difference between a lambda expression and an anonymous inner class in Java?
A Lambda expressions are only for functional interfaces, while anonymous classes can implement any interface
B Lambda expressions are cleaner and more concise for functional interfaces
C Lambda expressions create new scope, while anonymous classes don't
D Both A and B
Correct Answer:  D. Both A and B
EXPLANATION

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.

Test
Consider a scenario where you need to sort a list of strings by length. Which lambda expression is most appropriate for Comparator?
A (s1, s2) -> s1.compareTo(s2)
B (s1, s2) -> Integer.compare(s1.length(), s2.length())
C (s1, s2) -> s1.length() - s2.length()
D Both B and C
Correct Answer:  D. Both B and C
EXPLANATION

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.

Test
What is the correct way to chain lambda expressions using Function interface?
Function f1 = x -> x * 2;
Function f2 = x -> x + 5;
How to apply f1 first, then f2?
A f1.andThen(f2).apply(3)
B f1.compose(f2).apply(3)
C f2.apply(f1.apply(3))
D A and C
Correct Answer:  D. A and C
EXPLANATION

Both andThen and explicit composition order result in applying f1 first (3*2=6, then 6+5=11), equivalent to option C.

Test
Consider: Stream.of(1, 2, 3).map(x -> { System.out.println(x); return x * 2; }).collect(Collectors.toList()); What will print?
A 1 2 3
B 2 4 6
C Nothing until the collect operation completes
D 1 2 3 2 4 6
Correct Answer:  A. 1 2 3
EXPLANATION

map() is an intermediate operation. The println executes because collect() is a terminal operation that triggers evaluation. It prints 1, 2, 3 (the original values).

Test
Consider: Function curried = a -> b -> a + b; What is this pattern called?
A Higher-order function with currying
B Nested lambda expression
C Function composition
D Stream reduction
Correct Answer:  A. Higher-order function with currying
EXPLANATION

This is a curried function - a higher-order function that takes one argument and returns another function taking the next argument. It transforms multi-argument functions into sequences of single-argument functions.

Test
What is the time complexity of reducing a stream with a stateful lambda operation?
A O(n) for sequential, O(log n) for parallel
B O(n) for both sequential and parallel streams
C O(1) regardless of stream size
D Depends on the operation and stream characteristics
Correct Answer:  D. Depends on the operation and stream characteristics
EXPLANATION

Time complexity depends on the specific reduction operation, whether the lambda is stateless, and stream characteristics. Generally O(n), but parallelization overhead affects actual performance.

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.

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