Threads Questions and Answers updated daily – Java



15 Threads Questions and answers section with explanation for various online exam preparation, various interviews, Java Threads online test. Threads Questions with detailed description, explanation will help you to master the topic.

Threads Questions

1.
public class MyRunnable implements Runnable 
{
    public void run() 
    {
        // some code here
    }
}
which of these will create and start this thread?



SHOW ANSWER
Correct Ans:new Thread(new MyRunnable()).start();
Explanation:
Because the class implements Runnable, an instance of it has to be passed to the Thread constructor, and then the instance of the Thread has to be started.

A is incorrect. There is no constructor like this for Runnable because Runnable is an interface, and it is illegal to pass a class or interface name to any constructor.

B is incorrect for the same reason; you can't pass a class or interface name to any constructor.

D is incorrect because MyRunnable doesn't have a start() method, and the only start() method that can start a thread of execution is the start() in the Thread class.


2. Which class or interface defines the wait(), notify(),and notifyAll() methods?



SHOW ANSWER
Correct Ans:object
Explanation:
The Object class defines these thread-specific methods.

Option B, C, and D are incorrect because they do not define these methods. And yes, the Java API does define a class called Class, though you do not need to know it for the exam.


3. Which of the following will not directly cause a thread to stop?



SHOW ANSWER
Correct Ans:notify()
Explanation:
Option A is correct. notify() - wakes up a single thread that is waiting on this object's monitor.

Option B is wrong. wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

Option C is wrong. Methods of the InputStream class block until input data is available, the end of the stream is detected, or an exception is thrown. Blocking means that a thread may stop until certain conditions are met.

Option D is wrong. sleep() - Causes the currently executing thread to sleep (temporarily cease execution) for a specified number of milliseconds. The thread does not lose ownership of any monitors.


4.
Assume the following method is properly synchronized and called from a thread A on an object B:

wait(2000);
After calling this method, when will the thread A become a candidate to get another turn at the CPU?



SHOW ANSWER
Correct Ans:After thread A is notified, or after two seconds.
Explanation:
Option A. Either of the two events (notification or wait time expiration) will make the thread become a candidate for running again.

Option B is incorrect because a waiting thread will not return to runnable when the lock is released, unless a notification occurs.

Option C is incorrect because the thread will become a candidate immediately after notification, not two seconds afterwards.

Option D is also incorrect because a thread will not come out of a waiting pool just because a lock has been released.


5. Which method registers a thread in a thread scheduler?



SHOW ANSWER
Correct Ans:start();
Explanation:
Option C is correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Option A is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.

Option B is wrong. There is no construct() method in the Thread class.

Option D is wrong. There is no register() method in the Thread class.


6. Which will contain the body of the thread?



SHOW ANSWER
Correct Ans:run();
Explanation:
Option A is Correct. The run() method to a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.

Option B is wrong. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Option C is wrong. The stop() method is deprecated. It forces the thread to stop executing.

Option D is wrong. Is the main entry point for an application.


7. Which method must be defined by a class implementing the java.lang.Runnable interface?



SHOW ANSWER
Correct Ans:public void run()
Explanation:
Option B is correct because in an interface all methods are abstract by default therefore they must be overridden by the implementing class. The Runnable interface only contains 1 method, the void run() method therefore it must be implemented.

Option A and D are incorrect because they are narrowing the access privileges i.e. package(default) access is narrower than public access.

Option C is not method in the Runnable interface therefore it is incorrect.


8. Which of the following will directly stop the execution of a Thread?



SHOW ANSWER
Correct Ans:wait()
Explanation:
Option A is correct. wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

Option B is wrong. notify() - wakes up a single thread that is waiting on this object's monitor.

Option C is wrong. notifyAll() - wakes up all threads that are waiting on this object's monitor.

Option D is wrong. Typically, releasing a lock means the thread holding the lock (in other words, the thread currently in the synchronized method) exits the synchronized method. At that point, the lock is free until some other thread enters a synchronized method on that object. Does entering/exiting synchronized code mean that the thread execution stops? Not necessarily because the thread can still run code that is not synchronized. I think the word directly in the question gives us a clue. Exiting synchronized code does not directly stop the execution of a thread.


9.
Which three guarantee that a thread will leave the running state?

yield()
wait()
notify()
notifyAll()
sleep(1000)
aLiveThread.join()
Thread.killThread()



SHOW ANSWER
Correct Ans:2,5 and 6
Explanation:
(2) is correct because wait() always causes the current thread to go into the object's wait pool.

(5) is correct because sleep() will always pause the currently running thread for at least the duration specified in the sleep argument (unless an interrupted exception is thrown).

(6) is correct because, assuming that the thread you're calling join() on is alive, the thread calling join() will immediately block until the thread you're calling join() on is no longer alive.

(1) is wrong, but tempting. The yield() method is not guaranteed to cause a thread to leave the running state, although if there are runnable threads of the same priority as the currently running thread, then the current thread will probably leave the running state.

(3) and (4) are incorrect because they don't cause the thread invoking them to leave the running state.

(7) is wrong because there's no such method.


10.
Which two of the following methods are defined in class Thread?

start()
wait()
notify()
run()
terminate()



SHOW ANSWER
Correct Ans:1 and 4
Explanation:
(1) and (4). Only start() and run() are defined by the Thread class.

(2) and (3) are incorrect because they are methods of the Object class. (5) is incorrect because there's no such method in any thread-related class.


11. Which cannot directly cause a thread to stop executing?



SHOW ANSWER
Correct Ans:Calling notify() method on an object.
Explanation:
notify() - wakes up a single thread that is waiting on this object's monitor.


12.
class X implements Runnable 
    public static void main(String args[]) 
    {
        /* Missing code? */
    } 
    public void run() {} 
}
Which of the following line of code is suitable to start a thread ?



SHOW ANSWER
Correct Ans:X run = new X(); Thread t = new Thread(run); t.start();
Explanation:


13.
Which three are methods of the Object class?

notify();
notifyAll();
isInterrupted();
synchronized();
interrupt();
wait(long msecs);
sleep(long msecs);
yield();



SHOW ANSWER
Correct Ans:1,2,6
Explanation:
(1), (2), and (6) are correct. They are all related to the list of threads waiting on the specified object.

(3), (5), (7), and (8) are incorrect answers. The methods isInterrupted() and interrupt() are instance methods of Thread.

The methods sleep() and yield() are static methods of Thread.

D is incorrect because synchronized is a keyword and the synchronized() construct is part of the Java language.


14.
Which two are valid constructors for Thread?

Thread(Runnable r, String name)
Thread()
Thread(int priority)
Thread(Runnable r, ThreadGroup g)
Thread(Runnable r, int priority)



SHOW ANSWER
Correct Ans:1 and 2
Explanation:
(1) and (2) are both valid constructors for Thread.

(3), (4), and (5) are not legal Thread constructors, although (4) is close. If you reverse the arguments in (4), you'd have a valid constructor.


15. What is the name of the method used to start a thread execution?



SHOW ANSWER
Correct Ans:start();
Explanation:
Option B is Correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Option A is wrong. There is no init() method in the Thread class.

Option C is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.

Option D is wrong. The resume() method is deprecated. It resumes a suspended thread.


Are you seeking for good platform for practicing Threads questions in online. This is the right place. The time you spent in Fresherslive will be the most beneficial one for you.

Online Test on Threads @ Fresherslive

This page provides important questions on Threads along with correct answers and clear explanation, which will be very useful for various Interviews, Competitive examinations and Entrance tests. Here, Most of the Threads questions are framed with Latest concepts, so that you may get updated through these Threads Online tests. Threads Online Test questions are granted from basic level to complex level.

Why To Practice Threads Test questions Online @ Fresherslive?

Threads questions are delivered with accurate answer. For solving each and every question, very lucid explanations are provided with diagrams wherever necessary.

Practice in advance of similar questions on Threads may improve your performance in the real Exams and Interview.

Time Management for answering the Threads questions quickly is foremost important for success in Competitive Exams and Placement Interviews.

Through Fresherslive Threads questions and answers, you can acquire all the essential idea to solve any difficult questions on Threads in short time and also in short cut method.

Winners are those who can use the simplest method for solving a question. So that they have enough time for solving all the questions in examination, correctly without any tense. Fresherslive provides most simplest methods to answer any tough questions. Practise through Fresherslive test series to ensure success in all competitive exams, entrance exams and placement tests.

Why Fresherslive For Threads Online Test Preparation?

Most of the job seekers finding it hard to clear Threads test or get stuck on any particular question, our Threads test sections will help you to success in Exams as well as Interviews. To acquire clear understanding of Threads, exercise these advanced Threads questions with answers.

You're Welcome to use the Fresherslive Online Test at any time you want. Start your beginning, of anything you want by using our sample Threads Online Test and create yourself a successful one. Fresherslive provides you a new opportunity to improve yourself. Take it and make use of it to the fullest. GOODLUCK for Your Bright Future.


FreshersLive - No.1 Job site in India. Here you can find latest 2017 government as well as private job recruitment notifications for different posts vacancies in India. Get top company jobs for both fresher and experienced. Job Seekers can get useful interview tips, resume services & interview Question and answer. Practice online test free which is helpful for interview preparation. Register with us to get latest employment news/rojgar samachar notifications. Also get latest free govt and other sarkari naukri job alerts daily through E-mail.