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

Threads - General Questions Questions and Answers

Answer: C

Correct answer: Runnable r = () -> System.out.println("go");

A lambda for Runnable takes no parameters and returns nothing, so it is an empty parameter list, the arrow token, and a statement. The arrow is -> and not =>; no return type is written; and adding a parameter would not match run(), which takes none.

Please log in or register to report this question.

33.

Which of these classes does Thread extend?

Answer: A

Thread extends Object like any class with no other superclass, and separately implements the Runnable interface. ThreadGroup is a container Thread refers to rather than a parent class, and Process is unrelated, describing an operating system process.

Please log in or register to report this question.

Answer: B

The two-argument constructor takes the Runnable first and the name second. There is no builder-style name method, and single quotes make a char literal rather than a String. setName can also be used after construction.

Please log in or register to report this question.

35.

Which of these is NOT a method declared on Thread?

Answer: C

notifyAll is declared on Object, so Thread has it only by inheritance, as every class does. setName, getState and isDaemon are all genuine Thread members. The distinction matters when reading which class the documentation puts a method under.

Please log in or register to report this question.

Answer: D

The statement takes a parenthesised expression followed by a BLOCK in braces, so both the parentheses and the braces are required. Unlike if or for, a synchronized statement cannot take a single statement without braces, and there is no method form.

Please log in or register to report this question.

Answer: A

synchronized describes how a method body acquires a lock, and an abstract method has no body, so the two cannot be combined. An abstract declaration on its own is fine, as is synchronized on any method that does have a body.

Please log in or register to report this question.

38.

What is the return type of Thread.interrupted()?

Answer: B

Correct answer: boolean

Thread.interrupted() reports whether the current thread has been interrupted and clears the flag as it does so, which is the trap in the name. isInterrupted() is the instance method that reports without clearing, and confusing the two loses interrupts.

Please log in or register to report this question.

39.

Which of these correctly starts a thread?

Answer: C

Correct answer: t.start();

start() creates the new thread of execution and arranges for run() to be called on it. Calling run() directly compiles and executes the code, but on the calling thread - so nothing runs concurrently, which is the classic beginner’s error.

Please log in or register to report this question.

Answer: D

Correct answer: <T> Future<T> submit(Callable<T> task)

Submission is asynchronous, so the method returns immediately with a Future standing for the eventual result rather than the result itself. The type parameter ties the future’s type to the callable’s, so get() returns the right type without a cast.

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