Home Subjects Java Programming Lambda Expressions

Java Programming
Lambda Expressions

Java OOP, collections, multithreading

51 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 31–40 of 51
Topics in Java Programming
Q.31 Medium Lambda Expressions
Consider: Stream.of(1, 2, 3, 4, 5).filter(x -> x > 2).map(x -> x * 2).forEach(System.out::println); What will be the output?
A 6 8 10
B 2 4 6 8 10
C 3 4 5
D 1 2 3 4 5
Correct Answer:  A. 6 8 10
EXPLANATION

filter(x -> x > 2) keeps 3, 4, 5. map(x -> x * 2) transforms them to 6, 8, 10. forEach prints each value.

Test
Q.32 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.

Test
Q.33 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.

Test
Q.34 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.

Test
Q.35 Medium Lambda Expressions
What is method reference (::) in Java and how does it relate to lambda expressions?
A Method reference is a shorthand for lambda expressions that call a single method
B Method reference is used for declaring new methods
C Method reference is faster than lambda but less readable
D Method reference can only be used with static methods
Correct Answer:  A. Method reference is a shorthand for lambda expressions that call a single method
EXPLANATION

Method reference (::) is syntactic sugar for lambda expressions. System.out::println is equivalent to x -> System.out.println(x).

Test
Q.36 Medium Lambda Expressions
Consider: Stream.of(1, 2, 3, 4, 5).filter(x -> x > 2).map(x -> x * x). What will the terminal operation need to be?
A collect(Collectors.toList())
B forEach(System.out::println)
C Either a or b
D reduce(0, Integer::sum)
Correct Answer:  C. Either a or b
EXPLANATION

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.

Test
Q.37 Medium Lambda Expressions
Which of the following lambda expressions is INCORRECT?
A () -> 42
B x -> x + 1
C (int x, int y) -> x + y
D (x, y) -> (x > y) ? x : y
Correct Answer:  C. (int x, int y) -> x + y
EXPLANATION

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.

Test
Q.38 Medium Lambda Expressions
What is the purpose of the Optional class when used with lambda expressions in Stream operations?
A To handle null values and avoid NullPointerException
B To increase stream processing speed
C To convert streams to lists automatically
D To sort elements in reverse order
Correct Answer:  A. To handle null values and avoid NullPointerException
EXPLANATION

Optional is used to handle absence of values gracefully. Methods like ifPresent(lambda) and orElse() help prevent null pointer exceptions.

Test
Q.39 Medium Lambda Expressions
Consider: Function f = x -> x * x; What will f.apply(5) return?
A 25
B 10
C 5
D 30
Correct Answer:  A. 25
EXPLANATION

Function<Integer, Integer> takes an Integer and returns an Integer. f.apply(5) computes 5 * 5 = 25.

Test
Q.40 Medium Lambda Expressions
What will be the result of executing: List nums = Arrays.asList(1, 2, 3, 4); nums.stream().map(x -> x * 2).collect(Collectors.toList());
A [2, 4, 6, 8]
B [1, 2, 3, 4]
C [4, 8, 12, 16]
D [3, 4, 5, 6]
Correct Answer:  A. [2, 4, 6, 8]
EXPLANATION

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

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