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 21–30 of 51
Topics in Java Programming
Q.21 Medium Lambda Expressions
Which functional interface should be used for a lambda with no parameters and no return value?
A Supplier
B Consumer
C Runnable
D Function
Correct Answer:  C. Runnable
EXPLANATION

Runnable is the functional interface for operations with no parameters and no return value (void).

Test
Q.22 Medium Lambda Expressions
What will be the type of the lambda expression (x) -> x.toString()?
A Function
B Function
C Cannot be determined without context
D Consumer
Correct Answer:  C. Cannot be determined without context
EXPLANATION

Without context, the parameter type cannot be inferred. The functional interface type determines the parameter type.

Test
Q.23 Medium Lambda Expressions
Which of the following lambda expressions has incorrect syntax?
A (int x) -> x * 2
B x -> x * 2
C (x, y) -> { return x + y; }
D (int x, int y) -> x > y ? x : y
Correct Answer:  D. (int x, int y) -> x > y ? x : y
EXPLANATION

Ternary operator inside lambda requires braces and explicit return statement for type inference.

Test
Q.24 Medium Lambda Expressions
What will be the output of: List list = Arrays.asList(1, 2, 3); list.replaceAll(x -> x * 2); System.out.println(list);
A [1, 2, 3]
B [2, 4, 6]
C [] (empty list)
D [2, 2, 2]
Correct Answer:  B. [2, 4, 6]
EXPLANATION

replaceAll() with a lambda expression modifies the list in-place, multiplying each element by 2. Result is [2, 4, 6].

Test
Q.25 Medium Lambda Expressions
What happens when a lambda expression references a local variable from its enclosing scope?
A The variable must be explicitly final or effectively final
B The variable is automatically copied into the lambda's scope
C Any variable can be accessed without restrictions
D The variable must be static
Correct Answer:  A. The variable must be explicitly final or effectively final
EXPLANATION

Lambda expressions can only access local variables that are final or effectively final (not reassigned). This ensures thread-safety and consistency.

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

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

Test
Q.28 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'.

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

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

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