Java OCA OCP Practice Question 1973

Question

What is the output of the following code snippet?.

Callable c = new Callable() { 
   public Object run() {return 10;} 
}; /*from  w ww  . j  a va  2 s.c  o m*/
ExecutorService s = Executors.newScheduledThreadPool(1); 
for(int i=0; i<10; i++) { 
   Future f = s.submit(c); 
   f.get(); 
} 
s.shutdown(); 
System.out.print("Done!"); 
  • A. Done!
  • B. The code does not compile.
  • C. The code hangs indefinitely at runtime.
  • D. The code throws an exception at runtime.


B.

Note

The code does not compile because Callable must define a call() method, not a run() method, so Option B is the correct answer.

If the code was fixed to use the correct method name, then it would complete without issue, printing Done! at runtime, and Option A would be the correct answer.




PreviousNext

Related