When using bounded type parameters with multiple interfaces, what is the correct syntax in Java?
Answer: A
Java uses ampersand (&) to combine multiple bounds. The class must appear first, followed by interfaces. Commas, pipes, and 'implements' keyword are not valid syntax for type parameter bounds.
Q.222Easy
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.223Easy
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.224Easy
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.225Easy
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.
Advertisement
Q.226Easy
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.
Q.227Easy
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.228Easy
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.229Easy
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.230Easy
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.231Easy
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.232Easy
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.233Easy
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.234Easy
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.235Easy
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.236Easy
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.237Easy
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.238Easy
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.239Easy
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.240Easy
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.