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.802Medium
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.803Medium
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.804Medium
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.805Medium
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).
Advertisement
Q.806Hard
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.807Medium
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.808Easy
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.809Medium
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.810Medium
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.811Hard
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.812Medium
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.813Hard
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 -> ...).
Q.814Easy
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.815Easy
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.816Easy
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.817Easy
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.818Medium
What will be the result of executing: List<Integer> nums = Arrays.asList(1, 2, 3, 4); nums.stream().map(x -> x * 2).collect(Collectors.toList());
Answer: A
The map() with lambda x -> x * 2 doubles each element: 1*2=2, 2*2=4, 3*2=6, 4*2=8, resulting in [2, 4, 6, 8].
Q.819Medium
Consider: Function<Integer, Integer> f = x -> x * x; What will f.apply(5) return?
Answer: A
Function<Integer, Integer> takes an Integer and returns an Integer. f.apply(5) computes 5 * 5 = 25.
Q.820Medium
What is the purpose of the Optional class when used with lambda expressions in Stream operations?
Answer: A
Optional is used to handle absence of values gracefully. Methods like ifPresent(lambda) and orElse() help prevent null pointer exceptions.