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

Threads - Pointing out the correct statements Questions and Answers

Answer: A

Monitors count entries by owner, so a synchronized method calling another synchronized method on the same object simply increments the count and releases the lock when the outermost region exits. Without reentrancy any such call, including a superclass call, would deadlock immediately.

Please log in or register to report this question.

Answer: B

A synchronized instance method locks its own receiver, so two objects mean two independent monitors and no contention between them. Synchronization protects an object's state, not the class's code, which is why shared static state needs a lock on something shared.

Please log in or register to report this question.

Answer: C

Every object has a monitor, so synchronized (lock) works with any reference. A private final lock object is often preferred to locking this, because outside code cannot accidentally acquire it and interfere with the class's own locking.

Please log in or register to report this question.

Answer: D

Starting a thread establishes a happens-before edge: actions in the starting thread before start() are ordered before everything the new thread does. So a field set during construction and then handed over by start() is safely visible without extra synchronization. The edge is one-way.

Please log in or register to report this question.

Answer: A

Termination of a thread happens-before another thread detecting that termination through join, so results computed by the worker can be read from a plain field once join has returned. That pairing with start() is what makes the simple fork-and-join pattern correct without locks.

Please log in or register to report this question.

Answer: B

StringBuffer is the older, synchronized class and StringBuilder is the unsynchronized replacement, which is why StringBuilder is faster and the right default for a local variable. Only genuinely shared mutable text needs StringBuffer, and even then a lock around a larger operation is usually clearer.

Please log in or register to report this question.

Answer: C

Vector and Hashtable are the legacy synchronized collections; ArrayList and HashMap deliberately are not, leaving the caller to decide. Note that per-method synchronization does not make a sequence of calls atomic, so even a Vector needs external locking for check-then-act logic.

Please log in or register to report this question.

Answer: D

Synchronizing each method makes individual operations atomic, not the loop built from them. Another thread can modify the collection between two calls, giving ConcurrentModificationException or a missed element, so the whole traversal must hold the lock.

Please log in or register to report this question.

Answer: A

Runnable.run() returns void and declares no checked exception, so a task that produces a result or fails has nowhere to put either. Callable.call() returns a value and is declared to throw Exception, which is why an ExecutorService hands back a Future that can deliver a result or rethrow the failure.

Please log in or register to report this question.

Answer: B

A ThreadLocal holds one value per thread, so each thread reads and writes its own copy and no coordination is needed. It solves confinement rather than sharing, and it does leak if a value is left behind on a pooled thread that is reused for other work.

Please log in or register to report this question.

Loading…

Other Subjects

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