Java SE 8 Programmer II — Question 27
Given the code fragment:
class CallerThread implements Callable<String> {
String str;
public CallerThread(String s) {this.str=s;}
public String call() throws Exception {
return str.concat("Call");
}
}
and
public static void main (String[] args) throws InterruptedException, ExecutionException
{
ExecutorService es = Executors.newFixedThreadPool(4); //line n1
Future f1 = es.submit (newCallerThread("Call"));
String str = f1.get().toString();
System.out.println(str);
}
Which statement is true?
Call Call and terminates.
Answer options
- A. The program prints Call Call and does not terminate.
- B. The program prints line n1.
- C. A compilation error occurs at ExecutionException is thrown at run time.
- D. An
Correct answer: B
Explanation
The correct answer is B because the output of the program is determined by the string concatenation in the call method, but the specific output of 'line n1' indicates that the program does not print the concatenated string before terminating. Options A and C are incorrect as they misinterpret the behavior of the code and the execution flow.