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

Threads - General Questions Questions and Answers

11.

Which of these Thread methods is deprecated?

Answer: C

stop is deprecated, as are suspend and resume, because they leave shared state in an unpredictable condition. join, interrupt and setDaemon are all current API. Compiling a call to a deprecated method still succeeds; the compiler only warns.

Please log in or register to report this question.

12.

Which one of these lists contains only deprecated Thread methods?

Answer: D

stop, suspend and resume are the deprecated trio, together with countStackFrames. join, yield, interrupt, sleep, setPriority and isAlive are all supported. The three were withdrawn together because each can leave locks or state inconsistent.

Please log in or register to report this question.

13.

What is the return type of isAlive()?

Answer: A

isAlive answers a yes or no question and returns a boolean. getState returns a Thread.State for finer detail, and neither method takes an argument. Confusing the two usually shows up as trying to compare isAlive against a state constant.

Please log in or register to report this question.

Answer: B

A constructor cannot be declared synchronized, because no other thread can reach the object until construction finishes. Instance methods, static methods and blocks may all carry the keyword. A synchronized block inside a constructor is legal if a lock is genuinely needed.

Please log in or register to report this question.

Answer: C

volatile and final cannot be combined: final says the value never changes after construction, while volatile exists to publish changes, so the pair is contradictory and the compiler rejects it. Every other combination shown is legal.

Please log in or register to report this question.

Answer: D

Thread implements Runnable, so a Thread reference is assignable to a Runnable variable. Runnable is an interface and cannot be instantiated directly without a body, and an Object is not a Thread so the last assignment needs a cast that would fail at runtime anyway.

Please log in or register to report this question.

17.

How many abstract methods does Runnable declare?

Answer: A

Correct answer: One

Runnable declares only run(), which is what makes it a functional interface and so expressible as a lambda. Having exactly one abstract method is the requirement for that, and it is why Runnable, Callable and Comparator can all be written as lambdas.

Please log in or register to report this question.

18.

Which of these is NOT a constant of Thread.State?

Answer: B

There is no SLEEPING state. A sleeping thread is reported as TIMED_WAITING, the same state as one in wait with a timeout or join with a timeout. The enum names describe why a thread is not running rather than which method put it there.

Please log in or register to report this question.

Answer: C

sleep takes milliseconds as a long, with a second form adding nanoseconds as an int. There is no overload taking a TimeUnit; that style belongs to the java.util.concurrent classes. An int argument widens to long automatically, which is why sleep(500) compiles.

Please log in or register to report this question.

Loading…

Topics

    No topic!
Tags: Threads - General Questions Questions and Answers || Threads - General Questions MCQ Questions and Answers || Threads - General Questions GK Questions and Answers || Threads - General Questions GK MCQ Questions || Threads - General Questions Multiple Choice Questions and Answers || Threads - General Questions GK || GK on Threads - General Questions || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming