Java Programming
Java OOP, collections, multithreading
958 Questions 10 Topics Take Test
Advertisement
Showing 91–100 of 958 questions
What is the result of this code?
UnaryOperator square = x -> x * x;
System.out.println(square.apply(5));
A 25
B 10
C 5
D Compilation error
Correct Answer:  A. 25
EXPLANATION

UnaryOperator applies the operation x * x where x = 5, resulting in 25.

Take Test
Q.92 Medium Lambda Expressions
Which of the following statements about variable scope in lambda expressions is TRUE?
A Lambda can access any local variable
B Lambda can only access effectively final local variables
C Lambda has its own scope separate from enclosing scope
D Lambda can modify local variables from enclosing scope
Correct Answer:  B. Lambda can only access effectively final local variables
EXPLANATION

Lambda expressions can only access local variables that are effectively final (not modified after initialization).

Take Test
Q.93 Medium Lambda Expressions
In the lambda expression (x) -> x < 10 && x > 0, what is the parameter type?
A Explicitly specified as int
B Must be inferred from context
C Cannot be a numeric type
D Automatically defaults to Integer
Correct Answer:  B. Must be inferred from context
EXPLANATION

Parameter type must be inferred from the functional interface it's assigned to, as it's not explicitly declared.

Take Test
What is the output of:
BiFunction add = (a, b) -> a + b;
System.out.println(add.apply(5, 3));
A 8
B 53
C Compilation error
D Runtime exception
Correct Answer:  A. 8
EXPLANATION

BiFunction takes two Integer parameters and returns their sum. apply(5, 3) returns 5 + 3 = 8.

Take Test
Q.95 Medium Lambda Expressions
Which of the following lambda expressions will compile successfully?
A (int x) -> { int y = x * 2; return y; }
B (int x) -> { int x = 5; return x; }
C (int x) -> { var y = x; return y; }
D A and C
Correct Answer:  D. A and C
EXPLANATION

Both A and C are valid. Option B fails because variable x is already declared as parameter. Option C uses var keyword (Java 10+).

Take Test
Q.96 Medium Lambda Expressions
Which of the following uses method reference correctly?
A list.forEach(System.out::println)
B list.forEach(System.out.println)
C list.forEach(x -> System.out.println(x))
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Both method reference (::) and lambda expression are valid ways to print list elements. Option B has incorrect syntax.

Take Test
Q.97 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.98 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.99 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.100 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
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