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 21–30 of 32
Topics in Java Programming
What will be the output of: List list = Arrays.asList("a", "bb", "ccc"); list.forEach(s -> System.out.print(s.length() + " "));
A 1 2 3
B a bb ccc
C 3 2 1
D 1 1 1
Correct Answer:  A. 1 2 3
EXPLANATION

The lambda expression prints the length of each string. Lengths are 1, 2, and 3 respectively, with spaces between them.

Test
What is the primary advantage of using lambda expressions in Java 8+?
A They reduce boilerplate code and enable functional programming style
B They increase execution speed significantly
C They eliminate the need for classes
D They are required for all stream operations
Correct Answer:  A. They reduce boilerplate code and enable functional programming style
EXPLANATION

Lambda expressions reduce boilerplate code by allowing inline implementation of functional interfaces without creating anonymous inner classes, enabling a more functional programming style.

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.

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.

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.

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.

Test
What is the output of this code?
Consumer print = s -> System.out.println(s.toUpperCase());
print.accept("java");
A java
B JAVA
C Compilation error
D java JAVA
Correct Answer:  B. JAVA
EXPLANATION

Consumer accepts one argument and returns nothing. print.accept("java") calls lambda which prints s.toUpperCase() = "JAVA".

Test
What will be the output of the following code?
UnaryOperator square = x -> x * x;
System.out.println(square.apply(5));
A 25
B 5
C Compilation error
D Runtime error
Correct Answer:  A. 25
EXPLANATION

UnaryOperator applies a function on its argument and returns result of same type. square.apply(5) returns 5*5 = 25.

Test
Which package contains the built-in functional interfaces in Java?
A java.util.function
B java.lang.function
C java.io.function
D java.util.interface
Correct Answer:  A. java.util.function
EXPLANATION

The java.util.function package contains functional interfaces like Predicate, Consumer, Function, Supplier, and BiFunction introduced in Java 8.

Test
What is a functional interface in Java?
A An interface with exactly one abstract method
B An interface that extends multiple interfaces
C An interface with default methods only
D An interface with no methods
Correct Answer:  A. An interface with exactly one abstract method
EXPLANATION

A functional interface has exactly one abstract method. It can have multiple default methods. The @FunctionalInterface annotation can be used to mark it.

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