100分问10道SCJP的题(都是基础题目),一道10分!

MaoZhua 2004-11-07 08:30:29
十道题,能作几道是几道!只要讲的有理,一道十分! -------------先谢谢大家了!

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。
问:A是什么意思?

2: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
问:为什么选第一个,3为什么不可以?

3:
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?

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.
问:这道题选什么,为什么?

4: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.
问:这道题选什么,为什么?

5:Which statement is true?
A. Assertions can be enabled or disabled on a class-by-class basis.
B. Conditional compilation is used to allow tested classes to run at full speed.
C. Assertions are appropriate for checking the validity of arguments in a method.
D. The programmer can choose to execute a return statement or to throw an exception if
an assertion fails.
Answer: A
问:为什么选A

6:QUESTION NO: 14
Given:
1. package test1;
2. public class Test1 {
3. static int x = 42;
4. }
1. package test2;
2. public class Test2 extends test1.Test1 {
3. public static void main(String[] args) {
4. System.out.println(“x = “ + x);
5. }
6. }
What is the result?
A. x = 0
B. x = 42
C. Compilation fails because of an error in line 2 of class Test2.
D. Compilation fails because of an error in line 3 of class Test1.
E. Compilation fails because of an error in line 4 of class Test2.
Answer: C
问:为什么编译错误,写了 test1.Test1 不是就可以省略import 吗?

7:Which statement is true about assertion in the Java programming language?
A. Assertion expressions should not contain side effects.
B. Assertion expression values can be any primitive type.
C. Assertion should be used for enforcing preconditions on public methods.
D. An AssertionError thrown as a result of a failed assertion should always be handled by
the enclosing method.
Answer: A
问:为什么选a?

8:QUESTION NO: 35
Given:
10. public Object m() {
11. Object o = new Float(3.14F);
12. Object [] oa = new Object[1];
13. oa[0] = o;
14. o = null;
15. return oa[0];
16. }
When is the Float object, created in line 11, eligible for garbage collection?
A. Just after line 13.
B. Just after line 14.
C. Never in this method.
D. Just after line 15 (that is, as the method returns).
Answer: B
问:偶决的应该选C啊为什么会是B?

9:隔离岛问题
1. class TestA {
2. TestB b;
3. TestA() {
4. b = new TestB(this);
5. }
6. }
7. class TestB {
8. TestA a;
9. TestB(TestA a) {
10. this.a = a;
11. }
12. }
13. class TestAll {
14. public static void main (String args[]) {
15. new TestAll().makeThings();
16. // ...code continues on
17. }
18. void makeThings() {
19. TestA test = new TestA();
20. }
21. }
Which two statements are true after line 15, before main completes? (Choose two)
A. Line 15 causes a stack overflow.
B. An exception is thrown at runtime.
C. The object referenced by a is eligible for garbage collection.
D. The object referenced by b is eligible for garbage collection.
E. The object referenced by a is not eligible for garbage collection.
F. The object referenced by b is not eligible for garbage collection.
Answer: C, F
问:这道据说是什么隔离岛问题?但不是说这种情况,两个对象都会被回收吗,那怎么还选F

最后道题:
Given:
1. public class Test {
2. public static void main(String[] args) {
3. int x = 0;
4. assert (x > 0): “assertion failed”;
5. System.out.println(“finished”);
6. }
7. }
What is the result?
A. finished
B. Compilation fails.
C. An AssertionError is thrown.
D. An AssertionError is thrown and finished is output.
Answer: A
问:x>0为false,程序不是就不能运行下去了呀?


谢谢各位!
...全文
259 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
MaoZhua 2004-11-12
  • 打赏
  • 举报
回复
呵呵,谢谢大家了!今天去考试,顺利通过,尽管分不高(78%)!现在节贴!
awers 2004-11-09
  • 打赏
  • 举报
回复
下午去考了,走前就再来复习下,嘿嘿
1.A,C,存活于同步块内的线程,释放于得到锁,不会影响其执行,同样notify方法是用来唤醒wait()中的线程

2.synchronized只保证wait中的对象会获得一个锁来保证其值不会被改变,而notify则不保证

3.他既然问了uses multiple Threads,所以我想这题应该用到synchroized,所以应该选C

4.wait()会一直等到notify/notifyAll来唤醒。而选项1说notify后没有作用??

5.Assertions 可以在类到类的基础上使用,前几天有过这样一个帖子,你可以找找.

6.不在同一包内的两个类,不可以直接继承.但我试过,即使在同一包内的不同子包内的两个类,也无法正常import,不知为什么

7.断言表达式不应该有执行效果,即assert ex1:ex2 这里的ex1的返回值是个boolean type,ex2不能是方法.当ex1 return false时,才会执行ex2!

8.这题就是C,别被迷惑!我就被迷惑了好几天!!TESTKING里这题出现过3次,给的答案也不一样,FT!!遇到垃圾回收题,自己画个图就明白多了~~

9.有类似的问题
http://www-900.ibm.com/developerWorks/cn/java/l-JavaMemoryLeak/index.shtml
http://community.csdn.net/Expert/topic/3488/3488589.xml?temp=.8718531

10.断言机制的默认状态是关闭的,除非开发者自己去command line下启用,否则不会影响到程序的正常编译及运行
GUN_JACK 2004-11-09
  • 打赏
  • 举报
回复
留位
xueqs 2004-11-08
  • 打赏
  • 举报
回复
mark
java_ak47 2004-11-08
  • 打赏
  • 举报
回复
up
prcgolf 2004-11-08
  • 打赏
  • 举报
回复
up
treeroot 2004-11-08
  • 打赏
  • 举报
回复
难道书上的就一定对,我是不明白existing from是什么意思,我不是说是你的笔误
MaoZhua 2004-11-08
  • 打赏
  • 举报
回复
声明:第一道题决不是笔误!
treeroot 2004-11-07
  • 打赏
  • 举报
回复
1.我觉得A是一个笔误,应该是Exiting from a ...
就是从一个同步块退出

2.wait()方法定义中就有说明,应该是竞争这个锁吧,而不是自动获得

3.这个题目也太祟了,怎么获得计数值呀,yourNumber不是public的
getYourNumber没有返回值!看到这种题就有点火.

4.我觉得应该选2, 1.一次notify只能有一个在wait的被唤醒,3.明显错误 4.要自己同步

5.断言一般用于调试吧

6.x没有声明为public或者protected,所以只能在同一个包可以访问

7.A是正确的,断言不应该有副作用,B.断言一般是布尔型.C没有这种说法D.不用捕捉

8.这个我觉得应该是C,应该返回值就是对这个对象的引用,怎么可以回收呢?

9.我觉得应该是C,F.其实没有对这两个对象的引用,但是a对象有对b的引用,所以应该
回收a后b才可以回收吧

10.这个是断言,只有为true的时候才抛出断言错误.
hvbird 2004-11-07
  • 打赏
  • 举报
回复
第一题,可能是一个同步线程执行完毕,另外一个现成获得锁,继续执行!
第二题,3不一定,要看拥有锁的对象是否执行完毕,释放出锁!
第三题,选D,getfYourNumber()也应该是synchronized,否则结果不确定。
以上见解仅供参考
MaoZhua 2004-11-07
  • 打赏
  • 举报
回复
自己up
windinring 2004-11-07
  • 打赏
  • 举报
回复
1.退出机锁区,因为退出机锁区,并不一定能使得线程停止。
2.选第一个是因为要用wait()必须要给线程机锁,第三个答案不对,因为被notify的线程不一定获得它所等待的线程的机锁,还是要等到那个线程退出机锁区。
3.选c,因为关键的计数部分只要被锁住,就能使得各个线程之间不冲突。
4.选2),4)不对是因为发出wait()命令的对象会锁住其他调用该对象的对象而不是调用wait()发到的目的对象,原因:wait() causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
5.选A,BCD是错的,因为Java的assertions特性用于一个特殊的目的:当用户定义的布尔表达式为False时它会抛出一个错误。更确切地说,一个assertion被当作一个发现某个值变成false的信号,它用来指示当前函数需要被中断。该性能被用在创建不该出现在真正产品部署过程中的专用调试模式上时非常有用。
6.不是那个原因,是你的x没有定义,如果你是要用继承下来的x,那你在声明的时候必须声明成protected的,而不是默认的friend
7.偶也不知道是什么意思
8.B.oa[0]引用o,当o = null时oa[0] = null
9.TestA test = new TestA();的原因
10.是错误,不是异常。
同样仅供参考

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧