为了准备scjp考试。做了100多题。有问题些,求解。谢谢!~(3)

fkys 2004-07-11 11:19:19
各位高手帮忙。多谢!~

问题一:

which is true?
A.If only one thread is blocked in the wait method of an object, and another
thread executes the notify method on that same object,then the first thread
immediately resumes executes.
B. If a thread is blocked in the wait method of an object, and another thread
executes the notify method on the same object,it is still possible that the
first thread might never resume execution
C.If a thread is blocked in the wait method of an object,and another thread
executes the notify method on the same object,then the first thread definitely
resumes execution as a direct and sole consequence of the notify call
D.If two threads are blocked in the wait method of one object,and another thread
executes the notify method on the same object,then the thread that executed
the wait call first definitely resumes execution as a direct and sole consequence
of the notify call
Answer:B
我的问题是:
使用notify方法怎么还有可能永远不能执行线程?
还有c和d在说什么?能否解释一下题干?

问题二:
1)public class X implements Runnable{
2)private int x;
3)private int y;
4)public static void main(String[] args){
5) X that =new X();
6) (new Thread(that)).start();
7) (new Thread(that)).start();
}
9) public synchronized void run(){
10) for(;;){
11) x++;
12) y++;
13) System.out.println("x="+x+",y="+y);
14) }
15) }
16) }
what is the result?
A.compile error at line 6
B.the program prints pairs of values for x and y that are
always the same on the same time
Answer:B
我的问题是:
我把代码第九synchronized去掉后。仍然是成对的出现x和y.
不知道是否对于这种情况加不加synchronized都成。

问题三:
class A implements Runnable{
int i;
public void run(){
try{
Thread.sleep(5000);
i=10;
}catch(InterruptException e){}
}
}
public static void main(String[] args){
try{
A a=new A();
Thread t=new Thread(a);
t.start();
17)
int j=a.i;
19)
}catch(Exception e){}
}
}
what be added at line line 17, ensure j=10 at line 19?
A. a.wait(); B. t.wait(); C. t.join(); D.t.yield();
E.t.notify(); F. a.notify(); G.t.interrupt();
Answer:C
我的问题是:
不是有i=10 的赋值么?
怎么还有运行t.join()方法?

问题四:
which two are true?
A. An anonymous inner class can be declared inside of a method
B. An anonymous inner class constructor can take arguments in some situations
C. An anonymous inner class that is a direct subclass of Object can implements
multiple interface
D. Even if a class Super does not implement any interfaces, it is still possible
to define an anonymous inner class that is an immediate subclass of Super that
implements a single interface
E. Even if a class Super does not implement any interfaces, it is still possible
to define an anonymous inner class that is an immediate subclass of Super that
implements multipe interface
Answer:A,D
我的问题是:b,c,d实在是不知所云.

问题五:
which two are true?
A. static inner class requires a static initializer
B. A static inner class requires an instance of the enclosing class
C. A static inner class has no reference to an instance of the enclosing class
D. A static inner class has accesss to the non-static member of the other class
E. static members of a static inner class can be referenced using the class
name of the static inner class
Answer:C,E

我的问题是:
b为什么错?静态内部类不是需要
 外部类的引用才能创建对象么?

问题六:
which two cannot directly cause a thread to stop executing?
A.calling the yield method
B.calling the wait method on an object
C.calling the notify method on an object
D.calling the notifyAll method on an object
E.calling the start method on another thread object
Answer:A,E
我的问题是:培训材料上说yield()方法当前线程主动放弃cpu.可是此题为什么还选中a.

问题七:
1)public class Foo implements Runnable{
2) public void run(Thread t){
System.out.println("Running");
}
public static void main(String[] args){
new Thread(new Foo()).start();
}
}
what is the result?
A.An Exception is thrown
B.The program exits without printing anything
C.An error at line 1 causes complication to fail
D.An error at line 2 causes complication to fail
E."Running" is pinted and the program exits
Answer:D
我的问题是:出错的原因是因为run方法没有参数列表么?

问题八:
which statement is true?
A. An anonymous inner class may be declared as final
B. An anonymous inner class can be declared as private
C. An anonymous inner class can implement mutiple interfaces
D. An anonymous inner class can access final variables in any enclosing scope
E. Construction of an instance of a static inner class requires an instance of
the encloing outer class
Answer:D
我的问题是:请大概a,b,c,d,e错误或者正确的理由.

问题九:
AnInterface is an interface
AnAdapter0 is a non-abstract,non-final class with a zero
argument constructer
AnAdapter1 is a non-abstract,non-final class without a zero
argument constructer,but with a constructer that takes one int argument
which two create an anoymous inner class?
A.AnAdapter0 aa=new AnAdapter0(){}
B.AnAdapter1 aa=new AnAdapter1(5){}
C.AnAdapter1 aa=new AnAdapter1(){}
D.AnAdapter0 aa=new AnAdapter0(5){}
E.AnInterface ai=new AnInterface(5){}
Answer:A,B
我的问题是:赋值号右边创建了匿名类对象,然后又把这个对象命名成赋值号左边的对象名.不知道对不对.
...全文
205 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fkys 2004-07-14
  • 打赏
  • 举报
回复
多谢各位!~
fkys 2004-07-12
  • 打赏
  • 举报
回复
多谢各位的支持.我把其它问题解答一下.(从其它途径得来的解释)

问题六:yeild方法是放弃cpu.但是如果这是无其它线程占用cpu.则仍然运行当前线程.所以yield方法可以理解为放弃优先权.

问题七:
run方法本身无参数.重写时也不应该有参数.

目前的问题只剩下:
问题九.
望高手帮忙.谢谢!~
fkys 2004-07-12
  • 打赏
  • 举报
回复
多谢各位的支持.我把其它问题解答一下.(从其它途径得来得解释)

问题六:yeild方法是放弃cpu.但是如果这是无其它线程cpu.则仍然运行当前线程.所以yield方法可以理解为放弃优先权.

问题七:
run方法本身无参数.重写时也不应该有参数.

目前的问题只剩下:
问题九.
望高手帮忙.谢谢!~

ecaol 2004-07-12
  • 打赏
  • 举报
回复
up
李玮顾 2004-07-12
  • 打赏
  • 举报
回复
问题8:
A. 不能被定义为final
B. 只能为public
C. 最多只能从单个接口继承
D. 可以访问final变量
E. 同问题5

rick_silver 2004-07-12
  • 打赏
  • 举报
回复
关于第9题我的看法:

没有那么复杂吧,题目说了,Adapter0是一个构造函数内不带参数的类,而Adapter1是一个构造函数中有一个整型参数的类,照题意来看,C,D在创建实例时的构造函数调用根本就不合法.
置于e,接口是不能用new来创建实例的.

fkys 2004-07-12
  • 打赏
  • 举报
回复
多谢liweigu(liweigu)嫩个解释一下第八题么?谢谢!~
e_Zkz 2004-07-12
  • 打赏
  • 举报
回复
第四题的答案应该是A、B吧。不然你举一个D的例子给我看一下。
e_Zkz 2004-07-12
  • 打赏
  • 举报
回复
第九题,我认为你的说法不准确。
匿名类可以说是继承了AnAdapter0或AnAdapter1,它是创建了一个匿名类,然后把匿名类upcasting(向上转换)成基类。肯定不是命名,不然叫什么匿名类。
李玮顾 2004-07-11
  • 打赏
  • 举报
回复
第4个问题我再查一下。
问题五:非静态内部类才需要通过实例引用,静态的直接通过类名就可以;
李玮顾 2004-07-11
  • 打赏
  • 举报
回复
问题一:线程的执行是不确定的,因为有可能别的线程优先运行;
问题二:不加synchronized,有可能结果会变化;
问题三:Thread.sleep(5000);表示等待5秒时间,如果没有join,在执行i=10; 之前,就先执行int j=a.i; 了;
问题四:匿名内部类不能执行带参数的构造函数,不能从多个接口继承;
zonelive 2004-07-11
  • 打赏
  • 举报
回复
路过~!

62,616

社区成员

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

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