1Z0-819 · Question #97
Given the code fragment: var pool = Executors.newFixedThreadPool(5); Future<Integer> result = pool.submit(() -> 1); Which type of lambda expression is passed into submit()?
The correct answer is D. java.util.concurrent.Callable. Callable<Integer> is correct because the lambda () -> 1 takes no arguments and returns an Integer - which matches Callable's single abstract method V call() throws Exception. The compiler resolves the submit() overload to submit(Callable<T>) (not submit(Runnable)) precisely…
Question
Options
- Ajava.lang.Runnable
- Bjava.util.function.Predicate
- Cjava.util.function.Function
- Djava.util.concurrent.Callable
How the community answered
(51 responses)- A6% (3)
- B12% (6)
- C2% (1)
- D80% (41)
Explanation
Callable<Integer> is correct because the lambda () -> 1 takes no arguments and returns an Integer - which matches Callable's single abstract method V call() throws Exception. The compiler resolves the submit() overload to submit(Callable<T>) (not submit(Runnable)) precisely because a return type is present, yielding a Future<Integer>.
Why the distractors fail:
- A (Runnable):
Runnable.run()isvoid- it cannot return a value, so() -> 1would not compile as aRunnable, andsubmit(Runnable)returnsFuture<?>, notFuture<Integer>. - B (Predicate):
Predicate<T>.test(T t)requires one argument and returnsboolean- the lambda has no parameters, so it can't match. - C (Function):
Function<T,R>.apply(T t)also requires one argument - again, the zero-argument lambda is incompatible.
Memory tip: Think "Callable = Capable of returning a value." If your lambda returns something meaningful and you want a typed Future<T> back from submit(), it's always Callable. Runnable is the fire-and-forget sibling that returns nothing.
Topics
Community Discussion
No community discussion yet for this question.