Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 18:38:25 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 15: Multithreading

Section 15.3 Creating Threads by Extending the Thread Class
1   Analyze the following code:

class Test extends Thread {
   public static void main(String[] args) {
     Thread t = new Thread(this);
     t.start();
   }

   public void run() {
     System.out.println("test");
   }
}

A. The program does not compile because this cannot be referenced in a static method.
B. The program compiles fine, but it does not print anything because t does not invoke the run() method.
C. The program compiles and runs fine and displays test on the console.
D. None of the above.

2   What is the output of the following code?

// Test.java: Define threads using the Thread class
public class Test {
   /** Main method */
   public static void main(String[] args) {
     new Test();
   }

   public Test() {
     // Create threads
     PrintChar printA = new PrintChar('a', 4);
     PrintChar printB = new PrintChar('b', 4);

     // Start threads
     printA.run();
     printB.run();
   }

   class PrintChar extends Thread {
     private char charToPrint; // The character to print
     private int times; // The times to repeat

     /** Construct a thread with specified character and number of
        times to print the character
      */
     public PrintChar(char c, int t) {
       charToPrint = c;
       times = t;
     }

     /** Override the run() method to tell the system
        what the thread will do
      */
     public void run() {
       for (int i = 0; i < times; i++)
         System.out.print(charToPrint);
     }
   }
}

A. aaaaabbbbb
B. bbbbbaaaaa
C. ababababab

Section 15.4 Creating Threads by Implementing the Runnable Interface
3   Analyze the following code:

abstract class Test implements Runnable {
   public void doSomething() {
   };
}

A. The program will not compile because it does not implement the run() method.
B. The program will not compile because it does not contain abstract methods.
C. The program compiles fine.
D. None of the above.

4   Analyze the following code:

class Test implements Runnable {
   public static void main(String[] args) {
     Test t = new Test();
     t.start();
   }

   public void run() {
   }
}

A. The program does not compile because the start() method is not defined in the Test class.
B. The program compiles, but it does not run because the start() method is not defined.
C. The program compiles, but it does not run because the run() method is not implemented.
D. The program compiles and runs fine.

5   Analyze the following code:

class Test implements Runnable {
   public static void main(String[] args) {
     Test t = new Test();
   }

   public Test() {
     Thread t = new Thread(this);
     t.start();
   }

   public void run() {
     System.out.println("test");
   }
}

A. The program has a compilation error because t is defined in both the main() method and the constructor Test().
B. The program compiles fine, but it does not run because you cannot use the keyword this in the constructor.
C. The program compiles and runs and displays nothing.
D. The program compiles and runs and displays test.

6   Why does the following class have a syntax error?

import java.applet.*;

class Test extends Applet implements Runnable {
    public void init() throws InterruptedException {
      Thread t = new Thread(this);
       t.sleep(1000);
    }

    public synchronized void run() {
    }
}

A. The sleep() method is not invoked correctly; it should be invoked as Thread.sleep(1000).
B. You cannot put the keyword synchronized in the run() method.
C. The init() method is defined in the Applet class, and it is overridden incorrectly because it cannot claim exceptions in the subclass.
D. The sleep() method should be put in the try-catch block. This is the only reason for the compilation failure.

7   Which method do you have to override if you implement the Runnable interface?

A. start()
B. stop()
C. resume()
D. init()
E. run()

8   Which of the following expressions must be true if you create a thread using Thread = new Thread(object)?

A. object instanceof Thread
B. object instanceof Frame
C. object instanceof Applet
D. object instanceof Runnable

Section 15.5 Thread Controls and Communications and Section 15.6 Thread States
9   The safe way to stop a thread in Java 2 is __________.

A. t = null;
B. t.stop();
C. t.destroy();
D. t.suspend();

10   When you run the following program, what will happen?

class Test extends Thread {
   public static void main(String[] args) {
     Test t = new Test();
     t.start();
     t.start();
   }

   public void run() {
     System.out.println("test");
   }
}

A. Nothing is displayed.
B. The program displays test twice.
C. The program displays test once.
D. An illegal java.lang.IllegalThreadStateException may be thrown because you just started thread and thread might have not yet finished before you start it again.

11   Which of the following method is a class method in java.lang.Thread?

A. run()
B. sleep(long)
C. start()
D. join()
E. setPriority(int)

12   Which of the following methods in Thread throws InterruptedException?

A. run()
B. sleep(long)
C. start()
D. yield()
E. setPriority(int)

13   Given the following code, which set of code can be used to replace the comment so that the program displays time to the console every second?

import java.applet.*;
import java.util.*;

class Test extends Applet implements Runnable {
   public void init() {
     Thread t = new Thread(this);
     t.start();
   }

   public void run() {
     for(; ;) {
       //display time every second
       System.out.println(new Date().toString());
     }
   }
}

A.  
try { sleep(1000); }
catch(InterruptedException e) { }

B.
try { t.sleep(1000); }
catch(InterruptedException e) { }

C.
try { Thread.sleep(1000); }
catch(RuntimeException e) { }

D.  
try { Thread.sleep(1000); }
catch(InterruptedException e) { }

Section 15.7 Thread Groups
14   Which of the following statements is correct to create a thread group and a new thread in the group?

A.  
ThreadGroup tg = new ThreadGroup();
Thread t1 = new Thread(tg, new Thread());

B.  
ThreadGroup tg = new ThreadGroup();
Thread t1 = new Thread(tg, new Thread(" "));

C.  
ThreadGroup tg = new ThreadGroup(" ");
Thread t1 = new Thread(tg, new Thread());

D.  
ThreadGroup tg = new ThreadGroup(" ");
Thread t1 = new Thread(tg, new Runnable());

Section 15.8 Synchronization
15   The keyword to synchronize methods in Java is __________.

A. synchronize
B. synchronizing
C. synchronized

16   How do you add a listener to an instance of the Timer class?

A. You can add a listener in the Timer constructor;
B. You can use the addActionListener method in the Timer class to add a listner.
C. Both a and b
D. A Timer object does not need a listener.
E. None of the above.

17   Which of the following is incorrect?

A. The wait(), notify(), and notifyAll() methods must be invoked from a synchronized method or a synchronized block.
B. When wait() is invoked, it pauses the thread and releases the lock on the object simultaneously. When the thread is restarted after being notified, the lock is automatically reacquired.
C. The notify() method can wake only one waiting thread.
D. An exception would occur if no thread is waiting on the object when the notify() method is invoked on the object.

18   Analyze the following code.

// Test.java: Define threads using the Thread class
import java.util.*;

public class Test {
   private Stack stack = new Stack();
   private int i = 0;

   /** Main method */
   public static void main(String[] args) {
     new Test();
   }

   public Test() {
     // Start threads
     new Producer().start();
     new Consumer().start();
   }

   class Producer extends Thread {
     public void run() {
       while (true) {
           System.out.println("Producer: put " + i);
           stack.push(new Integer(i++));
           synchronized (stack) {
             notifyAll();
           }
       }
     }
   }

   class Consumer extends Thread {
     public void run() {
       while (true) {
         synchronized (stack) {
           try {
             while (stack.isEmpty())
               stack.wait();
             System.out.println("Consumer: get " + stack.pop());
           }
           catch (InterruptedException ex) {
             ex.printStackTrace();
           }
         }
       }
     }
   }
}
A. The program creats two threads: one to add data to the stack and the other to get data from the stack.
B. The program has a compilation error on the notifyAll() method in the Producer class because it is not invoked from the stack object.
C. The program will throw an exception because the notifyAll() method in the Producer class is not invoked from the stack object.
D. The program has a logic error because the lock obtained by the synchronized block for notifyAll in the Producer class is stack and it should be this (i.e., synchronized (this) { notifyAll(); }).