Showing 81–90 of 100 questions
in Lambda Expressions
What is the type inference mechanism in lambda expressions?
A
Compiler infers parameter types from functional interface and context
B
Parameters must always have explicit types
C
Type inference only works with single parameter
D
Lambda expressions don't support type inference
Correct Answer:
A. Compiler infers parameter types from functional interface and context
EXPLANATION
Compiler uses target type (functional interface) to infer parameter types. If context is clear, explicit types not needed: list.forEach(x -> ...).
In Java streams, what does the filter() method use?
A
Predicate functional interface
B
Function functional interface
C
Consumer functional interface
D
Supplier functional interface
Correct Answer:
A. Predicate functional interface
EXPLANATION
filter() method in streams takes Predicate which tests condition and returns boolean to determine if element should be included.
What will happen if you try to compile this code?
BiFunction concat = (a, b) -> a + b;
concat.apply("Java", "8", "Features");
A
It will compile and run successfully
B
Runtime error - extra argument
C
Compilation error - too many arguments
D
No output produced
Correct Answer:
C. Compilation error - too many arguments
EXPLANATION
BiFunction takes exactly 2 parameters. Passing 3 arguments is compilation error. apply() only accepts 2 arguments.
Which functional interface is best suited for this scenario: method that takes no parameters and returns a random number?
A
Supplier
B
Consumer
C
Predicate
D
Function
Correct Answer:
A. Supplier
EXPLANATION
Supplier<T> takes no parameters () and returns T. Perfect for generating/providing values like random numbers.
Can a lambda expression have multiple statements in its body?
A
No, lambda can only have single expression
B
Yes, using curly braces and explicit return statement
C
Yes, but only if using arrow syntax
D
No, compile will throw error
Correct Answer:
B. Yes, using curly braces and explicit return statement
EXPLANATION
Lambda can have multiple statements using braces: (x,y) -> { int z = x+y; return z; }. Single expression needs no braces or return.
What is the output of this code?
Consumer print = s -> System.out.println(s.toUpperCase());
print.accept("java");
A
java
B
JAVA
C
Compilation error
D
java JAVA
EXPLANATION
Consumer accepts one argument and returns nothing. print.accept("java") calls lambda which prints s.toUpperCase() = "JAVA".
Which of the following statements about @FunctionalInterface is true?
A
It is mandatory for all functional interfaces
B
It's optional but helps compiler verify exactly one abstract method
C
It prevents inheritance of the interface
D
It makes the interface static
Correct Answer:
B. It's optional but helps compiler verify exactly one abstract method
EXPLANATION
@FunctionalInterface is optional annotation that helps compiler verify interface has exactly one abstract method, catching errors early.
What will be the output of this nested lambda?
Function add = x -> y -> x + y;
System.out.println(add.apply(3).apply(5));
A
8
B
3
C
5
D
Compilation error
EXPLANATION
Curried function: add.apply(3) returns a Function that adds 3. .apply(5) on that adds 5 to 3, returning 8.
What is the difference between a lambda expression and an anonymous inner class?
A
Lambda is more concise, doesn't create separate class file
B
Anonymous inner class is faster
C
Lambda can only be used with functional interfaces
D
Both A and C
Correct Answer:
D. Both A and C
EXPLANATION
Lambdas provide concise syntax, are compiled directly without separate class files, and work only with functional interfaces (single abstract method).
Which of the following correctly represents a BiFunction?
A
(int a, int b) -> a + b
B
() -> 5
C
x -> x * 2
D
() -> System.out.println("Hi")
Correct Answer:
A. (int a, int b) -> a + b
EXPLANATION
BiFunction takes 2 parameters and returns a result. (int a, int b) -> a + b fits this. Others are Function, Supplier, Consumer respectively.