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

Threads - Pointing out the correct statements Questions and Answers

Answer: A

The javadoc states the method must be invoked before the thread is started, and that it throws IllegalThreadStateException if the thread is alive. Daemon status is therefore fixed for the whole life of a running thread and cannot be switched on later.

Please log in or register to report this question.

Answer: B

Daemon status is inherited: a new Thread is a daemon exactly when the creating thread is one, just as its initial priority is copied from the creator. Because the main thread is not a daemon, threads created from main default to non-daemon, which is why the explicit-call answer looks right.

Please log in or register to report this question.

Answer: C

Execution continues until all non-daemon threads have died, or Runtime.exit is called; the JVM exits when the only threads running are daemons. So main returning does not end the program if a non-daemon thread is still alive, and a daemon can be killed mid-work at shutdown.

Please log in or register to report this question.

Answer: D

Because the JVM exits as soon as only daemons remain, a daemon can stop anywhere, including part-way through updating shared state or writing a file. No grace period is promised and holding a lock does not help. Work that must complete belongs on a non-daemon thread.

Please log in or register to report this question.

Answer: A

join() waits for the thread it is invoked on to die, blocking the caller until then. The direction is the common confusion: t.join() suspends the current thread, not t. It is implemented as a loop of wait calls guarded by isAlive, with notifyAll issued as the thread terminates.

Please log in or register to report this question.

Answer: B

The javadoc defines join() as behaving exactly like join(0), and states that a timeout of 0 means to wait forever. Treating zero as no wait is the natural reading and the wrong one, which is what makes it worth asking.

Please log in or register to report this question.

Answer: C

Those six are the whole enum. There is deliberately no RUNNING state: a thread that is executing and one merely eligible to execute are both RUNNABLE, because the JVM does not distinguish them. A sleeping thread reports TIMED_WAITING, and getState is documented for monitoring rather than for control.

Please log in or register to report this question.

Answer: D

BLOCKED means waiting for a monitor lock in order to enter or re-enter a synchronized region. WAITING means the thread called something like wait() or join() with no timeout and is waiting for another thread to act. The distinction is what makes a thread dump readable.

Please log in or register to report this question.

Answer: A

A thread dies either by returning from run() or by throwing an exception that propagates beyond it, and that death is local to the thread. It cannot be caught by whoever called start(), because that call returned long before. The uncaught exception handler reports it, so it is not silent.

Please log in or register to report this question.

Answer: B

Handling is controlled first by the thread, via setUncaughtExceptionHandler, then by its ThreadGroup's uncaughtException method, and finally by the default handler. The chain is why a per-thread handler overrides a default one that is already installed.

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