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

Threads - Pointing out the correct statements Questions and Answers

Answer: A

The javadoc says the method is designed for monitoring of the system state, not for synchronization control, because the answer can be stale the instant it is returned. Branching on it produces a race. There is also no RUNNING value; executing and merely eligible are both RUNNABLE.

Please log in or register to report this question.

Answer: B

Every object carries a monitor, which is why private final Object lock = new Object(); is a complete lock. Making it private and final is the point: nothing outside can acquire it, and the reference cannot be reassigned to leave threads locking different objects.

Please log in or register to report this question.

Answer: C

A monitor belongs to an object, not to the variable that refers to it. If the field is reassigned between two threads entering their synchronized blocks, each locks a different object and neither excludes the other, so the mutual exclusion silently disappears. Declaring the lock final prevents it.

Please log in or register to report this question.

Answer: D

A semaphore counts permits rather than tracking an owner, so release() may be called by a thread that never acquired one. That makes it useful for signalling between threads, and also means it offers no reentrancy: a thread that acquires its single permit twice blocks against itself.

Please log in or register to report this question.

Answer: A

Registering a listener or starting a thread from inside a constructor publishes the reference before the constructor has finished, so another thread can observe fields at their default values. The final field guarantee applies to a correctly constructed object, and one that escaped early does not qualify. Publish after construction instead.

Please log in or register to report this question.

Answer: B

The operation reads, compares and writes as one indivisible step and returns whether it succeeded, which is what lets a retry loop update a value without a lock. Because it never blocks, a losing thread simply reads the new value and tries again rather than waiting.

Please log in or register to report this question.

Answer: C

Throwing InterruptedException clears the status, so a method that cannot handle the interruption itself should call Thread.currentThread().interrupt() before returning. That preserves the request for code further up the stack, which would otherwise never learn the thread had been interrupted. Swallowing the exception silently is the mistake it avoids.

Please log in or register to report this question.

Answer: D

They lock different monitors, the instance and the Class object, so neither waits for the other and both may run at the same time. State touched by both therefore needs one agreed lock; relying on the keyword alone leaves it unprotected.

Please log in or register to report this question.

Answer: A

BLOCKED means waiting for a monitor lock, which is exactly the state of a thread stopped at the entrance to its first synchronized block. Locks it already holds are irrelevant. NEW is reserved for a thread that has not been started at all.

Please log in or register to report this question.

Answer: B

A monitor has at most one owner at a time; that is the whole guarantee. Reentrancy lets the single owning thread enter again, incrementing a count, but never admits a second thread. The lock is released only when the owner leaves the outermost region.

Please log in or register to report this question.

Loading…

Topics

    No topic!
Tags: Threads - Pointing out the correct statements Questions and Answers || Threads - Pointing out the correct statements MCQ Questions and Answers || Threads - Pointing out the correct statements GK Questions and Answers || Threads - Pointing out the correct statements GK MCQ Questions || Threads - Pointing out the correct statements Multiple Choice Questions and Answers || Threads - Pointing out the correct statements GK || GK on Threads - Pointing out the correct statements || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming