Java Programming
Java OOP, collections, multithreading
476 Questions 10 Topics Take Test
Advertisement
Showing 51–60 of 476 questions
Q.51 Medium Lambda Expressions
What is the difference between Consumer and Function functional interfaces?
A Consumer returns a value, Function doesn't
B Function returns a value, Consumer doesn't
C Both are identical
D Consumer can have multiple parameters, Function cannot
Correct Answer:  B. Function returns a value, Consumer doesn't
EXPLANATION

Function<T, R> takes parameter T and returns R. Consumer<T> takes parameter T and returns void.

Take Test
Q.52 Medium Lambda Expressions
Which functional interface should be used for a lambda with no parameters and no return value?
A Supplier
B Consumer
C Runnable
D Function
Correct Answer:  C. Runnable
EXPLANATION

Runnable is the functional interface for operations with no parameters and no return value (void).

Take Test
Q.53 Medium Lambda Expressions
What will be the type of the lambda expression (x) -> x.toString()?
A Function
B Function
C Cannot be determined without context
D Consumer
Correct Answer:  C. Cannot be determined without context
EXPLANATION

Without context, the parameter type cannot be inferred. The functional interface type determines the parameter type.

Take Test
Q.54 Medium Lambda Expressions
Which of the following lambda expressions has incorrect syntax?
A (int x) -> x * 2
B x -> x * 2
C (x, y) -> { return x + y; }
D (int x, int y) -> x > y ? x : y
Correct Answer:  D. (int x, int y) -> x > y ? x : y
EXPLANATION

Ternary operator inside lambda requires braces and explicit return statement for type inference.

Take Test
Q.55 Medium Lambda Expressions
What will be the output of: List list = Arrays.asList(1, 2, 3); list.replaceAll(x -> x * 2); System.out.println(list);
A [1, 2, 3]
B [2, 4, 6]
C [] (empty list)
D [2, 2, 2]
Correct Answer:  B. [2, 4, 6]
EXPLANATION

replaceAll() with a lambda expression modifies the list in-place, multiplying each element by 2. Result is [2, 4, 6].

Take Test
Advertisement
Q.56 Medium Lambda Expressions
What happens when a lambda expression references a local variable from its enclosing scope?
A The variable must be explicitly final or effectively final
B The variable is automatically copied into the lambda's scope
C Any variable can be accessed without restrictions
D The variable must be static
Correct Answer:  A. The variable must be explicitly final or effectively final
EXPLANATION

Lambda expressions can only access local variables that are final or effectively final (not reassigned). This ensures thread-safety and consistency.

Take Test
Q.57 Medium Lambda Expressions
Which of the following correctly uses method reference with constructor?
A String::new
B new String::
C String->new
D new::String
Correct Answer:  A. String::new
EXPLANATION

Constructor method references use ClassName::new syntax. String::new refers to the String constructor and can be used wherever a Supplier<String> is expected.

Take Test
Q.58 Medium Lambda Expressions
In the lambda expression (a, b) -> a.compareTo(b), what can we infer about the parameters?
A Both a and b are integers
B Both a and b are Comparable objects
C a is a String and b is a number
D Types cannot be determined without context
Correct Answer:  D. Types cannot be determined without context
EXPLANATION

Without explicit type declarations or context, the compiler must infer types from usage. We can only determine they have a compareTo() method, likely Comparable types.

Take Test
Q.59 Medium Lambda Expressions
What will be the output of: IntStream.range(1, 4).map(x -> x * x).forEach(System.out::print);
A 149
B 123
C 111
D 000
Correct Answer:  A. 149
EXPLANATION

range(1, 4) generates 1, 2, 3. map(x -> x * x) produces 1, 4, 9. Printed without spaces gives '149'.

Take Test
Q.60 Medium Lambda Expressions
What issue can arise when using lambda expressions with checked exceptions?
A Checked exceptions cannot be thrown directly in lambda expressions
B They must be wrapped in try-catch or a custom functional interface must be created
C Checked exceptions automatically become unchecked in lambdas
D Lambda expressions cannot handle any exceptions
Correct Answer:  B. They must be wrapped in try-catch or a custom functional interface must be created
EXPLANATION

Since standard functional interfaces don't declare checked exceptions, you must either wrap in try-catch or create a custom functional interface that throws the checked exception.

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