Java SE 8 Programmer II — Question 177

Given the code fragments:
class ThreadRunner implements Runnable {
public void run () { System.out.print ("Runnable") ; }
}
class ThreadCaller implements Callable {
Public String call () throws Exception {return "Callable"; )
}
and
ExecutorService es = Executors.newCachedThreadPool ();
Runnable r1 = new ThreadRunner ();
Callable c1 = new ThreadCaller ();
// line n1
es.shutdown();
Which code fragment can be inserted at line n1 to start r1 and c1 threads?

Answer options

Correct answer: D

Explanation

Option D is correct because it properly submits both the Runnable r1 and the Callable c1 to the executor service using the submit method, which is suitable for both types. The other options either mix up the methods (like using execute for Callable) or cast incorrectly, which would lead to runtime errors.