Java Programming — Lambda Expressions
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 100 questions in Lambda Expressions
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
Q.22 Medium Lambda Expressions
What will happen if you try to access a local variable from an enclosing scope that is not final or effectively final in a lambda expression?
A It will compile and run successfully
B Compilation error: variable must be final or effectively final
C Runtime exception will be thrown
D The variable value will be copied
Correct Answer:  B. Compilation error: variable must be final or effectively final
EXPLANATION

Lambda expressions can only access local variables that are final or effectively final. This is because lambda expressions are translated to methods that need access to stable variable values.

Take Test
Q.23 Medium Lambda Expressions
Consider a lambda expression: (String s) -> s.length(). What is the correct functional interface for this?
A Function
B Supplier
C Consumer
D Predicate
Correct Answer:  A. Function
EXPLANATION

Function<String, Integer> takes a String input and returns an Integer (the length). This matches the lambda that takes a String parameter and returns s.length().

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
Q.25 Medium Lambda Expressions
What is the return type of a lambda expression (x, y) -> x + y where x and y are integers?
A void
B Integer
C Depends on the functional interface it's assigned to
D int
Correct Answer:  C. Depends on the functional interface it's assigned to
EXPLANATION

The return type of a lambda expression is inferred from the functional interface it's assigned to. The same lambda can return Integer or int depending on the context.

Take Test
Advertisement
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
Q.27 Medium Lambda Expressions
Analyze: What is the output?
BiConsumer printer = (a, b) -> System.out.println(a + b);
printer.accept("Java", "8");
A Java8
B JavaCompilation error
C Java 8
D Compilation error
Correct Answer:  A. Java8
EXPLANATION

BiConsumer accepts two parameters and performs the action. String concatenation of "Java" + "8" produces "Java8".

Take Test
What is the correct way to chain lambda expressions using Function interface?
Function f1 = x -> x * 2;
Function f2 = x -> x + 5;
How to apply f1 first, then f2?
A f1.andThen(f2).apply(3)
B f1.compose(f2).apply(3)
C f2.apply(f1.apply(3))
D A and C
Correct Answer:  D. A and C
EXPLANATION

Both andThen and explicit composition order result in applying f1 first (3*2=6, then 6+5=11), equivalent to option C.

Take Test
Q.29 Medium Lambda Expressions
Which lambda expression is equivalent to the method reference Integer::parseInt?
A (s) -> Integer.parseInt(s)
B () -> Integer.parseInt()
C (Integer i) -> i.parseInt()
D (s) -> s.parseInt()
Correct Answer:  A. (s) -> Integer.parseInt(s)
EXPLANATION

Method reference Integer::parseInt is equivalent to lambda (s) -> Integer.parseInt(s).

Take Test
Q.30 Medium Lambda Expressions
Consider: Comparator comp = (s1, s2) -> s2.compareTo(s1);
What does this lambda expression do?
A Sorts strings in ascending order
B Sorts strings in descending order
C Compares string lengths
D Checks string equality
Correct Answer:  B. Sorts strings in descending order
EXPLANATION

By reversing the compareTo order (s2 compared to s1), it sorts in descending order.

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