Java Programming
Java OOP, collections, multithreading
958 Questions 10 Topics Take Test
Advertisement
Showing 61–70 of 958 questions
Q.61 Medium Lambda Expressions
A stream processes a list of strings and applies a lambda to convert them to uppercase. Which intermediate operation should be used?
A filter(s -> s.toUpperCase())
B map(s -> s.toUpperCase())
C peek(s -> s.toUpperCase())
D collect(s -> s.toUpperCase())
Correct Answer:  B. map(s -> s.toUpperCase())
EXPLANATION

The map() intermediate operation transforms each element using the provided lambda function. filter() returns boolean, peek() doesn't transform, and collect() is a terminal operation. map() is the correct choice for transformation.

Take Test
Q.62 Medium Lambda Expressions
Which of the following lambda expressions is INVALID in Java?
A () -> System.out.println("Hello")
B (int x) -> x * x
C (x, y) -> { return x + y; }
D (x, y) -> return x + y;
Correct Answer:  D. (x, y) -> return x + y;
EXPLANATION

In a lambda expression, if the body uses curly braces, 'return' keyword is required. But without curly braces, 'return' keyword cannot be used. Option D is invalid because it has 'return' without curly braces.

Take Test
What will be the output of the following code?
List numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
A 1 3 5
B 2 4
C 1 2 3 4 5
D Compilation error
Correct Answer:  B. 2 4
EXPLANATION

The filter operation with lambda expression (n -> n % 2 == 0) filters even numbers only. So 2 and 4 are printed, each on a new line due to println.

Take Test
Which functional interface is used to create a lambda expression that takes two integers and returns a boolean value?
A BiPredicate
B BiFunction
C BiConsumer
D Supplier
Correct Answer:  A. BiPredicate
EXPLANATION

BiPredicate<T, U> is a functional interface that takes two parameters and returns a boolean. This matches the requirement of taking two integers and returning a boolean value.

Take Test
Which of the following best demonstrates the use of Optional with lambda expressions for safe null handling?
A Optional.of(value).map(x -> x.toUpperCase()).orElse("default")
B if (value != null) { value.toUpperCase(); }
C try { value.toUpperCase(); } catch (NullPointerException e) { }
D value != null ? value.toUpperCase() : "default"
Correct Answer:  A. Optional.of(value).map(x -> x.toUpperCase()).orElse("default")
EXPLANATION

Optional with map() using a lambda expression is the functional programming approach. It's safe, readable, and chains operations effectively. Options B, C, and D are imperative approaches.

Take Test
Advertisement
In the context of exception handling with lambda expressions, what happens if a lambda body throws a checked exception?
A The lambda expression automatically wraps it in RuntimeException
B It causes a compilation error unless the functional interface declares the exception
C It's always caught and ignored
D The functional interface must declare throws clause
Correct Answer:  B. It causes a compilation error unless the functional interface declares the exception
EXPLANATION

If a lambda body throws a checked exception, it must be declared in the functional interface's method signature. Otherwise, compilation will fail.

Take Test
What is the difference between a lambda expression and an anonymous inner class in Java?
A Lambda expressions are only for functional interfaces, while anonymous classes can implement any interface
B Lambda expressions are cleaner and more concise for functional interfaces
C Lambda expressions create new scope, while anonymous classes don't
D Both A and B
Correct Answer:  D. Both A and B
EXPLANATION

Lambda expressions can only be used with functional interfaces (single abstract method), whereas anonymous classes can implement any interface. Lambda expressions are also more concise.

Take Test
Consider a scenario where you need to sort a list of strings by length. Which lambda expression is most appropriate for Comparator?
A (s1, s2) -> s1.compareTo(s2)
B (s1, s2) -> Integer.compare(s1.length(), s2.length())
C (s1, s2) -> s1.length() - s2.length()
D Both B and C
Correct Answer:  D. Both B and C
EXPLANATION

Both expressions correctly sort by length. Option B uses Integer.compare (safer for int overflow), while Option C uses direct subtraction. Both work correctly here.

Take Test
Q.69 Medium Lambda Expressions
Which stream operation uses a lambda expression to transform elements from one type to another?
A filter()
B map()
C forEach()
D reduce()
Correct Answer:  B. map()
EXPLANATION

map() is used to transform elements. It takes a Function lambda that converts each element from one type/value to another.

Take Test
Given: Function f = x -> x * 2; Integer result = f.apply(5); What is the value of result?
A 5
B 10
C 15
D 25
Correct Answer:  B. 10
EXPLANATION

The lambda expression x -> x * 2 multiplies the input by 2. When applied to 5, it returns 10.

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