Wednesday 25 July 2012

java.lang.Thread - Objective Questions - Part 2

1)
Which of the following methods is not a member of the Object class?
  1. wait()
  2. notify()
  3. notifyAll()
  4. join()
Answer:join()

join() is a non-static method defined within the class java.lang.Thread.

2)
class A extends Thread {
  public void run() {
    try {sleep(10000);} catch (InterruptedException ie){}}
  public static void main(String[] args) {
    A a1 = new A();
    long startTime = System.currentTimeMillis();
    a1.start();
    System.out.print(System.currentTimeMillis() - startTime);
}}

What are the possible results of attempting to compile and run the program?
  1. Prints a number greater than or equal to 0
  2. The number printed must always be greater than 10000
  3. Compile-time error
  4. Run-time error

Answer:Prints a number greater than or equal to 0

Thread a1 will run for at least ten seconds, but the main method is likely to run to completion very quickly. The start method will return without waiting for thread a1 to complete. Since thread a1 immediately goes to sleep the thread that is processing the main method has an opportunity to complete the main method quickly. The number printed in the main method can be as small as zero.  

3)
class A extends Thread {String[] sa; public A(String[] sa) {this.sa = sa;}
  public void run() {synchronized (sa) {
  while (!sa[0].equals("Done")) {try {sa.wait();} catch (InterruptedException ie) {}
  }}System.out.print(sa[1] + sa[2] + sa[3]);}}
class B {  private static String[] sa = new String[]{"Not Done","X","Y","Z"};
public static void main (String[] args) {Thread t1 = new A(sa);
t1.start();synchronized(sa){sa[0] = "Done";sa[1]= "A";sa[2]="B";sa[3]="C";sa.notify();}}}
  1. Prints ABC
  2. Prints XYZ
  3. Compile-time error
  4. Run-time error
Answer:Prints ABC

Inside the main method, thread t1 is started and will move into the Running state at the discretion of the thread scheduler. The A.run method invokes the wait method on the String array object sa causing the thread to block until another thread invokes the sa.notify method. Before the B.main method invokes sa.notify, all of the elements of the String array object sa have already been updated.

4)
Which of the following methods are static members of the Thread class?

1. wait()
2. join()
3. run()
4. sleep()
5. yield()

Answer:sleep() and yield() are static methods declared within the class java.lang.Thread.

5)
Which of the following methods are deprecated members of the Thread class?

1. sleep()
2. stop()
3. suspend()
4. resume()
5. yield()
6. join()

Answer: stop(),suspend() and resume() .Every Java programmer should know that the deprecated methods should not be used in new programs.

6)
Which of the following thread state transitions model the lifecycle of a thread?

1. The Dead state to the Ready state
2. The Ready state to the Not-Runnable state
3. The Ready state to the Running state
4. The Running state to the Not-Runnable state
5. The Running state to the Ready state
6. The Not-Runnable state to the Ready state
7. The Not-Runnable state to the Running state

Answer:3,4,5&6

A dead thread can not be restarted.

7)
Which of the following methods name the InterruptedException in its throws clause?

1. run()
2. wait()
3. sleep()
4. join()
5. yield()
6. notify()

Answer:wait(),sleep(),join() declared within the class java.lang.Thread throws java.lang.InterruptedException.So always keep these functions within try-catch block else compile-time error will generate.

8)
class A implements Runnable {
  public void run() {System.out.print(Thread.currentThread().getName());}}
class B implements Runnable {
  public void run() {new A().run();
    new Thread(new A(),"T2").run();
    new Thread(new A(),"T3").start();}}
class C { public static void main (String[] args) {
    new Thread(new B(),"T1").start();}}

What is the result of attempting to compile and run the program?
  1. Prints: T1T1T1
  2. Prints: T1T2T3
  3. Prints: T1T1T3
  4. Prints: T1T3T3
Answer:Prints: T1T1T3

The Thread.currentThread method returns a reference to the currently executing thread. When the run method is invoked directly it does not start a new thread; so T1 is printed twice.

9)
class A extends Thread {
  private int i;
  public void run() {i = 1;}
  public static void main(String[] args) {
    A a = new A(); a.start(); System.out.print(a.i);
}}

What are the possible results of attempting to compile and run the program?
  1. Prints Nothing
  2. Prints 0
  3. Prints 1
  4. Prints 0 or 1
Answer:Prints 0 or 1

The new thread is started before the print statement, but there is no guarantee that the new thread will run before the print statement is processed. The guarantee could be provided by placing the method invocation expression a.join() before the print statement, but the invocation of the join method does not appear in the program. If the new thread runs before the print statement is processed, then 1 is printed. Otherwise, 0 is printed.

10)
class A extends Thread {
  public void run() {
    synchronized (this) {
      try {wait(5000);} catch (InterruptedException ie){}
  }}
  public static void main(String[] args) {
    A a1 = new A();
    long startTime = System.currentTimeMillis();
    a1.start();
    try {a1.join(6000);} catch (InterruptedException ie) {}
    System.out.print(System.currentTimeMillis() - startTime);
}}
What are the possible results of attempting to compile and run the program?
  1. The number printed is greater than or equal to 0
  2. The number printed is greater than 5000
  3. The number printed is greater than 6000
  4. Compile-time error
Answer:The number printed is greater than 5000

The notify method is never invoked on thread a1; so it will sleep for at least five seconds. The invocation of the join method forces the main thread to wait for the completion of thread a1. The argument of 6000 will allow the main thread to wait for six seconds if necessary, but we know that thread a1 will complete in only five seconds.

No comments:

Post a Comment