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

Threads - Pointing out the correct statements Questions and Answers

Answer: C

Its worker threads are non-daemon by default and stay alive waiting for more work, so a program that never calls shutdown does not exit even after all its tasks are done. Forgetting the call is a common reason a JVM appears to hang at the end of a run.

Please log in or register to report this question.

Answer: D

The JVM creates a thread to run main, so single-threaded code is really one-thread code. That thread is not a daemon, which is why the program does not exit while it is still running, and it is the thread whose name Thread.currentThread().getName() reports as main.

Please log in or register to report this question.

Answer: A

Scheduling is left to the platform, so nothing about relative order is promised and a program that happens to print in a tidy order on one run may not on the next. Any required ordering has to be imposed explicitly, with join, a lock, a latch or a queue.

Please log in or register to report this question.

Answer: B

A finished thread is TERMINATED, and the javadoc states a thread may not be restarted once it has completed execution; a second start() throws IllegalThreadStateException. Reusing the object is impossible, which is one reason executors exist: they reuse threads while you submit fresh tasks.

Please log in or register to report this question.

Answer: C

The keyword marks a method or a block; there is no synchronized class and no synchronized field. Making a field's individual reads and writes atomic and visible is what volatile does, and neither keyword makes a compound update such as an increment safe on its own.

Please log in or register to report this question.

Answer: D

start() takes no arguments and run() declares none, so anything the task needs is supplied when the Runnable or Thread is constructed, or captured by a lambda. Captured locals must be effectively final, which is one reason shared mutable state tends to live in a field instead.

Please log in or register to report this question.

Answer: A

Atomicity of a single write is not the issue: each write lands whole, but without ordering or visibility guarantees one thread can overwrite the other's value or read a stale one, so an update disappears. The write being atomic is exactly why this looks safe and is not.

Please log in or register to report this question.

Answer: B

Every other thread needing the monitor waits for the whole region, so holding it across slow work such as I/O or a sleep serialises the program. The block still has to be long enough to cover the whole invariant, since splitting it lets another thread observe a half-finished update.

Please log in or register to report this question.

Answer: C

Interruption sets a flag and nothing more for a thread that is simply running; a loop that never consults it runs to completion regardless. That cooperative model is deliberate, because stopping a thread at an arbitrary point is what made stop() unsafe.

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