1z0-809 Exam QuestionsBrowse all questions from this exam

1z0-809 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?

Show Answer
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

8 comments
Sign in to comment
HuimOption: A
May 13, 2021

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

Abdullah_RahahleahOption: A
Dec 22, 2020

A is the correct answer-tested

jduarteOption: A
Feb 7, 2021

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

jduarteOption: B
Apr 28, 2021

Sorry. answer is B.

Svetleto13Option: B
May 14, 2021

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

WilsonKKerllOption: B
Mar 20, 2022

Answer is B. tested.

duydnOption: B
Sep 22, 2023

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

DarGrin
Jun 6, 2024

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