Home > Articles > Programming > Java

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

Review Questions

13.1

Which is the correct way to start a new thread?

Select the one correct answer.

  1. Just create a new Thread object. The thread will start automatically.
  2. Create a new Thread object and call the method begin().
  3. Create a new Thread object and call the method start().
  4. Create a new Thread object and call the method run().
  5. Create a new Thread object and call the method resume().

13.2

When extending the Thread class to implement the code executed by a thread, which method should be overridden?

Select the one correct answer.

  1. begin()
  2. start()
  3. run()
  4. resume()
  5. behavior()

13.3

Which statements are true?

Select the two correct answers.

  1. The class Thread is abstract.
  2. The class Thread implements Runnable.
  3. The Runnable interface has a single method named start.
  4. Calling the method run() on an object implementing Runnable will create a new thread.
  5. A program terminates when the last user thread finishes.

13.4

What will be the result of attempting to compile and run the following program?

public class MyClass extends Thread {
  public MyClass(String s) { msg = s; }
  String msg;
  public void run() {
    System.out.println(msg);
  }

  public static void main(String[] args) {
    new MyClass("Hello");
    new MyClass("World");
  }
}

Select the one correct answer.

  1. The program will fail to compile.
  2. The program will compile without errors and will print Hello and World, in that order, every time the program is run.
  3. The program will compile without errors and will print a never-ending stream of Hello and World.
  4. The program will compile without errors and will print Hello and World when run, but the order is unpredictable.
  5. The program will compile without errors and will simply terminate without any output when run.

13.5

What will be the result of attempting to compile and run the following program?

class Extender extends Thread {
  public Extender() { }
  public Extender(Runnable runnable) {super(runnable);}
  public void run() {System.out.print("|Extender|");}
}

public class Implementer implements Runnable {
  public void run() {System.out.print("|Implementer|");}
  public static void main(String[] args) {
    new Extender(new Implementer()).start();                 // (1)
    new Extender().start();                                  // (2)
    new Thread(new Implementer()).start();                   // (3)
  }
}

Select the one correct answer.

  1. The program will fail to compile.
  2. The program will compile without errors and will print |Extender| twice and |Implementer| once, in some order, every time the program is run.
  3. The program will compile without errors and will print |Extender| once and |Implementer| twice, in some order, every time the program is run.
  4. The program will compile without errors and will print |Extender| once and |Implementer| once, in some order, every time the program is run
  5. The program will compile without errors and will simply terminate without any output when run.
  6. The program will compile without errors, and will print |Extender| once and |Implementer| once, in some order, and terminate because of an runtime error.

13.6

What will be the result of attempting to compile and run the following program?

class R1 implements Runnable {
  public void run() {
    System.out.print(Thread.currentThread().getName());
  }
}
public class R2 implements Runnable {
  public void run() {
    new Thread(new R1(),"|R1a|").run();
    new Thread(new R1(),"|R1b|").start();
    System.out.print(Thread.currentThread().getName());
  }

  public static void main(String[] args) {
    new Thread(new R2(),"|R2|").start();
  }
}

Select the one correct answer.

  1. The program will fail to compile.
  2. The program will compile without errors and will print |R1a| twice and |R2| once, in some order, every time the program is run.
  3. The program will compile without errors and will print |R1b| twice and |R2| once, in some order, every time the program is run.
  4. The program will compile without errors and will print |R1b| once and |R2| twice, in some order, every time the program is run.
  5. The program will compile without errors and will print |R1a| once, |R1b| once, and |R2| once, in some order, every time the program is run.

13.7

What will be the result of attempting to compile and run the following program?

public class Threader extends Thread {
  Threader(String name) {
    super(name);
  }
  public void run() throws IllegalStateException {
    System.out.println(Thread.currentThread().getName());
    throw new IllegalStateException();
  }
  public static void main(String[] args) {
    new Threader("|T1|").start();
  }
}

Select the one correct answer.

  1. The program will fail to compile.
  2. The program will compile without errors, will print |T1|, and terminate normally every time the program is run.
  3. The program will compile without errors, will print|T1|, and throw an IllegalStateException, every time the program is run.
  4. None of the above.

13.8

What will be the result of attempting to compile and run the following program?

public class Worker extends Thread {
  public void run() {
    System.out.print("|work|");
  }
  public static void main(String[] args) {
    Worker worker = new Worker();
    worker.start();
    worker.run();
    worker.start();
  }
}

Select the one correct answer.

  1. The program will fail to compile.
  2. The program will compile without errors, will print |work| twice, and terminate normally every time the program is run.
  3. The program will compile without errors, will print|work| three times, and terminate normally every time the program is run.
  4. The program will compile without errors, will print|work| twice, and throw an IllegalStateException, every time the program is run.
  5. None of the above.
  • + Share This
  • 🔖 Save To Your Account