Home > Java Programming > Threads > Questions and Answers
01. |
public class Threads5 { public static void main (String[] args) { new Thread(new Runnable() { public void run() { System.out.print("bar"); }}).start(); } } What is the result? | |||||||||||
|
02. |
12. public class Starter extends Thread { private int x = 2; public static void main(String[] args) throws Exception { new Starter().makeItSo(); } public Starter() { x = 5; start(); } public void makeItSo() throws Exception { join(); x = x - 1; System.out.println(x); } public void run() { x *= 2; } } What is the output if the main() method is run? | |||||||||||||||
|
03. |
What will happen when you attempt to compile and run the following code ? 1. public class Test extends Thread{ 2. public static void main(String argv[]){ 3. Test t = new Test(); 4. t.run(); 5. t.start(); 6. } 7. public void run(){ 8. System.out.println("run-test"); 9. } 10. } | |||||||||||
|
04. |
What is the output for the below code ? class A implements Runnable{ public void run(){ System.out.println("run-a"); } } 1. public class Test { 2. public static void main(String... args) { 3. A a = new A(); 4. Thread t = new Thread(a); 5. t.start(); 6. t.start(); 7. } 8. } | |||||||||||
|
05. |
What is the output for the below code ? class A implements Runnable{ public void run(){ try{ for(int i=0;i<4;i++){ Thread.sleep(100); System.out.println(Thread.currentThread().getName()); } }catch(InterruptedException e){ } } } public class Test { public static void main(String argv[]) throws Exception{ A a = new A(); Thread t = new Thread(a,"A"); Thread t1 = new Thread(a,"B"); t.start(); t.join(); t1.start(); } } | |||||||||||
|
06. |
What is the output for the below code ? public class B { public synchronized void printName(){ try{ System.out.println("printName"); Thread.sleep(5*1000); }catch(InterruptedException e){ } } public synchronized void printValue(){ System.out.println("printValue"); } } public class Test extends Thread{ B b = new B(); public static void main(String argv[]) throws Exception{ Test t = new Test(); Thread t1 = new Thread(t,"t1"); Thread t2 = new Thread(t,"t2"); t1.start(); t2.start(); } public void run(){ if(Thread.currentThread().getName().equals("t1")){ b.printName(); }else{ b.printValue(); } } } | |||||||||||
|
07. |
What is the output for the below code ? public class B { public static synchronized void printName(){ try{ System.out.println("printName"); Thread.sleep(5*1000); }catch(InterruptedException e){ } } public synchronized void printValue(){ System.out.println("printValue"); } } public class Test extends Thread{ B b = new B(); public static void main(String argv[]) throws Exception{ Test t = new Test(); Thread t1 = new Thread(t,"t1"); Thread t2 = new Thread(t,"t2"); t1.start(); t2.start(); } public void run(){ if(Thread.currentThread().getName().equals("t1")){ b.printName(); }else{ b.printValue(); } } } | |||||||||||
|
08. |
What is the output for the below code ? class A extends Thread{ int count = 0; public void run(){ System.out.println("run"); synchronized (this) { for(int i =0; i < 50 ; i++){ count = count + i; } notify(); } } } public class Test{ public static void main(String argv[]) { A a = new A(); a.start(); synchronized (a) { System.out.println("waiting"); try{ a.wait(); }catch(InterruptedException e){ } System.out.println(a.count); } } } | |||||||||||
|
09. |
Which of the following statements about this code are true? class A extends Thread{
public void run(){ for(int i =0; i < 2; i++){ System.out.println(i); } } } public class Test{ public static void main(String argv[]){ Test t = new Test(); t.check(new A(){}); } public void check(A a){ a.start(); } } | |||||||||||
|
10. |
What will happen when you attempt to compile and run the following code? class A implements Runnable{ public void run(){ System.out.println("run-A"); } } 1. public class Test { 2. public static void main(String argv[]){ 3. A a = new A(); 4. Thread t = new Thread(a); 5. System.out.println(t.isAlive()); 6. t.start(); 7. System.out.println(t.isAlive()); 8. } 9. } | |||||||||||
|