Machine-translated by Google. Questions are authored in English.
Top

Threads - General Questions Questions and Answers

Answer: A

Correct answer: Exchanger<String> ex = new Exchanger<String>();

An Exchanger pairs exactly two threads by construction, so the constructor takes no count. Each thread calls exchange() with the object it is offering and receives the other’s in return, blocking until its partner arrives.

Please log in or register to report this question.

82.

Which of these correctly stops accepting new tasks while letting submitted ones finish?

Answer: B

Correct answer: es.shutdown();

shutdown() refuses new submissions and lets the queued and running tasks finish, which is the orderly stop. shutdownNow() also interrupts the running tasks and returns those never started, which is the abrupt one. The two are commonly used together, with a timed awaitTermination between them.

Please log in or register to report this question.

83.

Which of these is the correct type for the value a Future<?> from submit(Runnable) yields?

Answer: C

The one-argument submit for a Runnable gives a Future whose get returns null once the task is done, so the Future is useful for waiting and for surfacing an exception rather than for a result. A second argument can supply a fixed value to return instead.

Please log in or register to report this question.

Answer: D

Correct answer: ScheduledExecutorService s = Executors.newScheduledThreadPool(1); s.schedule(task, 5, TimeUnit.SECONDS);

Scheduling requires a ScheduledExecutorService, which a fixed thread pool is not, and the delay must carry a TimeUnit so that the number is unambiguous. Executors is a factory and has no schedule method of its own.

Please log in or register to report this question.

85.

Which interface does ExecutorService extend?

Answer: A

Executor is the minimal interface, declaring only execute(Runnable), and ExecutorService adds submission, shutdown and termination on top of it. Runnable and Callable describe tasks, and Future describes a pending result.

Please log in or register to report this question.

Answer: B

Executor declares exactly one method, execute, which takes a Runnable and returns nothing. submit belongs to the larger ExecutorService, because returning a Future implies the lifecycle management that Executor deliberately leaves out.

Please log in or register to report this question.

Answer: C

The enum constant itself carries a sleep method, so the unit reads before the duration and no conversion is written by hand. Thread.sleep has no TimeUnit overload, which is why the enum form is often preferred for readability.

Please log in or register to report this question.

88.

Which of these declares a lock acquisition that can be interrupted while waiting?

Answer: D

lockInterruptibly declares InterruptedException and abandons the attempt if the thread is interrupted while waiting. Plain lock ignores interruption, tryLock returns immediately, and a synchronized block cannot be interrupted at all.

Please log in or register to report this question.

89.

Which of these correctly declares a counter optimised for frequent updates from many threads?

Answer: A

Correct answer: LongAdder hits = new LongAdder();

LongAdder starts at zero and takes no constructor argument. It outperforms AtomicLong under heavy contention by spreading the count over several cells and summing them only when the value is read - which is exactly the trade that suits a frequently incremented counter read rarely.

Please log in or register to report this question.

90.

Which class does a fork-join task that returns a value extend?

Answer: B

Correct answer: RecursiveTask

RecursiveTask is the value-returning fork-join task and takes a type parameter for the result; RecursiveAction is the equivalent that returns nothing. Both extend ForkJoinTask, and ForkJoinPool is what executes them rather than what they extend.

Please log in or register to report this question.

Loading…
Tags: Threads - General Questions Questions and Answers || Threads - General Questions MCQ Questions and Answers || Threads - General Questions GK Questions and Answers || Threads - General Questions GK MCQ Questions || Threads - General Questions Multiple Choice Questions and Answers || Threads - General Questions GK || GK on Threads - General Questions || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming