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.6Easy
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.7Easy
In Java 8, a lambda expression can access local variables from its enclosing scope. What is the constraint on these variables?
Answer: A
Lambda expressions can only access local variables that are final or effectively final to ensure thread safety and immutability.
Q.8Easy
What is the return type of the map() method when used with lambda expressions in Java Streams?
Answer: A
The map() method in Stream API returns a new Stream with transformed elements of type R, maintaining the stream pipeline.
Q.9Easy
Consider the code: List<String> names = Arrays.asList("Raj", "Priya", "Amit"); names.forEach(n -> System.out.println(n)); What does this code do?
Answer: A
forEach with the lambda expression n -> System.out.println(n) iterates through each element and prints it with a newline.
Q.10Easy
Which functional interface is used for operations that take no arguments and return a value?
Answer: A
Supplier<T> is a functional interface with method get() that takes no parameters and returns a value of type T.
Q.11Easy
What is the primary advantage of using lambda expressions in Java 8+?
Answer: A
Lambda expressions reduce boilerplate code by allowing inline implementation of functional interfaces without creating anonymous inner classes, enabling a more functional programming style.
Q.12Easy
What will be the output of: List<String> list = Arrays.asList("a", "bb", "ccc"); list.forEach(s -> System.out.print(s.length() + " "));
Answer: A
The lambda expression prints the length of each string. Lengths are 1, 2, and 3 respectively, with spaces between them.
Q.13Easy
Which functional interface is used for operations that take two arguments and return a single value?
Answer: B
BiFunction<T, U, R> represents a function that accepts two arguments of types T and U, and returns a result of type R.
Q.14Easy
What is a method reference (::) in Java?
Answer: A
Method references are compact lambda expressions that directly reference existing methods. They use :: syntax and are equivalent to lambdas that call those methods.
Q.15Easy
What is the purpose of the @FunctionalInterface annotation in Java?
Answer: B
@FunctionalInterface is a compile-time check that ensures an interface has exactly one abstract method, making it safe for lambda implementation.
Q.16Easy
Which stream operation would you use to transform elements from one type to another?
Answer: B
map() is the intermediate operation used to transform elements from one type/value to another. It applies a function to each element.
Q.17Easy
Which of the following is a valid lambda expression in Java?
Answer: D
All three are valid lambda expression syntaxes with different parameter types and return statements.
Q.18Easy
A lambda expression can only be used with which type of interface?
Answer: B
Lambda expressions can only be assigned to functional interfaces that have exactly one abstract method.
Q.19Easy
What is the return type of the lambda expression: (a, b) -> a > b?
Answer: B
The expression uses comparison operator (>), which returns a boolean value.
Q.20Easy
What is the output of:
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
System.out.println(add.apply(5, 3));
Answer: A
BiFunction takes two Integer parameters and returns their sum. apply(5, 3) returns 5 + 3 = 8.