Newsletter:

(Quiz) SCJP (Sun Certified Java Program) Threads

QUIZ: SCJP (Sun Certified Java Program) Threads

Questions 1 What will happen when you attempt to compile and run the following code?
public class Bground extends Thread{
public static void main(String argv[]){
Bground b = new Bground();
b.run();
}
public void start(){
for (int i = 0; i <10; i++){
System.out.println("Value of i = " + i);
}
}
}
(a) A compile time error indicating that no run method is defined for the Thread class.
(b) A run time error indicating that no run method is defined for the Thread class.
(c) Clean compile and at run time the values 0 to 9 are printed out.
(d) Clean compile but no output at runtime.

Answers 1 (d)
This is a bit of a sneaky one as I have swapped around the names of the methods you need to define and call when running a thread. If the for loop were defined in a method called public void run()
and the call in the main method had been to b.start()
The list of values from 0 to 9 would have been output.

Questions 2 What can cause a thread to stop executing?
(a) The program exits via a call to System.exit(0);
(b) Another thread is given a higher priority.
(c) A call to the thread's stop method.
(d) A call to the halt method of the Thread class.

Answers 2 (a), (b), (c)
Java threads are somewhat platform dependent and you should be carefull when making assumptions about Thread priorities. On some platforms you may find that a Thread with higher priorities gets to "hog" the processor. You can read up on this in more detail at http://java.sun.com/docs/books/tutorial/essential/threads/priority.html

Questions 3 Which statement is true of the following code?
public class Agg {
public static void main(String argv[]){
Agg a = new Agg();
a.go();
}
public void go(){
DSRoss ds1 = new DSRoss("one");
ds1.start();
}
}

class DSRoss extends Thread {
private String sTname = "";
DSRoss(String s){
sTname = s;
}
public void run(){
notwait();
System.out.println("finished");
}
public void notwait(){
while(true) {
try {
System.out.println("waiting");
wait();
} catch(InterruptedException ie){
}
System.out.println(sTname);
notifyAll();
}
}
}
(a) It will cause a compile time error.
(b) Compilation and output of "waiting".
(c) Compilation and output of "waiting" followed by "finished".
(d) Runtime error, an exception will be thrown.

Answers 3 (d)
A call to wait/notify must be within synchronized code. With JDK1.2 this code throws the error message
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:424)
at DSRoss.notwait(Compiled Code)
at DSRoss.run(Agg.java:21)

Questions 4 What will happen when you attempt to compile and run the following code?
public class Holt extends Thread {
private String sThreadName;
public static void main(String argv[]){
Holt h = new Holt();
h.go();
}
Holt() {
}
Holt(String s) {
sThreadName = s;
}
public String getThreadName() {
return sThreadName;
}
public void go() {
Holt first = new Holt("first");
first.start();
Holt second = new Holt("second");
second.start();
}
public void start(){
for(int i = 0; i < 2; i ++){
System.out.println(getThreadName() +i);
try {
Thread.sleep(100);
} catch(InterruptedException e) {
System.out.println(e.getMessage());}
}
}
}
(a) Compile time error.
(b) Output of first0, second0, first0, second1.
(c) Output of first0, first1, second0, second1.
(d) Runtime error.

Answers 4 (c)
Note that this code overrides and calls the start method. If you wished to get the output mixed you would need to override the run method but call the start method.

Questions 5 What will happen when you attempt to compile and run the following code?
class Background implements Runnable{
int i = 0;
public int run(){
while (true) {
i++;
System.out.println("i="+i);
}
return 1;
}
}//End class
(a) It will compile and the run method will print out the increasing value of i.
(b) It will compile and calling start will print out the increasing value of i.
(c) The code will cause an error at compile time.
(d) Compilation will cause an error because while cannot take a parameter of true.

Answers 5 (c)
The error is caused because run should have a void not an int return type.
Any class that is implements an interface must create a method to match all of the methods in the interface. The Runnable interface has one method called run that has a void return type.The sun compiler gives the error
Method redefined with different return type: int run() was defined as void run();

Questions 6 Which statement is true of the following code?
public class Rpcraven {
public static void main(String argv[]){
Pmcraven pm1 = new Pmcraven("one");
pm1.run();
Pmcraven pm2 = new Pmcraven("two");
pm2.run();
}
}
class Pmcraven extends Thread {
private String sTname="";
Pmcraven(String s) {
sTname = s;
}
public void run(){
for(int i =0; i < 2 ; i++){
try {
sleep(1000);
}catch(InterruptedException e){
}
yield();
System.out.println(sTname);
}
}
}
(a) Compile time error, class Rpcraven does not import java.lang.Thread
(b) Output of One One Two Two
(c) Output of One Two One Two
(d) Output of One Two One Two

Answers 6 (b)
Answer (c) would would be true if the code called the start method instead of the run method (well it is on my Windows machine anyway, I'm not sure it would be for ever implementation of Java Threads). If you call the run method directly it just acts as any other method and does not return to the calling code until it has finished executing.

Questions 7 You are creating a class that extends Object and implements Runnable. You have already written a run method for this class, and you need a way to create a thread and have it execute the run method, Which of these start methods should you use? [3]
(a) public void start() { new Thread(this).start(); }
(b) public void start() { Thread myT = new Thread(); myT.start(); }
(c) public void start() { Thread myT = new Thread(this); myT.run(); }

Answers 7 (a)
Answer (a) is ok, it does not matter that there is no reference to the thread in the start method. Answer (b) is incorrect because the new Thread is not attached to the Runnable object so it cannot find the run method. Instead, the default run in the Thread class will be executed. Answer (c) is incorrect because the Thread that is executing the start method calls run in the Thread class. The myT Thread is not started.

Question 8 Java Thread A is attached to an object B, which is responsible for writing data to a file. After writing data, Thread A calls the following method in object B, where it waits until more data is available.
private void synchronized waitForData() { // (1)
try { // (2)
wait(); // (3)
} catch (InterruptedException ex) { // (4)
}
}
Another Thread, executing a method in another object C, needs to wake up Thread A. Assuming that object C has references to both A and B, select all of the following code fragments that would cause Thread A to exit the waitForData method.
(a) A.interrupt();
(b) synchronized(A) { A.notifyAll(); }
(c) synchronized(B) { B.notifyAll(); }
(d) A.resume();

Answers 8 (a), (c)
Answer (a) is correct because it will generate an InterruptedException, bringing thread A out of the wait. Because line (4) catches this exception, the thread exits the waitForData method normally. Answer (c) is correct because it removes all Threads waiting for object B from the wait list, including thread A. Answer (b) is incorrect because it refers to the thread A, not to the object B for which the thread is waiting. Answer (d) is incorrect because the resume method works only with suspended threads and is a deprecated method.

Questions 9 You have created a TimeOut class as an extension of Thread, the purpose of which is to print a “Time’s Up” message if the Thread is not interrupted within 10 seconds of being started. Here is the run method that you have coded:
public void run() {
System.out.println(“Start!”);
try {
Thread.sleep(10000);
System.out.println(“Time’s Up!”);
} catch (InterruptedException e) {
System.out.println(“Interrupted!”);
}
}
Given that a program creates and starts a TimeOut object, which of the following statements is true?
(a) Exactly 10 seconds after the start method is called, “Time’s Up!” will be printed.
(b) Exactly 10 seconds after the “Start!” is printed, “Time’s Up!” will be printed.
(c) The delay between “Start!” being printed and “Time’s Up!” will be 10 seconds plus or minus one tick of the system clock.
(d) If “Time’s Up!” is printed, you can be sure that at least 10 seconds have elapsed since “Start!” was printed.

Answers 9 (d)
It is the only statement that can be made with confidence. Answer (a), (b), (c) are all incorrect because the expiration of a sleep timer does not guarantee that a Thread will run – only that it can run.