Showing 31–40 of 100 questions
in Lambda Expressions
What will be the output of:
Supplier greeting = () -> "Hello";
System.out.println(greeting.get());
A
Hello
B
null
C
Compilation error
D
Empty string
EXPLANATION
Supplier.get() returns the value produced by the lambda expression, which is "Hello".
Which of the following best describes a functional interface?
A
An interface that can only have lambda expressions
B
An interface with exactly one abstract method
C
An interface with all methods as default
D
An interface with multiple abstract methods that are related
Correct Answer:
B. An interface with exactly one abstract method
EXPLANATION
A functional interface must have exactly one abstract method, which can be implemented using lambda expressions.
What is the result of this code?
UnaryOperator square = x -> x * x;
System.out.println(square.apply(5));
A
25
B
10
C
5
D
Compilation error
EXPLANATION
UnaryOperator applies the operation x * x where x = 5, resulting in 25.
Which of the following statements about variable scope in lambda expressions is TRUE?
A
Lambda can access any local variable
B
Lambda can only access effectively final local variables
C
Lambda has its own scope separate from enclosing scope
D
Lambda can modify local variables from enclosing scope
Correct Answer:
B. Lambda can only access effectively final local variables
EXPLANATION
Lambda expressions can only access local variables that are effectively final (not modified after initialization).
In the lambda expression (x) -> x < 10 && x > 0, what is the parameter type?
A
Explicitly specified as int
B
Must be inferred from context
C
Cannot be a numeric type
D
Automatically defaults to Integer
Correct Answer:
B. Must be inferred from context
EXPLANATION
Parameter type must be inferred from the functional interface it's assigned to, as it's not explicitly declared.
What is the output of:
BiFunction add = (a, b) -> a + b;
System.out.println(add.apply(5, 3));
A
8
B
53
C
Compilation error
D
Runtime exception
EXPLANATION
BiFunction takes two Integer parameters and returns their sum. apply(5, 3) returns 5 + 3 = 8.
Which of the following lambda expressions will compile successfully?
A
(int x) -> { int y = x * 2; return y; }
B
(int x) -> { int x = 5; return x; }
C
(int x) -> { var y = x; return y; }
D
A and C
Correct Answer:
D. A and C
EXPLANATION
Both A and C are valid. Option B fails because variable x is already declared as parameter. Option C uses var keyword (Java 10+).
Which of the following uses method reference correctly?
A
list.forEach(System.out::println)
B
list.forEach(System.out.println)
C
list.forEach(x -> System.out.println(x))
D
Both A and C
Correct Answer:
D. Both A and C
EXPLANATION
Both method reference (::) and lambda expression are valid ways to print list elements. Option B has incorrect syntax.
What is the difference between Consumer and Function functional interfaces?
A
Consumer returns a value, Function doesn't
B
Function returns a value, Consumer doesn't
C
Both are identical
D
Consumer can have multiple parameters, Function cannot
Correct Answer:
B. Function returns a value, Consumer doesn't
EXPLANATION
Function<T, R> takes parameter T and returns R. Consumer<T> takes parameter T and returns void.
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).