Review Questions
13.1 |
Which is the correct way to start a new thread? Select the one correct answer.
|
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.
|
13.3 |
Which statements are true? Select the two correct answers.
|
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.
|
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.
|
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.
|
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.
|
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.
|