Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 114

Given the code fragment:

Which statement at line 1 will print Ping Pong?

    Correct Answer: C

    In this code, the task that needs to return a result must be submitted using a method that returns a Future object, which represents the pending result of the task. The 'execute' method of ExecutorService is used for Runnable tasks and does not return a Future. Therefore, the correct way to specify the task and get the resulting 'Ping Pong' output involves using the 'submit' method. The 'submit' method is specifically designed for tasks that return a result and is used with a Callable. Thus, using the 'submit' method with a lambda expression that returns 'Pong' will print 'Ping Pong' when combined with the 'Ping' printed by the initial 'execute' command.

Discussion
ASPushkinOption: C

answer : C theoretically it could return : Pong Ping B. not compiled method execute is void D. it will not compile the invokeAny method parameter has to be a type of Collection <T> T invokeAny(Collection<? extends Callable<T>> tasks)

d7bb0b2Option: C

only C: D:cannot because invokeany return a Result of callables completely ok. Future<String> future = executor.invokeAny(List.of(new Callable<String>(){ public String call() throws Exception { return "PONG"; }; }));

d7bb0b2

A: call method of callabe return a type String not a future and callabe cannot be asigned to future directry B: compile error BECAUSE Future<String> future = service.submit(()->"Pong"); is not a runnable C: Future<String> future = service.submit(()->"Pong"); => ok D: invoke any acept Collection of callable not a single callable

TheOneAboveAllOption: C

Tested C

OmnisumemOption: C

Tested: C. To test it is enough to code it with "public static void main (String[] args) throws Exception {...".

[Removed]Option: C

Let's just assume it's surrounded by a try-catch catching both InterruptedException & ExecutionException. Then C is correct.

JticOption: C

Future<String> future = es.submit(() -> "Pong");