nerdexam
Oracle

1Z0-809 · Question #210

Given the code fragments: `` class R implements Runnable { public void run() { System.out.println("Run..."); } } class C implements Callable<String> { public String call() throws Exception { return…

The correct answer is C. Run. Option C is correct because both lines compile and run without error: ExecutorService.execute() accepts a Runnable, and ExecutorService.submit() accepts a Callable<T>, so both n1 and n2 are valid API calls. The executor runs R.run(), printing Run..., then C.call() returns…

Question

Given the code fragments:
class R implements Runnable {
 public void run() { System.out.println("Run..."); }
}

class C implements Callable<String> {
 public String call() throws Exception { return "Call..."; }
}
and
ExecutorService es = Executors.newSingleThreadExecutor();
es.execute(new R()); // line n1
Future<String> f1 = es.submit(new C()); // line n2
System.out.println(f1.get());
es.shutdown();
What is the result?

Options

  • AThe program prints Run... and throws an exception.
  • BA compilation error occurs at line n1.
  • CRun...
  • DA compilation error occurs at line n2.

How the community answered

(42 responses)
  • A
    12% (5)
  • B
    7% (3)
  • C
    79% (33)
  • D
    2% (1)

Explanation

Option C is correct because both lines compile and run without error: ExecutorService.execute() accepts a Runnable, and ExecutorService.submit() accepts a Callable<T>, so both n1 and n2 are valid API calls. The executor runs R.run(), printing Run..., then C.call() returns "Call...", which f1.get() retrieves and println prints - giving the full output Run... followed by Call....

Why the distractors are wrong:

  • B is wrong because execute(Runnable) is a perfectly valid method on ExecutorService - no compilation error.
  • D is wrong because submit(Callable<V>) is also a valid overload - ExecutorService has separate overloads for Runnable and Callable.
  • A is wrong because C.call() returns normally (no exception is thrown), so f1.get() completes without an ExecutionException.

Memory tip: Think of the two methods by what they give back - execute is fire-and-forget (returns void), while submit gives you a Future to retrieve a result later. If a question asks about execute with a Callable or submit with a Runnable returning a Future<?>, those compile too - just remember execute never returns a result regardless.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice