nerdexam
Oracle

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…

Concurrency

Question

Given the code fragment: var pool = Executors.newFixedThreadPool(5); Future<Integer> result = pool.submit(() -> 1); Which type of lambda expression is passed into submit()?

Options

  • Ajava.lang.Runnable
  • Bjava.util.function.Predicate
  • Cjava.util.function.Function
  • Djava.util.concurrent.Callable

How the community answered

(51 responses)
  • A
    6% (3)
  • B
    12% (6)
  • C
    2% (1)
  • D
    80% (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() is void - it cannot return a value, so () -> 1 would not compile as a Runnable, and submit(Runnable) returns Future<?>, not Future<Integer>.
  • B (Predicate): Predicate<T>.test(T t) requires one argument and returns boolean - 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

#Lambda expressions#Callable interface#Functional interfaces#ExecutorService

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice