垃圾回收经典问题(转)

fxfeiyi 2006-08-04 03:46:33
Confusing Garbage Collection SCJP Exam Question

2. Given:

class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
}
}

When // doStuff is reached, how many objects are eligible for GC?

A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

-------------------------------------------------------
> The object originally pointed to by c1 is eligible,
> because you have no reference to it. You still have
> a reference to the object pointed to by c2, because
> Java is pass by value, so in "go", the parameter is
> set to null, not c2. c3 never gets an object--it is
> set to null immediately, because null is the return
> value of c1.go(c2).
...全文
241 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fxfeiyi 2006-08-16
  • 打赏
  • 举报
回复
not good,just pass scjp 77%
zhmt 2006-08-08
  • 打赏
  • 举报
回复
sf,mark
fxfeiyi 2006-08-05
  • 打赏
  • 举报
回复
The class AssertTest defines a method methodA(int i) as given below. Which of the

following declarations of methodA(int i) are valid in the subclass of AssertTest?
(Assume that the code is compiled and run with assertions enabled)

public class AssertTest
{
public void methodA(int i) throws AssertionError
{
assert i < 1024 : "Invalid Value";
}
}


A:public void methodA(int i)
D:public void methodA(int i) throws Error


A and D are correct. Both are the valid declarations of methodA(int i). Note that,

AssertionError is a subclass of Error, and it is not mandatory to declare it in the

throws clause of the method. Option A obviously will compile without any error. Also,

since it is not mandatory to declare Error or its subclass in the throws clause of the

method, any method can throw it;irrespective of what its super class implementation

throws. This is similar to RuntimeException. Hence option D is correct.

Exception is the super class of all Exceptions and subclass method can't throw it if

its super class counterpart does not throw it. Hence, option B is incorrect.

It applies to Throwable as well, which is the super class of both - Exception and

Error. Hence, option C is also incorrect.
fxfeiyi 2006-08-04
  • 打赏
  • 举报
回复
我不知道标准的答案是哪个?究竟是B吗,好象有人说是C。
kevinliuu 2006-08-04
  • 打赏
  • 举报
回复
SCJP的题
fxfeiyi 2006-08-04
  • 打赏
  • 举报
回复
c1------------->c1_object

c2------------->c2_object

c2(copy)------->null

c3------------->null

c1------------->null

c1_object is eligibled.

23,404

社区成员

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

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