Home Subjects Java Programming Lambda Expressions

Java Programming
Lambda Expressions

Java OOP, collections, multithreading

51 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 51
Topics in Java Programming
Q.11 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().

Test
Q.12 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.

Test
Q.13 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".

Test
Q.14 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).

Test
Q.15 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.

Test
Q.16 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).

Test
Q.17 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.

Test
Q.18 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+).

Test
Q.19 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.

Test
Q.20 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.

Test
IGET
IGET AI
Online · Exam prep assistant
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