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

Threads - General Questions Questions and Answers

1.

Which of these constructs a thread that will run the code in an existing Runnable called task?

Answer: A

Thread has a constructor taking a Runnable, and that is the standard way to attach existing work to a thread. There is no static create factory on Thread, run takes no arguments, and Runnable is an interface with no constructor at all.

Please log in or register to report this question.

Answer: B

Thread offers a no-argument form, one taking a Runnable, one taking a name, and combinations of Runnable, name and ThreadGroup. Neither priority nor id is a constructor argument: priority is set afterwards with setPriority, and the id is assigned by the JVM and only readable.

Please log in or register to report this question.

Answer: C

The interface declares run with no parameters and a void return, so an override must match exactly. Adding a parameter makes it an unrelated overload rather than an override, changing the return type is illegal, and an interface method cannot be implemented with reduced visibility.

Please log in or register to report this question.

4.

Which of these Thread methods is static?

Answer: D

Correct answer: sleep

sleep is static because it acts on whichever thread is executing the call, not on the object it is written against. Writing t.sleep(100) compiles but puts the calling thread to sleep, which is misleading enough that compilers warn about it. join, interrupt and setName all act on a particular thread and are instance methods.

Please log in or register to report this question.

5.

Which one of these lists contains only static methods of Thread?

Answer: A

Correct answer: sleep, yield, currentThread

All three concern the thread that is running rather than any named one, which is what makes them static. The other lists mix in join, interrupt, start, setPriority and isAlive, each of which is addressed to a particular thread object and so cannot be static.

Please log in or register to report this question.

8.

Which of these correctly reads a thread's identifier?

Answer: D

Correct answer: long id = t.getId();

The accessor is getId() and it returns a long, since ids are not reused and could exhaust an int in a long-running process. Assigning it to an int would not compile without a cast, and there is no method simply called id().

Please log in or register to report this question.

9.

Which one of these lists contains only methods that Thread inherits from Object?

Answer: A

wait, notify and notifyAll belong to Object, so every object has them, not just threads. sleep, join and interrupt are declared on Thread itself. Beginners often expect the whole set to live on Thread, which is why they are so often confused.

Please log in or register to report this question.

10.

Which of these types is NOT declared in java.lang?

Answer: B

ExecutorService lives in java.util.concurrent and needs an import. Thread, Runnable and ThreadLocal are all in java.lang, which is imported implicitly, which is why thread code compiles with no import at all until the concurrency utilities are used.

Please log in or register to report this question.

Loading…
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