Showing 91–100 of 100 questions
in Lambda Expressions
Can lambda expressions access local variables from their enclosing scope?
A
Yes, but only if they are final or effectively final
B
Yes, any local variable can be accessed
C
No, lambda expressions cannot access local variables
D
Only static variables can be accessed
Correct Answer:
A. Yes, but only if they are final or effectively final
EXPLANATION
Lambda expressions can access local variables only if they are final or effectively final (not modified after initialization) due to closure requirements.
What will this code print?
List list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach(x -> System.out.print(x + " "));
A
1 2 3 4 5
B
1 2 3 4 5
C
Compilation error
D
15
Correct Answer:
B. 1 2 3 4 5
EXPLANATION
forEach with Consumer lambda prints each element followed by space, resulting in '1 2 3 4 5 ' (with trailing space).
Which functional interface should be used for a lambda that filters elements?
A
Predicate
B
Function
C
Consumer
D
Supplier
Correct Answer:
A. Predicate
EXPLANATION
Predicate<T> tests a condition and returns boolean. It's ideal for filtering. Function would return any type, Consumer doesn't return anything.
What is the type of the following lambda expression: x -> x.length()?
A
Function
B
Consumer
C
Supplier
D
Predicate
Correct Answer:
A. Function
EXPLANATION
Function<T,R> takes one argument of type T and returns R. Here String -> Integer (length), so Function<String, Integer>.
Which of the following lambda expressions is invalid?
A
() -> System.out.println("Hello")
B
x -> x * 2
C
(int x, int y) -> { return x + y; }
D
-> x + y
Correct Answer:
D. -> x + y
EXPLANATION
Lambda must have parameter list (even if empty with ()). Missing parameter list '() -> x + y' is invalid syntax.
What will be the output of the following code?
UnaryOperator square = x -> x * x;
System.out.println(square.apply(5));
A
25
B
5
C
Compilation error
D
Runtime error
EXPLANATION
UnaryOperator applies a function on its argument and returns result of same type. square.apply(5) returns 5*5 = 25.
Which package contains the built-in functional interfaces in Java?
A
java.util.function
B
java.lang.function
C
java.io.function
D
java.util.interface
Correct Answer:
A. java.util.function
EXPLANATION
The java.util.function package contains functional interfaces like Predicate, Consumer, Function, Supplier, and BiFunction introduced in Java 8.
What is a functional interface in Java?
A
An interface with exactly one abstract method
B
An interface that extends multiple interfaces
C
An interface with default methods only
D
An interface with no methods
Correct Answer:
A. An interface with exactly one abstract method
EXPLANATION
A functional interface has exactly one abstract method. It can have multiple default methods. The @FunctionalInterface annotation can be used to mark it.
Which of the following is a valid lambda expression syntax in Java?
A
(int x, int y) -> x + y
B
(x, y) => x + y
C
[x, y] -> x + y
D
(x, y) : x + y
Correct Answer:
A. (int x, int y) -> x + y
EXPLANATION
Valid Java lambda syntax uses arrow operator (->). Parameters can be typed or untyped, and the expression or block follows the arrow.
What is the primary purpose of lambda expressions introduced in Java 8?
A
To provide a concise syntax for implementing functional interfaces
B
To replace all anonymous inner classes
C
To improve memory management
D
To simplify exception handling
Correct Answer:
A. To provide a concise syntax for implementing functional interfaces
EXPLANATION
Lambda expressions provide a concise way to implement single abstract method (functional interfaces) without verbose anonymous class syntax.