Java Programming — Lambda Expressions
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 51–60 of 100 questions in Lambda Expressions
Q.51 Medium Lambda Expressions
Which of the following correctly uses method reference with constructor?
A String::new
B new String::
C String->new
D new::String
Correct Answer:  A. String::new
EXPLANATION

Constructor method references use ClassName::new syntax. String::new refers to the String constructor and can be used wherever a Supplier<String> is expected.

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

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

Take Test
Q.54 Medium Lambda Expressions
In the lambda expression (a, b) -> a.compareTo(b), what can we infer about the parameters?
A Both a and b are integers
B Both a and b are Comparable objects
C a is a String and b is a number
D Types cannot be determined without context
Correct Answer:  D. Types cannot be determined without context
EXPLANATION

Without explicit type declarations or context, the compiler must infer types from usage. We can only determine they have a compareTo() method, likely Comparable types.

Take Test
Q.55 Medium Lambda Expressions
What will be the output of: IntStream.range(1, 4).map(x -> x * x).forEach(System.out::print);
A 149
B 123
C 111
D 000
Correct Answer:  A. 149
EXPLANATION

range(1, 4) generates 1, 2, 3. map(x -> x * x) produces 1, 4, 9. Printed without spaces gives '149'.

Take Test
Q.56 Medium Lambda Expressions
What issue can arise when using lambda expressions with checked exceptions?
A Checked exceptions cannot be thrown directly in lambda expressions
B They must be wrapped in try-catch or a custom functional interface must be created
C Checked exceptions automatically become unchecked in lambdas
D Lambda expressions cannot handle any exceptions
Correct Answer:  B. They must be wrapped in try-catch or a custom functional interface must be created
EXPLANATION

Since standard functional interfaces don't declare checked exceptions, you must either wrap in try-catch or create a custom functional interface that throws the checked exception.

Take Test
Q.57 Medium Lambda Expressions
What is the difference between peek() and forEach() in streams?
A peek() is terminal, forEach() is intermediate
B peek() is intermediate and returns a stream, forEach() is terminal and returns void
C There is no difference, they are aliases
D peek() modifies elements, forEach() doesn't
Correct Answer:  B. peek() is intermediate and returns a stream, forEach() is terminal and returns void
EXPLANATION

peek() is an intermediate operation that returns a stream for chaining, useful for debugging. forEach() is terminal and returns void, ending the stream chain.

Take Test
Q.58 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.

Take Test
What is a method reference (::) in Java?
A A shorthand syntax for lambda expressions that call existing methods
B An operator for accessing private members
C A way to create anonymous classes
D A reference to a method's memory address
Correct Answer:  A. A shorthand syntax for lambda expressions that call existing methods
EXPLANATION

Method references are compact lambda expressions that directly reference existing methods. They use :: syntax and are equivalent to lambdas that call those methods.

Take Test
Which functional interface is used for operations that take two arguments and return a single value?
A Function
B BiFunction
C Consumer
D Supplier
Correct Answer:  B. BiFunction
EXPLANATION

BiFunction<T, U, R> represents a function that accepts two arguments of types T and U, and returns a result of type R.

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