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

Threads - General Questions Questions and Answers

Answer: C

Correct answer: CountDownLatch latch = new CountDownLatch(3);

The constructor takes the initial count, and the latch opens when countDown() has been called that many times. There is no factory method and no timeout in the constructor - a timeout belongs to the await call, where waiting actually happens.

Please log in or register to report this question.

Answer: A

The lock is taken before the try so that the finally only runs when it was actually acquired; acquiring inside the try would attempt an unlock even if lock itself threw. ReentrantLock uses lock and unlock, and the synchronized statement does not drive it at all.

Please log in or register to report this question.

54.

Which of these methods does BlockingQueue declare for the blocking forms of insert and remove?

Answer: B

put and take are the forms that wait for space or for an element. add and remove throw when they cannot proceed, and offer and poll return a value or null instead of waiting. All three pairs exist, which is why the names matter.

Please log in or register to report this question.

55.

Which of these types is NOT in java.util.concurrent?

Answer: C

Correct answer: ReentrantLock

ReentrantLock lives in java.util.concurrent.locks, along with ReadWriteLock, Condition and StampedLock. The others are directly in java.util.concurrent, which is why importing that package alone leaves the locks unavailable.

Please log in or register to report this question.

57.

Which of these correctly acquires a permit from a Semaphore?

Answer: A

Correct answer: sem.acquire();

Semaphore uses acquire() and release(), and acquire() blocks until a permit is available or the thread is interrupted. tryAcquire() is the non-blocking form. The lock, take and enter vocabularies belong to other types entirely.

Please log in or register to report this question.

Answer: B

newSingleThreadExecutor takes no arguments and gives a one-thread service. newFixedThreadPool requires a count, newCachedThreadPool takes none and grows on demand, and ThreadPoolExecutor's constructors need several arguments rather than one.

Please log in or register to report this question.

Answer: C

Runnable is an interface with one method that declares no checked exception, which is exactly why run cannot throw one and why Callable was added for tasks that need to. It extends nothing, and Thread implements it rather than the other way round.

Please log in or register to report this question.

60.

Given the code below, which statement about the declaration is correct?

class Worker extends Thread { }

Thread outer = new Thread(new Worker());

Answer: D

Thread implements Runnable, so a Thread object satisfies the Runnable parameter and the outer thread will call the inner one's run on its own thread of execution. It compiles regardless of whether Worker overrides anything, since Thread supplies an inherited run.

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