Java Programming — Lambda Expressions
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 41–50 of 100 questions in Lambda Expressions
Q.41 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.

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

Take Test
What is the return type of the lambda expression: (a, b) -> a > b?
A int
B boolean
C void
D Cannot be determined
Correct Answer:  B. boolean
EXPLANATION

The expression uses comparison operator (>), which returns a boolean value.

Take Test
A lambda expression can only be used with which type of interface?
A Regular interface
B Functional interface with single abstract method
C Interface with multiple abstract methods
D Interface with default methods only
Correct Answer:  B. Functional interface with single abstract method
EXPLANATION

Lambda expressions can only be assigned to functional interfaces that have exactly one abstract method.

Take Test
Which of the following is a valid lambda expression in Java?
A () -> System.out.println("Hello")
B (int x, int y) -> { return x + y; }
C (String s) -> s.length()
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three are valid lambda expression syntaxes with different parameter types and return statements.

Take Test
Advertisement
Q.46 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].

Take Test
Which stream operation would you use to transform elements from one type to another?
A filter()
B map()
C forEach()
D collect()
Correct Answer:  B. map()
EXPLANATION

map() is the intermediate operation used to transform elements from one type/value to another. It applies a function to each element.

Take Test
What is the purpose of the @FunctionalInterface annotation in Java?
A To indicate an interface can be used with lambda expressions
B To enforce that an interface has exactly one abstract method
C To improve performance of lambda expressions
D To allow multiple lambda expressions in one interface
Correct Answer:  B. To enforce that an interface has exactly one abstract method
EXPLANATION

@FunctionalInterface is a compile-time check that ensures an interface has exactly one abstract method, making it safe for lambda implementation.

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

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

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