What is the primary purpose of lambda expressions introduced in Java 8?
Answer: A
Lambda expressions provide a concise way to implement single abstract method (functional interfaces) without verbose anonymous class syntax.
Q.2Easy
Which of the following is a valid lambda expression syntax in Java?
Answer: A
Valid Java lambda syntax uses arrow operator (->). Parameters can be typed or untyped, and the expression or block follows the arrow.
Q.3Easy
What is a functional interface in Java?
Answer: A
A functional interface has exactly one abstract method. It can have multiple default methods. The @FunctionalInterface annotation can be used to mark it.
Q.4Easy
Which package contains the built-in functional interfaces in Java?
Answer: A
The java.util.function package contains functional interfaces like Predicate, Consumer, Function, Supplier, and BiFunction introduced in Java 8.
Q.5Easy
What will be the output of the following code?
UnaryOperator<Integer> square = x -> x * x;
System.out.println(square.apply(5));
Answer: A
UnaryOperator applies a function on its argument and returns result of same type. square.apply(5) returns 5*5 = 25.
Advertisement
Q.6Medium
Which of the following lambda expressions is invalid?
Answer: D
Lambda must have parameter list (even if empty with ()). Missing parameter list '() -> x + y' is invalid syntax.
Q.7Medium
What is the type of the following lambda expression: x -> x.length()?
Answer: A
Function<T,R> takes one argument of type T and returns R. Here String -> Integer (length), so Function<String, Integer>.
Q.8Medium
Which functional interface should be used for a lambda that filters elements?
Answer: A
Predicate<T> tests a condition and returns boolean. It's ideal for filtering. Function would return any type, Consumer doesn't return anything.
Q.9Medium
What will this code print?
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach(x -> System.out.print(x + " "));
Answer: B
forEach with Consumer lambda prints each element followed by space, resulting in '1 2 3 4 5 ' (with trailing space).
Q.10Medium
Can lambda expressions access local variables from their enclosing scope?
Answer: A
Lambda expressions can access local variables only if they are final or effectively final (not modified after initialization) due to closure requirements.
Q.11Medium
Which of the following correctly represents a BiFunction?
Answer: A
BiFunction takes 2 parameters and returns a result. (int a, int b) -> a + b fits this. Others are Function, Supplier, Consumer respectively.
Q.12Medium
What is the difference between a lambda expression and an anonymous inner class?
Answer: D
Lambdas provide concise syntax, are compiled directly without separate class files, and work only with functional interfaces (single abstract method).
Q.13Hard
What will be the output of this nested lambda?
Function<Integer, Function<Integer, Integer>> add = x -> y -> x + y;
System.out.println(add.apply(3).apply(5));
Answer: A
Curried function: add.apply(3) returns a Function that adds 3. .apply(5) on that adds 5 to 3, returning 8.
Q.14Medium
Which of the following statements about @FunctionalInterface is true?
Answer: B
@FunctionalInterface is optional annotation that helps compiler verify interface has exactly one abstract method, catching errors early.
Q.15Easy
What is the output of this code?
Consumer<String> print = s -> System.out.println(s.toUpperCase());
print.accept("java");
Answer: B
Consumer accepts one argument and returns nothing. print.accept("java") calls lambda which prints s.toUpperCase() = "JAVA".
Q.16Medium
Can a lambda expression have multiple statements in its body?
Answer: B
Lambda can have multiple statements using braces: (x,y) -> { int z = x+y; return z; }. Single expression needs no braces or return.
Q.17Medium
Which functional interface is best suited for this scenario: method that takes no parameters and returns a random number?
Answer: A
Supplier<T> takes no parameters () and returns T. Perfect for generating/providing values like random numbers.
Q.18Hard
What will happen if you try to compile this code?
BiFunction<String, String, String> concat = (a, b) -> a + b;
concat.apply("Java", "8", "Features");
Answer: C
BiFunction takes exactly 2 parameters. Passing 3 arguments is compilation error. apply() only accepts 2 arguments.
Q.19Medium
In Java streams, what does the filter() method use?
Answer: A
filter() method in streams takes Predicate which tests condition and returns boolean to determine if element should be included.
Q.20Hard
What is the type inference mechanism in lambda expressions?
Answer: A
Compiler uses target type (functional interface) to infer parameter types. If context is clear, explicit types not needed: list.forEach(x -> ...).