Java Programming — Lambda Expressions
Java OOP, collections, multithreading
32 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 32 questions in Lambda Expressions
What will be the output of the following code?
List numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
A 1 3 5
B 2 4
C 1 2 3 4 5
D Compilation error
Correct Answer:  B. 2 4
EXPLANATION

The filter operation with lambda expression (n -> n % 2 == 0) filters even numbers only. So 2 and 4 are printed, each on a new line due to println.

Take Test
Which functional interface is used to create a lambda expression that takes two integers and returns a boolean value?
A BiPredicate
B BiFunction
C BiConsumer
D Supplier
Correct Answer:  A. BiPredicate
EXPLANATION

BiPredicate<T, U> is a functional interface that takes two parameters and returns a boolean. This matches the requirement of taking two integers and returning a boolean value.

Take Test
Given: Function f = x -> x * 2; Integer result = f.apply(5); What is the value of result?
A 5
B 10
C 15
D 25
Correct Answer:  B. 10
EXPLANATION

The lambda expression x -> x * 2 multiplies the input by 2. When applied to 5, it returns 10.

Take Test
What is the correct syntax for a lambda expression with no parameters that returns a fixed value?
A -> "Hello"
B () -> "Hello"
C ( ) -> "Hello"
D { } -> "Hello"
Correct Answer:  B. () -> "Hello"
EXPLANATION

When a lambda has no parameters, empty parentheses () are required before the arrow. Options A and D are syntactically incorrect.

Take Test
In a lambda expression, what does the arrow (->) operator represent?
A Assignment operator
B Separation between parameters and body
C Comparison operator
D Logical AND operator
Correct Answer:  B. Separation between parameters and body
EXPLANATION

The arrow (->) in lambda expressions separates the parameter list on the left from the method body on the right. It's a syntax element specific to lambda expressions.

Take Test
Advertisement
What is the purpose of a Predicate functional interface in Java?
A To transform one type of object to another
B To perform an action without returning a value
C To test a condition and return a boolean value
D To supply a value without taking any input
Correct Answer:  C. To test a condition and return a boolean value
EXPLANATION

Predicate<T> is a functional interface that takes a single input of type T and returns a boolean. It's commonly used for filtering operations in streams.

Take Test
Which of the following lambda expressions is syntactically incorrect?
A () -> System.out.println("Hello")
B (int x) -> x * x
C (x, y,) -> x + y
D x -> x > 5
Correct Answer:  C. (x, y,) -> x + y
EXPLANATION

The syntax (x, y,) is incorrect because there's a trailing comma after y. The correct syntax would be (x, y) -> x + y.

Take Test
Which annotation is used to mark an interface as a functional interface in Java?
A @Function
B @FunctionalInterface
C @Lambda
D @Interface
Correct Answer:  B. @FunctionalInterface
EXPLANATION

@FunctionalInterface is the standard annotation introduced in Java 8 to explicitly mark an interface as a functional interface. It helps in compile-time checking.

Take Test
Which of the following is a valid functional interface that can be used with lambda expressions?
A An interface with exactly one abstract method
B An interface with multiple abstract methods
C An interface with no abstract methods
D An interface with static methods only
Correct Answer:  A. An interface with exactly one abstract method
EXPLANATION

A functional interface must have exactly one abstract method. This is the defining characteristic that allows it to be used with lambda expressions and method references.

Take Test
What will be the output of:
Supplier greeting = () -> "Hello";
System.out.println(greeting.get());
A Hello
B null
C Compilation error
D Empty string
Correct Answer:  A. Hello
EXPLANATION

Supplier.get() returns the value produced by the lambda expression, which is "Hello".

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips