Java Programming
Java OOP, collections, multithreading
958 Questions 10 Topics Take Test
Advertisement
Showing 71–80 of 958 questions
What is the correct syntax for a lambda expression with no parameters that returns a fixed value?
A -> "Hello"
B () -> "Hello"
C ( ) -> "Hello"
D { } -> "Hello"
Correct Answer:  B. () -> "Hello"
EXPLANATION

When a lambda has no parameters, empty parentheses () are required before the arrow. Options A and D are syntactically incorrect.

Take Test
Q.72 Medium Lambda Expressions
Which of the following correctly uses a method reference as an alternative to a lambda expression?
A System.out::println instead of (x) -> System.out.println(x)
B String::length instead of (s) -> s.length()
C Integer::parseInt instead of (s) -> Integer.parseInt(s)
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three are valid method references that can replace their equivalent lambda expressions. Method references are a shorthand notation introduced in Java 8.

Take Test
Q.73 Medium Lambda Expressions
Consider: Comparator comp = (a, b) -> b - a; List list = Arrays.asList(3,1,2); Collections.sort(list, comp); What is the result?
A [1, 2, 3]
B [3, 2, 1]
C [2, 1, 3]
D [1, 3, 2]
Correct Answer:  B. [3, 2, 1]
EXPLANATION

The comparator (a, b) -> b - a returns negative values when b < a, causing elements to be sorted in descending order. Result is [3, 2, 1].

Take Test
In a lambda expression, what does the arrow (->) operator represent?
A Assignment operator
B Separation between parameters and body
C Comparison operator
D Logical AND operator
Correct Answer:  B. Separation between parameters and body
EXPLANATION

The arrow (->) in lambda expressions separates the parameter list on the left from the method body on the right. It's a syntax element specific to lambda expressions.

Take Test
Q.75 Medium Lambda Expressions
Which lambda expression correctly implements a custom functional interface: public interface Math { int calculate(int a, int b); }
A (a, b) -> a + b
B () -> 0
C (int a, int b) -> a - b
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Both expressions are valid. Option A uses type inference while Option C explicitly specifies types. Both can implement the calculate method.

Take Test
Advertisement
Q.76 Medium Lambda Expressions
What is the output of: List nums = Arrays.asList(1,2,3); nums.stream().filter(x -> x > 1).forEach(System.out::println);
A 1 2 3
B 2 3
C 1
D Nothing is printed
Correct Answer:  B. 2 3
EXPLANATION

filter(x -> x > 1) keeps only elements greater than 1, which are 2 and 3. These are then printed on separate lines using forEach.

Take Test
Q.77 Medium Lambda Expressions
Consider the code: List names = Arrays.asList("Alice", "Bob"); names.forEach(name -> System.out.println(name)); What type of functional interface is used in forEach?
A Function
B Consumer
C Supplier
D Predicate
Correct Answer:  B. Consumer
EXPLANATION

forEach accepts a Consumer functional interface. Consumer<T> takes an input and performs an action without returning anything, which matches the System.out.println action.

Take Test
What is the purpose of a Predicate functional interface in Java?
A To transform one type of object to another
B To perform an action without returning a value
C To test a condition and return a boolean value
D To supply a value without taking any input
Correct Answer:  C. To test a condition and return a boolean value
EXPLANATION

Predicate<T> is a functional interface that takes a single input of type T and returns a boolean. It's commonly used for filtering operations in streams.

Take Test
Which of the following lambda expressions is syntactically incorrect?
A () -> System.out.println("Hello")
B (int x) -> x * x
C (x, y,) -> x + y
D x -> x > 5
Correct Answer:  C. (x, y,) -> x + y
EXPLANATION

The syntax (x, y,) is incorrect because there's a trailing comma after y. The correct syntax would be (x, y) -> x + y.

Take Test
Q.80 Medium Lambda Expressions
What will happen if you try to access a local variable from an enclosing scope that is not final or effectively final in a lambda expression?
A It will compile and run successfully
B Compilation error: variable must be final or effectively final
C Runtime exception will be thrown
D The variable value will be copied
Correct Answer:  B. Compilation error: variable must be final or effectively final
EXPLANATION

Lambda expressions can only access local variables that are final or effectively final. This is because lambda expressions are translated to methods that need access to stable variable values.

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