Chapter 13: Multithreading 1. 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. # 2. 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. # 3. 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. The program has a runtime error because the thread t was started twice. # 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. Which of the following method is a class method in Thread? a. suspend() b. sleep() c. start() d. resume() e. setPriority() # 7. Which of the following methods in Thread throws InterruptedException? a. suspend() b. sleep() c. start() d. resume() e. setPriority() # 8. Which of the following methods is (are) not defined in ThreadGroup? a. suspend() b. setMaxPriority() c. resume() d. start() e. stop() # 9. 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) # 10. Which method do you have to override if you implement the Runnable interface? a. start() b. stop() c. resume() d. init() e. run() # 11. 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. # 12. 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 # 13. 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()); # 14. The keyword to synchronize methods in Java is __________. a. synchronize b. synchronizing c. synchronized # 15. The safe way to stop a thread in JDK 1.2 is __________. a. t = null; b. t.stop(); c. t.destroy(); d. t.suspend(); # 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. & End of the questions. Answers begins 1.b 2.a 3.d 4.a 5.d 6.b 7.b 8.d 9.b 10.e 11.c 12.d 13.c 14.c 15.a 16.c