SCJP考试中的线程题目分析
2006-08-06 17:47
447 查看
1:
Which two CANNOT directly cause a thread to stop executing? (Choose Two)
A.Existing from a synchronized block
B.Calling the wait method on an object
C.Calling notify method on an object
D.Calling read method on an InputStream object
E.Calling the SetPriority method on a Thread object
Answer:AC。同55题
2:
public class SyncTest{
public static void main(String[] args) {
final StringBuffer s1= new StringBuffer();
final StringBuffer s2= new StringBuffer();
new Thread () {
public void run() {
synchronized(s1) {
s2.append(“A”);
synchronized(s2) {
s2.append(“B”);
System.out.print(s1);
System.out.print(s2);
}
}
}
}.start();
new Thread() {
public void run() {
synchronized(s2) {
s2.append(“C”);
synchronized(s1) {
s1.append(“D”);
System.out.print(s2);
System.out.print(s1);
}
}
}
}.start();
}
}
Which two statements are true? (Choose Two)
A. The program prints “ABBCAD”
B. The program prints “CDDACB”
C. The program prints “ADCBADBC”
D. The output is a non-deterministic point because of a possible deadlock condition
E. The output is dependent on the threading model of the system the program is running on.
Answer:DE
3:
What will happen when you attempt to compile and run the following code?
public class Test{
int i = 0;
public static void main(String argv[]) {
Test t = new Test();
t.myMethod();
}
public void myMethod(){
while(true) {
try {
wait();
}catch (InterruptedException e) {}
i++;
}
}
}
A. Compile time error, no matching notify within the method.
B. Compile and run but an infinite looping of the while method.
C. Compilation and run without any output.
E. Runtime Exception "IllegalMonitorStatException".
Answer: E
Note: The wait/notify protocol can only be used within code that is synchronized. In this case calling code does not have a lock on the object(not synchronized) and will thus cause an Exception at runtime.
4:
1.10 What is the result of compiling and executing the following code?
public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
yield();
System.out.println("Leaving run");
}
public static void main(String args []) {
(new ThreadTest()).start();
}
}
A. The code fails to compile in the main() method.
B. The code fails to compile in the run() method.
C. Only the text "In run" will be displayed.
D. The text "In run" followed by "Leaving run" will be displayed.
E. The code compiles correctly, but nothing is displayed.
Answer: D
5:
1.12 Which of the following will definitely stop a thread from executing?A. wait()B. notify()C. yield()D. suspend()E. sleep()Answer: ACDE
6:
1.12 Which of the following will definitely stop a thread from executing?
A. wait()
B. notify()
C. yield()
D. suspend()
E. sleep()
Answer: ACDE
7:
Which of the following statements about threading are true?
A. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable.
B. You can obtain a mutually exclusive lock on any object.
C. You can't obtain a mutually exclusive lock on any object.
D. Thread scheduling algorithms are platform dependent.
Answer: BD
8:
Consider the following statement:
Thread myThread = new Thread();
Which of the following statements are true regarding myThread?
A. The thread myThread is now in a runnable state.
B. The thread myThread has a priority of 5.
C. On calling the start() method on myThread, the run method in the Thread class will be executed.
D. On calling the start() method on myThread, the run method in the calling class will be executed.
Answer: C
Note: the priority of myThread will be inherited from the Thread that called the constructor.
9:
What is the effect of issuing a wait() method on an object?(Mutiple)
1) If a notify() method has already been sent to that object then it has no effect
2) The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
3) An exception will be raised
4) The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.
ANSWER 1)
10:
Pick all the true statements below.
1) If a thread wants to call wait() on an object, the thread must own that object's lock.
2) There is a method that you can call on an instance of the Thread class that puts the instance to sleep for a specified number of milliseconds.
3) At the moment when a thread is notified, it automatically gets the lock of the object for which it was waiting.
ANSWER 1
11:
1. class Z {
2. public static void main(String[] args) {
3. new Z();
4. }
5.
6. Z() {
7. Z alias1 = this;
8. Z alias2 = this;
9. synchronized(alias1) {
10. try {
11. alias2.wait();
12. System.out.println("DONE WAITING");
13. }
14. catch (InterruptedException e) {
15. System.out.println("INTERRUPTED");
16. }
17. catch (Exception e) {
18. System.out.println("OTHER EXCEPTION");
19. }
20. finally {
21. System.out.println("FINALLY");
22. }
23. }
24. System.out.println("ALL DONE");
25. }
26. }
Mutiple:
1) Compiler error.
2) The application compiles but doesn't print anything.
3) The application prints "DONE WAITING".
4) The application prints "INTERRUPTED".
5) The application prints "OTHER EXCEPTION".
ANS: 2)
12:
What will happen when you attempt to compile and run the following code?
class MyThread extends Thread{
public void run(){
System.out.println("MyThread: run()");
}
public void start(){////注意,此方法重写后,不再有CALL RUN()方法的功能。
System.out.println("MyThread: start()");
}
}
class MyRunnable implements Runnable{
public void run(){
System.out.println("MyRunnable: run()");
}
public void start(){//Runnable 并无此方法,所以,除非本类的对象可以CALL 它,否则,将CALL THREAD 的start()执行run()
System.out.println("MyRunnable: start()");
}
}
public class MyTest {
public static void main(String args[]){
MyThread myThread = new MyThread();
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
myThread.start();
thread.start();
}
}
A.prints: MyThread: start() followed by MyRunnable: run()
B.prints: MyThread: run() followed by MyRunnable: start()
C.prints: MyThread: start() followed by MyRunnable: start()
D.prints: MyThread: run() followed by MyRunnable: run()
E.compile time error
F.None of the above
ANSWER:A
13:
Multiple objects of MyClass (given below) are used in a program that uses multiple Threads to create new integer count. What will happen when other threads use the following code? D
class MyClass{
static private int myCount = 0;
int yourNumber;
private static synchronized int nextCount(){
return ++myCount; //myCount为static
}
public void getYourNumber(){
yourNumber = nextCount();
}
}
A. the code ill give ompilation error
B. the code ill give runtime error
C. each thread will get a unique number
D. the uniqueness of the number different Threads can’t be guaranteed.
C
14:
What is the effect of issuing a wait() method on an object
Mutiple:
1) If a notify() method has already been sent to that object then it has no effect
2) The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
3) An exception will be raised
4) The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.
Which two CANNOT directly cause a thread to stop executing? (Choose Two)
A.Existing from a synchronized block
B.Calling the wait method on an object
C.Calling notify method on an object
D.Calling read method on an InputStream object
E.Calling the SetPriority method on a Thread object
Answer:AC。同55题
2:
public class SyncTest{
public static void main(String[] args) {
final StringBuffer s1= new StringBuffer();
final StringBuffer s2= new StringBuffer();
new Thread () {
public void run() {
synchronized(s1) {
s2.append(“A”);
synchronized(s2) {
s2.append(“B”);
System.out.print(s1);
System.out.print(s2);
}
}
}
}.start();
new Thread() {
public void run() {
synchronized(s2) {
s2.append(“C”);
synchronized(s1) {
s1.append(“D”);
System.out.print(s2);
System.out.print(s1);
}
}
}
}.start();
}
}
Which two statements are true? (Choose Two)
A. The program prints “ABBCAD”
B. The program prints “CDDACB”
C. The program prints “ADCBADBC”
D. The output is a non-deterministic point because of a possible deadlock condition
E. The output is dependent on the threading model of the system the program is running on.
Answer:DE
3:
What will happen when you attempt to compile and run the following code?
public class Test{
int i = 0;
public static void main(String argv[]) {
Test t = new Test();
t.myMethod();
}
public void myMethod(){
while(true) {
try {
wait();
}catch (InterruptedException e) {}
i++;
}
}
}
A. Compile time error, no matching notify within the method.
B. Compile and run but an infinite looping of the while method.
C. Compilation and run without any output.
E. Runtime Exception "IllegalMonitorStatException".
Answer: E
Note: The wait/notify protocol can only be used within code that is synchronized. In this case calling code does not have a lock on the object(not synchronized) and will thus cause an Exception at runtime.
4:
1.10 What is the result of compiling and executing the following code?
public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
yield();
System.out.println("Leaving run");
}
public static void main(String args []) {
(new ThreadTest()).start();
}
}
A. The code fails to compile in the main() method.
B. The code fails to compile in the run() method.
C. Only the text "In run" will be displayed.
D. The text "In run" followed by "Leaving run" will be displayed.
E. The code compiles correctly, but nothing is displayed.
Answer: D
5:
1.12 Which of the following will definitely stop a thread from executing?A. wait()B. notify()C. yield()D. suspend()E. sleep()Answer: ACDE
6:
1.12 Which of the following will definitely stop a thread from executing?
A. wait()
B. notify()
C. yield()
D. suspend()
E. sleep()
Answer: ACDE
7:
Which of the following statements about threading are true?
A. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable.
B. You can obtain a mutually exclusive lock on any object.
C. You can't obtain a mutually exclusive lock on any object.
D. Thread scheduling algorithms are platform dependent.
Answer: BD
8:
Consider the following statement:
Thread myThread = new Thread();
Which of the following statements are true regarding myThread?
A. The thread myThread is now in a runnable state.
B. The thread myThread has a priority of 5.
C. On calling the start() method on myThread, the run method in the Thread class will be executed.
D. On calling the start() method on myThread, the run method in the calling class will be executed.
Answer: C
Note: the priority of myThread will be inherited from the Thread that called the constructor.
9:
What is the effect of issuing a wait() method on an object?(Mutiple)
1) If a notify() method has already been sent to that object then it has no effect
2) The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
3) An exception will be raised
4) The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.
ANSWER 1)
10:
Pick all the true statements below.
1) If a thread wants to call wait() on an object, the thread must own that object's lock.
2) There is a method that you can call on an instance of the Thread class that puts the instance to sleep for a specified number of milliseconds.
3) At the moment when a thread is notified, it automatically gets the lock of the object for which it was waiting.
ANSWER 1
11:
1. class Z {
2. public static void main(String[] args) {
3. new Z();
4. }
5.
6. Z() {
7. Z alias1 = this;
8. Z alias2 = this;
9. synchronized(alias1) {
10. try {
11. alias2.wait();
12. System.out.println("DONE WAITING");
13. }
14. catch (InterruptedException e) {
15. System.out.println("INTERRUPTED");
16. }
17. catch (Exception e) {
18. System.out.println("OTHER EXCEPTION");
19. }
20. finally {
21. System.out.println("FINALLY");
22. }
23. }
24. System.out.println("ALL DONE");
25. }
26. }
Mutiple:
1) Compiler error.
2) The application compiles but doesn't print anything.
3) The application prints "DONE WAITING".
4) The application prints "INTERRUPTED".
5) The application prints "OTHER EXCEPTION".
ANS: 2)
12:
What will happen when you attempt to compile and run the following code?
class MyThread extends Thread{
public void run(){
System.out.println("MyThread: run()");
}
public void start(){////注意,此方法重写后,不再有CALL RUN()方法的功能。
System.out.println("MyThread: start()");
}
}
class MyRunnable implements Runnable{
public void run(){
System.out.println("MyRunnable: run()");
}
public void start(){//Runnable 并无此方法,所以,除非本类的对象可以CALL 它,否则,将CALL THREAD 的start()执行run()
System.out.println("MyRunnable: start()");
}
}
public class MyTest {
public static void main(String args[]){
MyThread myThread = new MyThread();
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
myThread.start();
thread.start();
}
}
A.prints: MyThread: start() followed by MyRunnable: run()
B.prints: MyThread: run() followed by MyRunnable: start()
C.prints: MyThread: start() followed by MyRunnable: start()
D.prints: MyThread: run() followed by MyRunnable: run()
E.compile time error
F.None of the above
ANSWER:A
13:
Multiple objects of MyClass (given below) are used in a program that uses multiple Threads to create new integer count. What will happen when other threads use the following code? D
class MyClass{
static private int myCount = 0;
int yourNumber;
private static synchronized int nextCount(){
return ++myCount; //myCount为static
}
public void getYourNumber(){
yourNumber = nextCount();
}
}
A. the code ill give ompilation error
B. the code ill give runtime error
C. each thread will get a unique number
D. the uniqueness of the number different Threads can’t be guaranteed.
C
14:
What is the effect of issuing a wait() method on an object
Mutiple:
1) If a notify() method has already been sent to that object then it has no effect
2) The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
3) An exception will be raised
4) The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.
相关文章推荐
- SCJP考试中的 thread 题目分析
- 关于线程的总结--安全,协调,开销分析
- jstack(查看线程)、jmap(查看内存)和jstat(性能分析)命令
- 卷二 Dalvik与Android源码分析 第二章 进程与线程 2.2 Dalvik线程创建机制 图书版试读--请勿转发
- 一些常见笔试题目---堆、栈的区别,进程、线程等
- 线程高级应用-心得3-线程范围内的共享变量以及应用场景及面试题案例分析
- Java笔记3 多线程<2>线程间通信-代码分析以及多线程常见方法的运用
- java程序cpu占用率高,分析具体线程
- 【Java8源码分析】线程-ThreadLocal的全面剖析
- google 面试题目分析
- React 常用面试题目与分析
- java线程数过高原因分析
- 如何使用jstack分析线程状态
- 软考培训 - 挣值分析的几道题目
- 线程堆栈分析
- android中UI主线程与子线程深入分析
- Hadoop的Server及其线程模型分析
- Memcached源码分析(线程模型)
- Android系统Surface机制的SurfaceFlinger服务的线程模型分析
- linux下tomcat线程分析-thread dump