CommerceThreads MCQs
Practice Threads MCQs for competitive exams.
Threads MCQs
Practice questions from this topic.
Which of this method can be used to make the main thread to be executed last among all the threads?
- A. stop()
- B. sleep()
- C. join()
- D. call()
Correct Answer: C
Which of these keywords are used to implement synchronization? class newthread extends Thread { Thread t; String name; newthread(String threadname) { name = threadname; t = new Thread(this,name); t.start(); } public void run() { } } class multithreaded_programing { public static void main(String args[]) { newthread obj1 = new newthread("one"); newthread obj2 = new newthread("two"); try { obj1.t.wait(); System.out.print(obj1.t.isAlive()); } catch(Exception e) { System.out.print("Main thread interrupted"); } } }
- A. synchronize
- B. syn
- C. synch
- D. synchronized
Correct Answer: D
Which of these method wakes up the first thread that called wait()?
- A. wake()
- B. notify()
- C. start()
- D. notifyAll()
Correct Answer: B
What will be the output of the following Java code? class newthread implements Runnable { Thread t; newthread() { t1 = new Thread(this,"Thread_1"); t2 = new Thread(this,"Thread_2"); t1.start(); t2.start(); } public void run() { t2.setPriority(Thread.MAX_PRIORITY); System.out.print(t1.equals(t2)); } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } }
- A. true
- B. false
- C. truetrue
- D. falsefalse
Correct Answer: D
What will be the output of the following Java code? class newthread implements Runnable { Thread t; newthread() { t = new Thread(this,"My Thread"); t.start(); } public void run() { System.out.println(t.getName()); } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } }
- A. My Thread
- B. Thread[My Thread,5,main]
- C. Compilation Error
- D. Runtime Error
Correct Answer: A
What will be the output of the following Java code? class newthread implements Runnable { Thread t; newthread() { t = new Thread(this,"My Thread"); t.start(); } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } }
- A. My Thread
- B. Thread[My Thread,5,main]
- C. Compilation Error
- D. Runtime Error
Correct Answer: C
What will be the output of the following Java program? class newthread extends Thread { Thread t; String name; newthread(String threadname) { name = threadname; t = new Thread(this,name); t.start(); } public void run() { } } class multithreaded_programing { public static void main(String args[]) { newthread obj1 = new newthread("one"); newthread obj2 = new newthread("two"); try { obj1.t.wait(); System.out.print(obj1.t.isAlive()); } catch(Exception e) { System.out.print("Main thread interrupted"); } } }
- A. true
- B. false
- C. Main thread interrupted
- D. None of the mentioned
Correct Answer: C
What is true about threading?
- A. run() method calls start() method and runs the code
- B. run() method creates new thread
- C. run() method can be called directly without start() method being called
- D. start() method creates new thread and calls code written in run() method
Correct Answer: D
What will be the output of the following Java code? class newthread extends Thread { Thread t1,t2; newthread() { t1 = new Thread(this,"Thread_1"); t2 = new Thread(this,"Thread_2"); t1.start(); t2.start(); } public void run() { t2.setPriority(Thread.MAX_PRIORITY); System.out.print(t1.equals(t2)); } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } }
- A. true
- B. false
- C. truetrue
- D. falsefalse
Correct Answer: D
What will be the output of the following Java code? class newthread extends Thread { Thread t; newthread() { t = new Thread(this,"New Thread"); t.start(); } public void run() { System.out.println(t.isAlive()); } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } }
- A. 0
- B. 1
- C. true
- D. false
Correct Answer: C
What will be the output of the following Java code? class newthread implements Runnable { Thread t; newthread() { t = new Thread(this,"My Thread"); t.start(); } public void run() { System.out.println(t); } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } }
- A. My Thread
- B. Thread[My Thread,5,main]
- C. Compilation Error
- D. Runtime Error
Correct Answer: B
Which of this method is used to avoid polling in Java?
- A. wait()
- B. notify()
- C. notifyAll()
- D. all of the mentioned
Correct Answer: D