Threads Questions and Answers
Threads in Java enable concurrent execution of multiple tasks, improving program efficiency and responsiveness. This concept is frequently tested in programming questions and answers and Java MCQs during placement drives by TCS, Infosys, and Wipro. Understanding thread creation, synchronization, and lifecycle is crucial for handling multitasking in software applications. Practicing Java programming interview questions with answers on threads enhances students’ understanding of concurrency, helping them tackle real-world programming challenges confidently. Knowledge of threads also plays a vital role in acing technical interviews and coding assessments.
Threads
Showing 10 of
15 questions
1. What is the name of the method used to start a thread execution?
- init();
- start();
- run();
- resume();
2. Which two are valid constructors for Thread? 1 Thread(Runnable r, String name) 2 Thread() 3 Thread(int priority) 4 Thread(Runnable r, ThreadGroup g) 5 Thread(Runnable r, int priority)
- 1 and 3
- 2 and 4
- 1 and 2
- 2 and 5
3. Which three are methods of the Object class? notify(); notifyAll(); isInterrupted(); synchronized(); interrupt(); wait(long msecs); sleep(long msecs); yield();
- 1, 2, 4
- 2, 4, 5
- 1, 2, 6
- 2, 3, 4
4. 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 ?
- Thread t = new Thread(X);
- Thread t = new Thread(X); t.start();
- X run = new X(); Thread t = new Thread(run); t.start();
- Thread t = new Thread(); x.run();
5. Which cannot directly cause a thread to stop executing?
- Calling the SetPriority() method on a Thread object.
- Calling the wait() method on an object.
- Calling notify() method on an object.
- Calling read() method on an InputStream object.
6. Which two of the following methods are defined in class Thread? start() wait() notify() run() terminate()
- 1 and 4
- 2 and 3
- 3 and 4
- 2 and 4
7. Which three guarantee that a thread will leave the running state? yield() wait() notify() notifyAll() sleep(1000) aLiveThread.join() Thread.killThread()
- 1, 2 and 4
- 2, 5 and 6
- 3, 4 and 7
- 4, 5 and 7
8. Which of the following will directly stop the execution of a Thread?
- wait()
- notify()
- notifyall(
- exits synchronized code
9. Which method must be defined by a class implementing the java.lang.Runnable interface?
- void run()
- public void run()
- public void start()
- void run(int priority)