Showing 41–50 of 100 questions
in 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.
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.
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.
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.
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.
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].
Which stream operation would you use to transform elements from one type to another?
A
filter()
B
map()
C
forEach()
D
collect()
EXPLANATION
map() is the intermediate operation used to transform elements from one type/value to another. It applies a function to each element.
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.
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
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).
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.