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

Threads - Pointing out the correct statements Questions and Answers

Answer: C

A thread is alive once it has been started and has not yet died, so a blocked, waiting or sleeping thread is still alive. A constructed but unstarted thread is NEW and not alive, and so is a finished one, which is TERMINATED and can never be restarted.

Please log in or register to report this question.

Answer: D

It is a static method that reports whoever is running at that moment, so the same line of code returns different threads depending on who reaches it. That makes it the way a shared method finds out which thread it is serving, and the way to restore an interrupt status after catching InterruptedException.

Please log in or register to report this question.

Answer: A

Two different monitors are involved, which is why a synchronized static method and a synchronized instance method on the same class do not exclude each other: one holds the lock on the Class object and the other on an instance. Guarding static state with an instance lock therefore protects nothing.

Please log in or register to report this question.

Answer: B

The tutorial states that reads and writes are atomic for reference variables and for most primitives, all types except long and double. Those two are the wide ones, and a non-volatile long can be observed half-written by another thread. Declaring it volatile makes its reads and writes atomic.

Please log in or register to report this question.

Answer: C

volatile does both jobs: reads and writes become atomic for all volatile variables including long and double, and each write establishes a happens-before relationship with subsequent reads of the same variable, so the change is always visible. A reader also sees the side effects of the code that led up to that write.

Please log in or register to report this question.

Answer: D

An increment expression is not an atomic action: it reads the value, adds one and writes it back, and two threads can interleave those steps and lose an update. volatile makes each individual read and write atomic and visible, which does not help across three of them. Compound updates need synchronization or a class from java.util.concurrent.atomic.

Please log in or register to report this question.

37.

Which one of the following statements about the relationship between Thread and Runnable is true?

Answer: A

Thread declares that it implements Runnable, so it satisfies the interface and can legally be handed to another Thread's constructor. Runnable declares only run(); start() belongs to Thread alone, which is why a Runnable cannot be started on its own.

Please log in or register to report this question.

Answer: B

Java allows a class only one superclass, so extending Thread spends it. Implementing Runnable keeps that slot free and separates the work from the machinery that runs it, which is why it is usually preferred. Either approach can create any number of threads.

Please log in or register to report this question.

Answer: C

run() is Runnable's only abstract method, which makes it a functional interface, so new Thread(() -> doWork()).start(); compiles. run() is also declared to throw nothing, which is why a checked exception inside it has to be caught rather than propagated.

Please log in or register to report this question.

Answer: D

Passing the same Runnable to several Thread objects is legal and common, and every one of those threads executes the same object, so its instance fields are shared mutable state needing protection. Nothing is copied and nothing is locked for you. The one-start rule applies to a Thread, not to a Runnable.

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