Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 131–140 of 958
Topics in Java Programming
Q.131 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.132 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.133 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.134 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
What is the type inference mechanism in lambda expressions?
A Compiler infers parameter types from functional interface and context
B Parameters must always have explicit types
C Type inference only works with single parameter
D Lambda expressions don't support type inference
Correct Answer:  A. Compiler infers parameter types from functional interface and context
EXPLANATION

Compiler uses target type (functional interface) to infer parameter types. If context is clear, explicit types not needed: list.forEach(x -> ...).

Take Test
Q.140 Medium Lambda Expressions
In Java streams, what does the filter() method use?
A Predicate functional interface
B Function functional interface
C Consumer functional interface
D Supplier functional interface
Correct Answer:  A. Predicate functional interface
EXPLANATION

filter() method in streams takes Predicate which tests condition and returns boolean to determine if element should be included.

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