Home > Java Programming > Threads > Questions and Answers
01. |
What will be the output of the program? class MyThread extends Thread { MyThread() { System.out.print(" MyThread"); } public void run() { System.out.print(" bar"); } public void run(String s) { System.out.println(" baz"); } } public class TestThreads { public static void main (String [] args) { Thread t = new MyThread() { public void run() { System.out.println(" foo"); } }; t.start(); } } | |||||||||||
|
02. |
What will be the output of the program? class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } } | |||||||||||
|
03. |
What will be the output of the program? class s1 extends Thread { public void run() { for(int i = 0; i < 3; i++) { System.out.println("A"); System.out.println("B"); } } } class Test120 extends Thread { public void run() { for(int i = 0; i < 3; i++) { System.out.println("C"); System.out.println("D"); } } public static void main(String args[]) { s1 t1 = new s1(); Test120 t2 = new Test120(); t1.start(); t2.start(); } } | |||||||||||
|
04. | What is the name of the method used to start a thread execution? | |||||||||||
|
05. |
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) | |||||||||||
|
06. | Which statement is true? | |||||||||||
|
07. |
Which two statements are true? 1. Deadlock will not occur if wait()/notify() is used 2. A thread will resume execution as soon as its sleep duration expires. 3. Synchronization can prevent two objects from being accessed by the same thread. 4. The wait() method is overloaded to accept a duration. 5. The notify() method is overloaded to accept a duration. 6. Both wait() and notify() must be called from a synchronized context. | |||||||||||
|
08. | Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.) | |||||||||||
|
09. |
public class Threads3 implements Runnable { public void run() { System.out.print("running"); } public static void main(String[] args) { Thread t = new Thread(new Threads3()); t.run(); t.run(); t.start(); } } What is the result? | |||||||||||||||
|
10. |
public class Threads4 { public static void main (String[] args) { new Threads4().go(); } public void go() { Runnable r = new Runnable() { public void run() { System.out.print("foo"); } }; Thread t = new Thread(r); t.start(); t.start(); } } What is the result?
| |||||||||||
|