分享
 
 
 

SCJP考试中的 thread 题目分析

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

SCJP考试中的线程题目分析

来源:www.chinajavaworld.net

1:

Which two CANNOT dir

ectly 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 metho

d on an InputStream object

E.Calling the SetPri

ority method on a Thread obj

ect

Answer:AC。同55题

2:

public class SyncTest{

public static void main(String[

] args){

final StringBuffer s1= new Stri

ngBuffer();

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 n

condition

on-deterministic point becau

se of a possible deadlock

E. The output is dep

is running on.

endent on the threading mode

l of the system the program

Answer:DE

3:

What will happen whe

n you attempt to compile and

run the following code?

public class Test{

int i = 0;

public static void main(Strin

g argv[]) {

Test t = new Test();

t.myMethod();

}

public void myMethod(){

while(true) {

try {

wait();

}catch (InterruptedException e

) {}

i++;

}

}

}

A. Compile time erro

r, no matching notify within

the method.

B. Compile and run b

ut an infinite looping of th

e while method.

C. Compilation and run without a

ny output.

E. Runtime Exception

"IllegalMonitorStatExceptio

n".

Answer: E

Note: The wait/notif

synchronized. In this ca

synchronized) and will t

y protocol can only be used

se calling code does not hav

hus cause an Exception at ru

within code that is

e a lock on the object(not

ntime.

4:

1.10 What is the result of compi

ling and executing the following code?

public class ThreadT

est 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() metho

d.

B. The code fails to

compile in the run() method

.

C. Only the text "In run" will b

e displayed.

D. The text "In run"

followed by "Leaving run" w

ill be displayed.

E. The code compiles correctly,

but nothing is displayed.

Answer: D

5:

1.12 Which of the following will

wait()B. notify()C. yield()D. suspen

definitely stop a thread from executing?A.

d()E. sleep()Answer: ACDE

6:

1.12 Which of the fo

llowing will definitely stop

a thread from executing?

A. wait()

B. notify()

C. yield()

D. suspend()

E. sleep()

Answer: ACDE

7:

Which of the follow

ing statements about threadi

ng are true?

A. You can only obtain a mutuall

extends Thread or implements runnabl

y exclusive lock on methods in a class that

e.

B. You can obtain a mutually exc

lusive lock on any object.

C. You can't obtain

a mutually exclusive lock on

any object.

D. Thread scheduling

algorithms are platform dep

endent.

Answer: BD

8:

Consider the following statement:

Thread myThread = new Thread();

Which of the followi

ng statements are true regar

ding myThread?

A. The thread myThre

ad is now in a runnable stat

e.

B. The thread myThre

ad has a priority of 5.

C. On calling the start() method

class will be executed.

on myThread, the run method in the Thread

D. On calling the st

class will be executed.

art() method on myThread, th

e run method in the calling

Answer: C

Note: the priority o

the constructor.

f myThread will be inherited

from the Thread that called

9:

What is the effect of issuing a

wait() method on an object?(Mutiple)

1) If a notify() met

effect

hod has already been sent to

that object then it has no

2) The object issuing the call t

a notify() or notifyAll() method

o wait() will halt until another object sends

3) An exception will be raised

4) The object issuin

with any other objects u

g the call to wait() will be

sing the receiving object.

automatically synchronized

ANSWER 1)

10:

Pick all the true statements below.

1) If a thread wants

object's lock.

to call wait() on an object

, the thread must own that

2) There is a method that you ca

that puts the instance to sleep for

n call on an instance of the Thread class

a specifiednumber of milliseconds.

3) At the moment whe

the object for which it

n a thread is notified, it a

was waiting.

utomatically gets the lock of

ANSWER 1

11:

1. class Z {

2. public static void main(Str

ing[] 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.o

ut.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 "INTER

RUPTED".

5) The application prints "OTHER

EXCEPTION".

ANS: 2)

12:

What will happen wh

en you attempt to compile an

d 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 THREAD 的start()执行run

并无此方法,所以,除非本类的对象可以CALL 它,

()

System.out.println

("MyRunnable: start()");

}

}

public class MyTest {

public static void main(String

args[]){

MyThread myThread=new MyThread();

MyRunnable myRunnable = new My

Runnable();

Thread thread=

new Thread(myRunnable);

myThread.start();

thread.start();

}

}

A.prints: MyThread: start() foll

owed by MyRunnable: run()

B.prints: MyThread:

run() followed by MyRunnable

: start()

C.prints: MyThread: start() foll

owed by MyRunnable: start()

D.prints: MyThread: run() follow

ed by MyRunnable: run()

E.compile time error

F.None of the above

ANSWER:A

13:

Multiple objects of

multiple Threads to crea

use the following code?

MyClass (given below) are us

te new integer count. What w

D

ed in a program that uses

ill happen when other threads

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 o

f issuing a wait() method on

an object

Mutiple:

1) If a notify() method has alre

effect

ady been sent to that object then it has no

2) The object issuin

a notify() or notifyAll(

g the call to wait() will ha

) method

lt until another object sends

3) An exception will be raised

4) The object issuing the call t

with any other objects using the rec

o wait() will be automatically synchronized

eiving object.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有