Showing 71–80 of 476 questions
What will be the result of executing: List nums = Arrays.asList(1, 2, 3, 4); nums.stream().map(x -> x * 2).collect(Collectors.toList());
A
[2, 4, 6, 8]
B
[1, 2, 3, 4]
C
[4, 8, 12, 16]
D
[3, 4, 5, 6]
Correct Answer:
A. [2, 4, 6, 8]
EXPLANATION
The map() with lambda x -> x * 2 doubles each element: 1*2=2, 2*2=4, 3*2=6, 4*2=8, resulting in [2, 4, 6, 8].
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.
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.
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.
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.
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).
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.
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.
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).
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.