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 41–50 of 51
Topics in Java Programming
Q.41 Medium Lambda Expressions
In Java streams, what does the filter() method use?
A Predicate functional interface
B Function functional interface
C Consumer functional interface
D Supplier functional interface
Correct Answer:  A. Predicate functional interface
EXPLANATION

filter() method in streams takes Predicate which tests condition and returns boolean to determine if element should be included.

Test
Q.42 Medium Lambda Expressions
Which functional interface is best suited for this scenario: method that takes no parameters and returns a random number?
A Supplier
B Consumer
C Predicate
D Function
Correct Answer:  A. Supplier
EXPLANATION

Supplier<T> takes no parameters () and returns T. Perfect for generating/providing values like random numbers.

Test
Q.43 Medium Lambda Expressions
Can a lambda expression have multiple statements in its body?
A No, lambda can only have single expression
B Yes, using curly braces and explicit return statement
C Yes, but only if using arrow syntax
D No, compile will throw error
Correct Answer:  B. Yes, using curly braces and explicit return statement
EXPLANATION

Lambda can have multiple statements using braces: (x,y) -> { int z = x+y; return z; }. Single expression needs no braces or return.

Test
Q.44 Medium Lambda Expressions
Which of the following statements about @FunctionalInterface is true?
A It is mandatory for all functional interfaces
B It's optional but helps compiler verify exactly one abstract method
C It prevents inheritance of the interface
D It makes the interface static
Correct Answer:  B. It's optional but helps compiler verify exactly one abstract method
EXPLANATION

@FunctionalInterface is optional annotation that helps compiler verify interface has exactly one abstract method, catching errors early.

Test
Q.45 Medium Lambda Expressions
What is the difference between a lambda expression and an anonymous inner class?
A Lambda is more concise, doesn't create separate class file
B Anonymous inner class is faster
C Lambda can only be used with functional interfaces
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Lambdas provide concise syntax, are compiled directly without separate class files, and work only with functional interfaces (single abstract method).

Test
Q.46 Medium Lambda Expressions
Which of the following correctly represents a BiFunction?
A (int a, int b) -> a + b
B () -> 5
C x -> x * 2
D () -> System.out.println("Hi")
Correct Answer:  A. (int a, int b) -> a + b
EXPLANATION

BiFunction takes 2 parameters and returns a result. (int a, int b) -> a + b fits this. Others are Function, Supplier, Consumer respectively.

Test
Q.47 Medium Lambda Expressions
Can lambda expressions access local variables from their enclosing scope?
A Yes, but only if they are final or effectively final
B Yes, any local variable can be accessed
C No, lambda expressions cannot access local variables
D Only static variables can be accessed
Correct Answer:  A. Yes, but only if they are final or effectively final
EXPLANATION

Lambda expressions can access local variables only if they are final or effectively final (not modified after initialization) due to closure requirements.

Test
Q.48 Medium Lambda Expressions
What will this code print?
List list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach(x -> System.out.print(x + " "));
A 1 2 3 4 5
B 1 2 3 4 5
C Compilation error
D 15
Correct Answer:  B. 1 2 3 4 5
EXPLANATION

forEach with Consumer lambda prints each element followed by space, resulting in '1 2 3 4 5 ' (with trailing space).

Test
Q.49 Medium Lambda Expressions
Which functional interface should be used for a lambda that filters elements?
A Predicate
B Function
C Consumer
D Supplier
Correct Answer:  A. Predicate
EXPLANATION

Predicate<T> tests a condition and returns boolean. It's ideal for filtering. Function would return any type, Consumer doesn't return anything.

Test
Q.50 Medium Lambda Expressions
What is the type of the following lambda expression: x -> x.length()?
A Function
B Consumer
C Supplier
D Predicate
Correct Answer:  A. Function
EXPLANATION

Function<T,R> takes one argument of type T and returns R. Here String -> Integer (length), so Function<String, Integer>.

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