Home Subjects Java Programming Lambda Expressions

Java Programming
Lambda Expressions

Java OOP, collections, multithreading

32 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 32
Topics in Java Programming
Which of the following best describes a functional interface?
A An interface that can only have lambda expressions
B An interface with exactly one abstract method
C An interface with all methods as default
D An interface with multiple abstract methods that are related
Correct Answer:  B. An interface with exactly one abstract method
EXPLANATION

A functional interface must have exactly one abstract method, which can be implemented using lambda expressions.

Test
What is the result of this code?
UnaryOperator square = x -> x * x;
System.out.println(square.apply(5));
A 25
B 10
C 5
D Compilation error
Correct Answer:  A. 25
EXPLANATION

UnaryOperator applies the operation x * x where x = 5, resulting in 25.

Test
What is the output of:
BiFunction add = (a, b) -> a + b;
System.out.println(add.apply(5, 3));
A 8
B 53
C Compilation error
D Runtime exception
Correct Answer:  A. 8
EXPLANATION

BiFunction takes two Integer parameters and returns their sum. apply(5, 3) returns 5 + 3 = 8.

Test
What is the return type of the lambda expression: (a, b) -> a > b?
A int
B boolean
C void
D Cannot be determined
Correct Answer:  B. boolean
EXPLANATION

The expression uses comparison operator (>), which returns a boolean value.

Test
A lambda expression can only be used with which type of interface?
A Regular interface
B Functional interface with single abstract method
C Interface with multiple abstract methods
D Interface with default methods only
Correct Answer:  B. Functional interface with single abstract method
EXPLANATION

Lambda expressions can only be assigned to functional interfaces that have exactly one abstract method.

Test
Which of the following is a valid lambda expression in Java?
A () -> System.out.println("Hello")
B (int x, int y) -> { return x + y; }
C (String s) -> s.length()
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three are valid lambda expression syntaxes with different parameter types and return statements.

Test
Which stream operation would you use to transform elements from one type to another?
A filter()
B map()
C forEach()
D collect()
Correct Answer:  B. map()
EXPLANATION

map() is the intermediate operation used to transform elements from one type/value to another. It applies a function to each element.

Test
What is the purpose of the @FunctionalInterface annotation in Java?
A To indicate an interface can be used with lambda expressions
B To enforce that an interface has exactly one abstract method
C To improve performance of lambda expressions
D To allow multiple lambda expressions in one interface
Correct Answer:  B. To enforce that an interface has exactly one abstract method
EXPLANATION

@FunctionalInterface is a compile-time check that ensures an interface has exactly one abstract method, making it safe for lambda implementation.

Test
What is a method reference (::) in Java?
A A shorthand syntax for lambda expressions that call existing methods
B An operator for accessing private members
C A way to create anonymous classes
D A reference to a method's memory address
Correct Answer:  A. A shorthand syntax for lambda expressions that call existing methods
EXPLANATION

Method references are compact lambda expressions that directly reference existing methods. They use :: syntax and are equivalent to lambdas that call those methods.

Test
Which functional interface is used for operations that take two arguments and return a single value?
A Function
B BiFunction
C Consumer
D Supplier
Correct Answer:  B. BiFunction
EXPLANATION

BiFunction<T, U, R> represents a function that accepts two arguments of types T and U, and returns a result of type R.

Test
IGET
IGET AI
Online · Exam prep assistant
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