Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The following questions will help you measure your understanding of the material presented in this chapter. If you have a rough time with some of these at first, don’t beat yourself up. Some of these questions are long and intricate, expect long and intricate questions on the real exam too!
| 1. | The following block of code creates a Thread using a Runnable target:
Runnable target = new MyRunnable(); Thread myThread = new Thread(target); Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
|
| 2. | Given:
3. class MyThread extends Thread {
4. public static void main(String [] args) {
5. MyThread t = new MyThread();
6. Thread x = new Thread(t);
7. x.start();
8. }
9. public void run() {
10. for(int i=0;i<3;++i) {
11. System.out.print(i + "..");
12. }
13. }
14. }What is the result of this code?
|
| 3. | Given:
3. class Test {
4. public static void main(String [] args) {
5. printAll(args);
6. }
7. public static void printAll(String[] lines) {
8. for(int i=0;i<lines.length;i++){
9. System.out.println(lines[i]);
10. Thread.currentThread().sleep(1000);
11. }
12. }
13. }The static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?
|
| 4. | Assume you have a class that holds two private variables: a and b. Which of the following pairs can prevent concurrent access problems in that class? (Choose all that apply.)
|
| 5. | Given:
1. public class WaitTest {
2. public static void main(String [] args) {
3. System.out.print("1 ");
4. synchronized(args){
5. System.out.print("2 ");
6. try {
7. args.wait();
8. }
9. catch(InterruptedException e){}
10. }
11. System.out.print("3 ");
12. }
13. }What is the result of trying to compile and run this program?
|
| 6. | Assume the following method is properly synchronized and called from a thread A on an object B:
wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU?
|
| 7. | Which are true? (Choose all that apply.)
|
| 8. | Given the scenario: This class is intended to allow users to write a series of messages, so that each message is identified with a timestamp and the name of the thread that wrote the message:
public class Logger {
private StringBuilder contents = new StringBuilder();
public void log(String message) {
contents.append(System.currentTimeMillis());
contents.append(": ");
contents.append(Thread.currentThread().getName());
contents.append(message);
contents.append("\n");
}
public String getContents() { return contents.toString(); }
}How can we ensure that instances of this class can be safely used by multiple threads?
|
| 9. | Given:
public static synchronized void main(String[] args) throws
InterruptedException {
Thread t = new Thread();
t.start();
System.out.print("X");
t.wait(10000);
System.out.print("Y");
}What is the result of this code?
|
| 10. | Given:
class MyThread extends Thread {
MyThread() {
System.out.print(" MyThread");
}
public void run() {
System.out.print(" bar");
}
public void run(String s) {
System.out.print(" baz");
}
}
public class TestThreads {
public static void main (String [] args) {
Thread t = new MyThread() {
public void run() {
System.out.print(" foo");
}
};
t.start();
} }What is the result?
|
| 11. | Given:
Code View:
Scroll
/
Show All public class ThreadDemo {
synchronized void a() { actBusy(); }
static synchronized void b() { actBusy(); }
static void actBusy() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
public static void main(String[] args) {
final ThreadDemo x = new ThreadDemo();
final ThreadDemo y = new ThreadDemo();
Runnable runnable = new Runnable() {
public void run() {
int option = (int) (Math.random() * 4);
switch (option) {
case 0: x.a(); break;
case 1: x.b(); break;
case 2: y.a(); break;
case 3: y.b(); break;
}
}
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
thread1.start();
thread2.start();
}
}
Which of the following pairs of method invocations could NEVER be executing at the same time? (Choose all that apply.)
|
| 12. | Given:
Code View:
Scroll
/
Show All public class TwoThreads {
static Thread laurel, hardy;
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
System.out.println("A");
try {
hardy.sleep(1000);
} catch (Exception e) {
System.out.println("B");
}
System.out.println("C");
}
};
hardy = new Thread() {
public void run() {
System.out.println("D");
try {
laurel.wait();
} catch (Exception e) {
System.out.println("E");
}
System.out.println("F");
}
};
laurel.start();
hardy.start();
}
}
Which letters will eventually appear somewhere in the output? (Choose all that apply.)
|
| 13. | Given:
3. public class Starter implements Runnable {
4. void go(long id) {
5. System.out.println(id);
6. }
7. public static void main(String[] args) {
8. System.out.print(Thread.currentThread().getId() + " ");
9. // insert code here
10. }
11. public void run() { go(Thread.currentThread().getId()); }
12. }And given the following five fragments: I. new Starter().run(); II. new Starter().start(); III. new Thread(new Starter()); IV. new Thread(new Starter()).run(); V. new Thread(new Starter()).start(); When the five fragments are inserted, one at a time at line 9, which are true? (Choose all that apply.)
|
| 14. | Given:
3. public class Leader implements Runnable {
4. public static void main(String[] args) {
5. Thread t = new Thread(new Leader());
6. t.start();
7. System.out.print("m1 ");
8. t.join();
9. System.out.print("m2 ");
10. }
11. public void run() {
12. System.out.print("r1 ");
13. System.out.print("r2 ");
14. }
15. }Which are true? (Choose all that apply.)
|
| 15. | Given:
Code View:
Scroll
/
Show All 3. class Dudes {
4. static long flag = 0;
5. // insert code here
6. if(flag == 0) flag = id;
7. for(int x = 1; x < 3; x++) {
8. if(flag == id) System.out.print("yo ");
9. else System.out.print("dude ");
10. }
11. }
12. }
13. public class DudesChat implements Runnable {
14. static Dudes d;
15. public static void main(String[] args) {
16. new DudesChat().go();
17. }
18. void go() {
19. d = new Dudes();
20. new Thread(new DudesChat()).start();
21. new Thread(new DudesChat()).start();
22. }
23. public void run() {
24. d.chat(Thread.currentThread().getId());
25. }
26. }
And given these two fragments: I. synchronized void chat(long id) {
II. void chat(long id) {When fragment I or fragment II is inserted at line 5, which are true? (Choose all that apply.)
|
| 16. | Given:
Code View:
Scroll
/
Show All 3. class Chicks {
4. synchronized void yack(long id) {
5. for(int x = 1; x < 3; x++) {
6. System.out.print(id + " ");
7. Thread.yield();
8. }
9. }
10. }
11. public class ChicksYack implements Runnable {
12. Chicks c;
13. public static void main(String[] args) {
14. new ChicksYack().go();
15. }
16. void go() {
17. c = new Chicks();
18. new Thread(new ChicksYack()).start();
19. new Thread(new ChicksYack()).start();
20. }
21. public void run() {
22. c.yack(Thread.currentThread().getId());
23. }
24. }
Which are true? (Choose all that apply.)
|
| 17. | Given:
3. public class Chess implements Runnable {
4. public void run() {
5. move(Thread.currentThread().getId());
6. }
7. // insert code here
8. System.out.print(id + " ");
9. System.out.print(id + " ");
10. }
11. public static void main(String[] args) {
12. Chess ch = new Chess();
13. new Thread(ch).start();
14. new Thread(new Chess()).start();
15. }
16. }And given these two fragments: I. synchronized void move(long id) {
II. void move(long id) {When either fragment I or fragment II is inserted at line 7, which are true? (Choose all that apply.)
|