Java Programming
Java OOP, collections, multithreading
476 Questions 10 Topics Take Test
Advertisement
Showing 41–50 of 476 questions
Q.41 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
Q.42 Medium Lambda Expressions
Consider a lambda expression: (String s) -> s.length(). What is the correct functional interface for this?
A Function
B Supplier
C Consumer
D Predicate
Correct Answer:  A. Function
EXPLANATION

Function<String, Integer> takes a String input and returns an Integer (the length). This matches the lambda that takes a String parameter and returns s.length().

Take Test
Q.43 Medium Lambda Expressions
What is the return type of a lambda expression (x, y) -> x + y where x and y are integers?
A void
B Integer
C Depends on the functional interface it's assigned to
D int
Correct Answer:  C. Depends on the functional interface it's assigned to
EXPLANATION

The return type of a lambda expression is inferred from the functional interface it's assigned to. The same lambda can return Integer or int depending on the context.

Take Test
Q.44 Medium Lambda Expressions
Analyze: What is the output?
BiConsumer printer = (a, b) -> System.out.println(a + b);
printer.accept("Java", "8");
A Java8
B JavaCompilation error
C Java 8
D Compilation error
Correct Answer:  A. Java8
EXPLANATION

BiConsumer accepts two parameters and performs the action. String concatenation of "Java" + "8" produces "Java8".

Take Test
Q.45 Medium Lambda Expressions
Which lambda expression is equivalent to the method reference Integer::parseInt?
A (s) -> Integer.parseInt(s)
B () -> Integer.parseInt()
C (Integer i) -> i.parseInt()
D (s) -> s.parseInt()
Correct Answer:  A. (s) -> Integer.parseInt(s)
EXPLANATION

Method reference Integer::parseInt is equivalent to lambda (s) -> Integer.parseInt(s).

Take Test
Advertisement
Q.46 Medium Lambda Expressions
Consider: Comparator comp = (s1, s2) -> s2.compareTo(s1);
What does this lambda expression do?
A Sorts strings in ascending order
B Sorts strings in descending order
C Compares string lengths
D Checks string equality
Correct Answer:  B. Sorts strings in descending order
EXPLANATION

By reversing the compareTo order (s2 compared to s1), it sorts in descending order.

Take Test
Q.47 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.48 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
Q.49 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.50 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
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