Java Programming — Lambda Expressions
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 71–80 of 100 questions in Lambda Expressions
Q.71 Medium Lambda Expressions
What is method reference (::) in Java and how does it relate to lambda expressions?
A Method reference is a shorthand for lambda expressions that call a single method
B Method reference is used for declaring new methods
C Method reference is faster than lambda but less readable
D Method reference can only be used with static methods
Correct Answer:  A. Method reference is a shorthand for lambda expressions that call a single method
EXPLANATION

Method reference (::) is syntactic sugar for lambda expressions. System.out::println is equivalent to x -> System.out.println(x).

Take Test
Q.72 Medium Lambda Expressions
Consider: Stream.of(1, 2, 3, 4, 5).filter(x -> x > 2).map(x -> x * x). What will the terminal operation need to be?
A collect(Collectors.toList())
B forEach(System.out::println)
C Either a or b
D reduce(0, Integer::sum)
Correct Answer:  C. Either a or b
EXPLANATION

Both collect() and forEach() are valid terminal operations. collect() returns a list, forEach() performs an action. reduce() is also valid but would sum the squares.

Take Test
Q.73 Medium Lambda Expressions
Which of the following lambda expressions is INCORRECT?
A () -> 42
B x -> x + 1
C (int x, int y) -> x + y
D (x, y) -> (x > y) ? x : y
Correct Answer:  C. (int x, int y) -> x + y
EXPLANATION

In option C, you cannot specify types in lambda parameters when using implicit typing. It should be either (x, y) or explicitly define all parameters.

Take Test
Q.74 Medium Lambda Expressions
What is the purpose of the Optional class when used with lambda expressions in Stream operations?
A To handle null values and avoid NullPointerException
B To increase stream processing speed
C To convert streams to lists automatically
D To sort elements in reverse order
Correct Answer:  A. To handle null values and avoid NullPointerException
EXPLANATION

Optional is used to handle absence of values gracefully. Methods like ifPresent(lambda) and orElse() help prevent null pointer exceptions.

Take Test
Q.75 Medium Lambda Expressions
Consider: Function f = x -> x * x; What will f.apply(5) return?
A 25
B 10
C 5
D 30
Correct Answer:  A. 25
EXPLANATION

Function<Integer, Integer> takes an Integer and returns an Integer. f.apply(5) computes 5 * 5 = 25.

Take Test
Q.76 Medium Lambda Expressions
What will be the result of executing: List nums = Arrays.asList(1, 2, 3, 4); nums.stream().map(x -> x * 2).collect(Collectors.toList());
A [2, 4, 6, 8]
B [1, 2, 3, 4]
C [4, 8, 12, 16]
D [3, 4, 5, 6]
Correct Answer:  A. [2, 4, 6, 8]
EXPLANATION

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].

Take Test
Which functional interface is used for operations that take no arguments and return a value?
A Supplier
B Function
C Predicate
D BiConsumer
Correct Answer:  A. Supplier
EXPLANATION

Supplier<T> is a functional interface with method get() that takes no parameters and returns a value of type T.

Take Test
Consider the code: List names = Arrays.asList("Raj", "Priya", "Amit"); names.forEach(n -> System.out.println(n)); What does this code do?
A Prints each name with a newline
B Creates a new list with uppercase names
C Filters names starting with 'A'
D Sorts the names alphabetically
Correct Answer:  A. Prints each name with a newline
EXPLANATION

forEach with the lambda expression n -> System.out.println(n) iterates through each element and prints it with a newline.

Take Test
What is the return type of the map() method when used with lambda expressions in Java Streams?
A Stream
B List
C Set
D Map
Correct Answer:  A. Stream
EXPLANATION

The map() method in Stream API returns a new Stream with transformed elements of type R, maintaining the stream pipeline.

Take Test
In Java 8, a lambda expression can access local variables from its enclosing scope. What is the constraint on these variables?
A They must be declared as final or effectively final
B They must be static variables
C They must be instance variables of the class
D They can be modified freely within the lambda
Correct Answer:  A. They must be declared as final or effectively final
EXPLANATION

Lambda expressions can only access local variables that are final or effectively final to ensure thread safety and immutability.

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