Showing 91–100 of 958 questions
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).
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.