Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Review Questions

13.15

Which one of these events will cause a thread to die?

Select the one correct answer.

  1. The method sleep() is called.
  2. The method wait() is called.
  3. Execution of the start() method ends.
  4. Execution of the run() method ends.
  5. Execution of the thread’s constructor ends.

13.16

Which statements are true about the following code?

public class Joining {
  static Thread createThread(final int i, final Thread t1) {
    Thread t2 = new Thread() {
      public void run() {
        System.out.println(i+1);
        try {
          t1.join();
        } catch (InterruptedException ie) {
        }
        System.out.println(i+2);
      }
    };
    System.out.println(i+3);
    t2.start();
    System.out.println(i+4);
    return t2;
  }

  public static void main(String[] args) {
    createThread(10, createThread(20, Thread.currentThread()));
  }
}

Select the two correct answers.

  1. The first number printed is 13.
  2. The number 14 is printed before the number 22.
  3. The number 24 is printed before the number 21.
  4. The last number printed is 12.
  5. The number 11 is printed before the number 23.

13.17

Which statements are true about the following program?

public class ThreadAPI {
  private static Thread t1 = new Thread("T1") {
    public void run() {
      try { wait(1000); } catch (InterruptedException ie){}
  }};

  private static Thread t2 = new Thread("T2") {
    public void run() {
      notify();
  }};

  private static Thread t3 = new Thread("T3") {
    public void run() {
      yield();
  }};

  private static Thread t4 = new Thread("T4") {
    public void run() {
      try { sleep(100); } catch (InterruptedException ie){}
  }};

  public static void main(String[] args) {
    t1.start(); t2.start(); t3.start(); t4.start();
    try { t4.join(); } catch (InterruptedException ie){}
  }
}

Select the three correct answers.

  1. The program will compile and will run and terminate normally.
  2. The program will compile but thread t1 will throw an exception.
  3. The program will compile but thread t2 will throw an exception.
  4. The program will compile but thread t3 will throw an exception.
  5. Enclosing the call to the sleep() method in a try-catch construct in thread t4 is unnecessary.
  6. Enclosing the call to the join() method in a try-catch construct in the main thread is necessary.

13.18

Which code, when inserted at (1), will result in the program compiling and printing Done on the standard input stream, and then all threads terminating normally?

public class RunningThreads {

  private static Thread t1 = new Thread("T1") {
    public void run() {
      synchronized(RunningThreads.class) {
        try {
          // (1) INSERT CODE HERE ...
        } catch (InterruptedException ie){
          ie.printStackTrace();
        }
        System.out.println("Done");
      }}};

   public static void main(String[] args) {
     t1.start();
     try {
       t1.join();
     } catch (InterruptedException ie){
       ie.printStackTrace();
     }
   }
}

Select the two correct answers.

  1. wait();
  2. wait(100);
  3. RunningThreads.class.wait();
  4. RunningThreads.class.wait(100);
  5. yield();
  6. sleep(100);

13.19

What can be guaranteed by calling the method yield()?

Select the one correct answer.

  1. All lower priority threads will be granted CPU time.
  2. The current thread will sleep for some time while some other threads run.
  3. The current thread will not continue until other threads have terminated.
  4. The thread will wait until it is notified.
  5. None of the above.

13.20

In which class or interface is the notify() method defined?

Select the one correct answer.

  1. Thread
  2. Object
  3. Appendable
  4. Runnable

13.21

How can the priority of a thread be set?

Select the one correct answer.

  1. By using the setPriority() method in the Thread class.
  2. By passing the priority as an argument to a constructor of the Thread class.
  3. Both of the above.
  4. None of the above.

13.22

Which statements are true about locks?

Select the two correct answers.

  1. A thread can hold more than one lock at a time.
  2. Invoking wait() on a Thread object will relinquish all locks held by the thread.
  3. Invoking wait() on an object whose lock is held by the current thread will relinquish the lock.
  4. Invoking notify() on a object whose lock is held by the current thread will relinquish the lock.
  5. Multiple threads can hold the same lock at the same time.

13.23

What will be the result of invoking the wait() method on an object without ensuring that the current thread holds the lock of the object?

Select the one correct answer.

  1. The code will fail to compile.
  2. Nothing special will happen.
  3. An IllegalMonitorStateException will be thrown if the wait() method is called while the current thread does not hold the lock of the object.
  4. The thread will be blocked until it gains the lock of the object.

13.24

Which of these are plausible reasons why a thread might be alive, but still not be running?

Select the four correct answers.

  1. The thread is waiting for some condition as a result of a wait() call.
  2. The execution has reached the end of the run() method.
  3. The thread is waiting to acquire the lock of an object in order to execute a certain method on that object.
  4. The thread does not have the highest priority and is currently not executing.
  5. The thread is sleeping as a result of a call to the sleep() method.

13.25

What will the following program print when compiled and run?

public class Tank {
  private boolean isEmpty = true;

  public synchronized void emptying() {
    pause(true);
    isEmpty = !isEmpty;
    System.out.println("emptying");
    notify();
  }

  public synchronized void filling() {
    pause(false);
    isEmpty = !isEmpty;
    System.out.println("filling");
    notify();
  }

  private void pause(boolean flag) {
    while(flag ? isEmpty : !isEmpty) {
      try {
        wait();
      } catch (InterruptedException ie) {
        System.out.println(Thread.currentThread() + " interrupted.");
      }
    }
  }

  public static void main(String[] args) {
    final Tank token = new Tank();
    (new Thread("A") { public void run() {for(;;) token.emptying();}}).start();
    (new Thread("B") { public void run() {for(;;) token.filling();}}).start();
  }
}

Select the one correct answer.

  1. The program will compile and continue running once started, but will not print anything.
  2. The program will compile and continue running once started, printing only the string "emptying".
  3. The program will compile and continue running once started, printing only the string "filling".
  4. The program will compile and continue running once started, always printing the string "filling" followed by the string "emptying".
  5. The program will compile and continue running once started, printing the strings "filling" and "emptying" in some order.

13.26

What will the following program print when compiled and run?

public class Syncher2 {
  final static int[] intArray = new int[2];

  private static void pause() {
    while(intArray[0] == 0) {
      try { intArray.wait(); }
      catch (InterruptedException ie) {
        System.out.println(Thread.currentThread() + " interrupted.");
      }
    }
  }

  public static void main (String[] args) {

    Thread runner = new Thread() {
      public void run() {
        synchronized (intArray) {
          pause();
          System.out.println(intArray[0] + intArray[1]);
    }}};

    runner.start();
    intArray[0] = intArray[1] = 10;
    synchronized(intArray) {
      intArray.notify();
    }
  }
}

Select the one correct answer.

  1. The program will not compile.
  2. The program will compile, but throw an exception when run.
  3. The program will compile and continue running once started, but will not print anything.
  4. The program will compile and print 0 and terminate normally, when run.
  5. The program will compile and print 20 and terminate normally, when run.
  6. The program will compile and print some other number than 0 or 20, and terminate normally, when run.
  • + Share This
  • 🔖 Save To Your Account