那位高手帮忙看看这几道题的答案

edisonyh 2004-06-14 03:10:01
最好能解释一下!

You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be access in nature order, which interace provides that capability?
A. java.uil.Map
B.java.util.Set
C.java.util.List
D.java.util.SortedSet
E.java.util.SortedMap
F.java.util.Collection
B还是D?


Which statement is true for the class java.util.HashSet?
A. The elements in the collection are ordered.
B. The collection is guaranteed to be immutable.
C. The elements in the collection are guaranteed to be unique.
D. The elements in the collection are accessed using a unique key.
E. The elements in the collections are guaranteed to be synchronized.


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.printIn(“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.


Given:
11. try {
12. int x = 0;
13. int y = 5 / x;
14. } catch (Exception e) {
15. System.out.println(“Exception”);
16. } catch (ArithmeticException ae) {
17. System.out.println(“Arithmetic Exception”);
18. }
19. System.out.println(“finished”);
What is the result?
A. finished
B. Exception
C. Compilation fails.
D. Arithmetic Exception


Which statement is true?
A. The Error class is a runtimeException.
B. No exceptions are subclasses of Error.
C. Any statement that may throw an Error must be enclosed in a try block.
D. Any statement that may throw an Exception must be enclosed in a try block.
E. Any statement that may thro a runtimeException must be enclosed in a try
block.



Given:
11. public class Test {
12. public void foo() {
13. assert false;
14. assert false;
15. }
16. public void bar(){
17. while(true){
18. assert false;
19. }
20. assert false;
21. }
22. }
What causes compilation to fail?
A. Line 13
B. Line 14
C. Line 18
D. Line 20


Given:
1. class Bar { }
1. class Test {
2. Bar doBar() {
3. Bar b = new Bar();
4. return b;
5. }
6. public static void main (String args[]) {
7. Test t = new Test();
8. Bar newBar = t.doBar();
9. System.out.printIn(“newBar”);
10. newBar = new Bar();
11. System.out.printIn(“finishing”);
12. }
13. }
At what point is the Bar object, created on line 3, eligible for garbage collection?
A. After line 8.
B. After line 10.
C. After line 4, when doBar() completes.
D. After line 11, when main() completes.


Given:
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.


Given:
1. class A {
2. }
3. class Alpha {
4. private A myA = new A();
5.
6. void dolt( A a ) {
7. a = null;
8. }
9. void tryIt() {
10. dolt( myA );
11. }
12. }
Which two statements are correct? (Choose two.)
A. There are no instanced of A that will become eligible for garbage collection.
B. Explicitly setting myA to null marks that instance to be eligible for garbage collection.
C. Any call on tryIt() causes the private instance of A to be marked for garbage
collection.
D. Private instances of A become eligible for garbage collection when instances of
Alpha become eligible for garbage collection.
...全文
247 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
wqqissc 2004-06-15
  • 打赏
  • 举报
回复
题实在太多了,简单的说一下
1 which interace provides that capability 选择D no duplicat->Set nature sorted->SortedSet
2 Which statement is true for the class java.util.HashSet?
我说不清楚其他答案,但是有HashSet的性质,D。UniqueKey访问肯定是对的,如果是多选,请自己查一下
3 What is the result?
选B X=42 因为X是父类的公共类变量,子类很自然的继承过来了。静态变量可以当作普通的变量使用,所以直接输出是可以的
4 What is the result?
选C 编译错误。因为当捕获了Exception后,其后面所有的子类就被屏蔽,不能再捕获了,所以编译器会认为后面的Catch子句无效,从而报错
6 第五题第一个assert,第13行就出错,因为assert后应该是一个表达式,不可以是常量。这道题我不太熟悉,请另外的人发表意见
7 选B,在第10行后,可以回收。因为第三行doBar方法创建的对象,只是被第八行的newBar变量引用。(注意,方法体内的b变量虽然也引用该对象,但b是局部变量,方法掉用结束后b就释放了),所以在第10行的newBar指向另一对象后,doBar方法创建的变量就没有人引用了,所以可回收
8 选CD,都可以回收,只是一个孤岛现象,具体解答我在认证栏目的java考试版里做过了,请查阅帖子题目“我觉得这条关于垃圾回收的问题好困难耶~”
9 选AD,当前A的实例不可回收,因为私有实例变量myA依然指向它。注意,doIt方法里只是将局部变量a设为null,并不影响myA,java里是按值传递。
myA是Alpha的类变量,生命周期同Alpha类的实例,所以当该实例可以回收后,myA变量释放,从而这个A实例就可以回收了
HawaiiLeo 2004-06-15
  • 打赏
  • 举报
回复
1. class A {
2. }
3. class Alpha {
4. private A myA = new A();
5.
6. void dolt( A a ) {
7. a = null;
8. }
9. void tryIt() {
10. dolt( myA );
11. }
12. }
Which two statements are correct? (Choose two.)
A. There are no instanced of A that will become eligible for garbage collection.
B. Explicitly setting myA to null marks that instance to be eligible for garbage collection.
C. Any call on tryIt() causes the private instance of A to be marked for garbage
collection.
D. Private instances of A become eligible for garbage collection when instances of
Alpha become eligible for garbage collection.
Answer:BD
C肯定是错的,按照sun官方的说法:当一个引用变量作为参数传递给一个方法时, 在这个方法内可以改变变量的值,即改变引用指向的对象,但是方法的调用结束后,改变量恢复原来的值,即变量仍然指向原来的对象。
wuyuestar 2004-06-15
  • 打赏
  • 举报
回复
Given:
11. public class Test {
12. public void foo() {
13. assert false;
14. assert false;
15. }
16. public void bar(){
17. while(true){
18. assert false;
19. }
20. assert false;
21. }
22. }
What causes compilation to fail?
A. Line 13
B. Line 14
C. Line 18
D. Line 20

Answer: B
Line 14不可能走到......
HawaiiLeo 2004-06-15
  • 打赏
  • 举报
回复
Given:
1. class Bar { }
1. class Test {
2. Bar doBar() {
3. Bar b = new Bar();
4. return b;
5. }
6. public static void main (String args[]) {
7. Test t = new Test();
8. Bar newBar = t.doBar();
9. System.out.printIn(“newBar”);
10. newBar = new Bar();
11. System.out.printIn(“finishing”);
12. }
13. }
At what point is the Bar object, created on line 3, eligible for garbage collection?
A. After line 8.
B. After line 10.
C. After line 4, when doBar() completes.
D. After line 11, when main() completes.
Answer:B
理由:我认为在这行,指向第三行创建的Bar 的对象的引用已经消失,可以进行垃圾收集。
wuyuestar 2004-06-15
  • 打赏
  • 举报
回复
Which statement is true?
A. The Error class is a runtimeException.
B. No exceptions are subclasses of Error.
C. Any statement that may throw an Error must be enclosed in a try block.
D. Any statement that may throw an Exception must be enclosed in a try block.
E. Any statement that may thro a runtimeException must be enclosed in a try
block.

Answer: E
wuyuestar 2004-06-15
  • 打赏
  • 举报
回复
Given:
11. try {
12. int x = 0;
13. int y = 5 / x;
14. } catch (Exception e) {
15. System.out.println(“Exception”);
16. } catch (ArithmeticException ae) {
17. System.out.println(“Arithmetic Exception”);
18. }
19. System.out.println(“finished”);
What is the result?
A. finished
B. Exception
C. Compilation fails.
D. Arithmetic Exception

Answer: B
Exception在ArithmeticException前面,Exception包含的集合最大,应该在这里就被catch住了......

HawaiiLeo 2004-06-15
  • 打赏
  • 举报
回复
Which statement is true?
A. The Error class is a runtimeException.
B. No exceptions are subclasses of Error.
C. Any statement that may throw an Error must be enclosed in a try block.
D. Any statement that may throw an Exception must be enclosed in a try block.
E. Any statement that may throw a runtimeException must be enclosed in a try
block.
Answer:D
HawaiiLeo 2004-06-15
  • 打赏
  • 举报
回复
11. try {
12. int x = 0;
13. int y = 5 / x;
14. } catch (Exception e) {
15. System.out.println(“Exception”);
16. } catch (ArithmeticException ae) {
17. System.out.println(“Arithmetic Exception”);
18. }
19. System.out.println(“finished”);
What is the result?
A. finished
B. Exception
C. Compilation fails.
D. Arithmetic Exception
Answer: C
Fault:exception java.lang.ArithmeticException has already been caught
wuyuestar 2004-06-15
  • 打赏
  • 举报
回复
test1 test2:
D
Because static variable is not used by classed but objects.

D没错吧,在class里面定义全局的静态变量没问题吧....应该是E
HawaiiLeo 2004-06-15
  • 打赏
  • 举报
回复
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.printIn(“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: E
Fault:x is not public in test1.Test1; cannot be accessed from outside package
HawaiiLeo 2004-06-15
  • 打赏
  • 举报
回复
Which statement is true for the class java.util.HashSet?
A. The elements in the collection are ordered.
B. The collection is guaranteed to be immutable.
C. The elements in the collection are guaranteed to be unique.
D. The elements in the collection are accessed using a unique key.
E. The elements in the collections are guaranteed to be synchronized.
Answer: C
HawaiiLeo 2004-06-15
  • 打赏
  • 举报
回复
You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be access in nature order, which interace provides that capability?
A. java.uil.Map
B.java.util.Set
C.java.util.List
D.java.util.SortedSet
E.java.util.SortedMap
F.java.util.Collection
Answer: B
set为最佳答案。
Jinkan_zhong 2004-06-15
  • 打赏
  • 举报
回复
学习中,但是我顶!!
edisonyh 2004-06-14
  • 打赏
  • 举报
回复
shuneng 2004-06-14
  • 打赏
  • 举报
回复
test1 test2:
D
Because static variable is not used by classed but objects.
shuneng 2004-06-14
  • 打赏
  • 举报
回复
HashSet D
shuneng 2004-06-14
  • 打赏
  • 举报
回复
D SortedSet会保证它的遍历是按natural ordering的
edisonyh 2004-06-14
  • 打赏
  • 举报
回复
自己顶一下

62,614

社区成员

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

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