Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 186

Given the code fragments:

class Caller implements Callable {

String str;

public Caller (String s) {this.str=s;}

public String call()throws Exception { return str.concat ("Caller");}

}

class Runner implements Runnable {

String str;

public Runner (String s) {this.str=s;}

public void run () { System.out.println (str.concat ("Runner"));}

}

and

public static void main (String[] args) throws InterruptedException, ExecutionException {

ExecutorService es = Executors.newFixedThreadPool(2);

Future f1 = es.submit (new Caller ("Call"));

Future f2 = es.submit (new Runner ("Run"));

String str1 = (String) f1.get();

String str2 = (String) f2.get(); //line n1

System.out.println(str1+ ":" + str2);

es.shutdown();

}

What is the result?

    Correct Answer: C

    The future f1 refers to a Callable task which returns a result whereas future f2 refers to a Runnable task which does not return a result. The call to f2.get() will cause a ClassCastException because f2.get() does not return a String, which leads to a compilation error at line n1 when trying to explicitly cast the return type to String.

Discussion
HuimOption: A

The program prints: RunRunner CallCaller:null Process finished with exit code 0

jduarteOption: A

answer is A but it ends because has es.shutdow();

Abdullah_RahahleahOption: A

A is the correct answer-tested

jduarteOption: B

Sorry. answer is B.

DarGrin

The program prints: RunRunner CallCaller:null and terminates. So there is no correct answer.

duydnOption: B

B is correct. Program terminates by es.shutdown();

WilsonKKerllOption: B

Answer is B. tested.

Svetleto13Option: B

B,tested.If we delete es.shutdown(); answer is A.