Topic: 這一題Garbage collection 的問題 我不明白為甚麼 c 是對的

  Print this page

1.這一題Garbage collection 的問題 我不明白為甚麼 c 是對的 Copy to clipboard
Posted by: kenny_yan
Posted on: 2004-06-17 20:14

1. public class TestA {
2. TestB b;
3. TestA() {
4 b = new TestB(this);
5 }
6 }
7public class TestB {
8 TestA a;
9 TestB(TestA a) {
10 this.a = a;
11 }
12}
13public class TestAll {
14 public static void main(String[] args) {
15 new TestAll().makeThings();
16 }
17
18 void makeThings() {
19 TestA test = new TestA();
20 }
21}
which two statement are true after line 15, before main complete(choose two)
a. Line 15 will casuse a stack flow
b. Exception throws at run time.
c. The object referebced by a is eligible for garbage collection
d. The object referebced by b is eligible for garbage collection
e. The object referebced by a is not eligible for garbage collection
f. The object referebced by b is not eligible for garbage collection
Answer C,F

2.Re:這一題Garbage collection 的問題 我不明白為甚麼 c 是對的 [Re: kenny_yan] Copy to clipboard
Posted by: ringayumi
Posted on: 2004-06-17 21:04

kenny_yan wrote:
1. public class TestA {
2. TestB b;
3. TestA() {
4 b = new TestB(this);
5 }
6 }
7public class TestB {
8 TestA a;
9 TestB(TestA a) {
10 this.a = a;
11 }
12}
13public class TestAll {
14 public static void main(String[] args) {
15 new TestAll().makeThings();
16 }
17
18 void makeThings() {
19 TestA test = new TestA();
20 }
21}
which two statement are true after line 15, before main complete(choose two)
a. Line 15 will casuse a stack flow
b. Exception throws at run time.
c. The object referebced by a is eligible for garbage collection
d. The object referebced by b is eligible for garbage collection
e. The object referebced by a is not eligible for garbage collection
f. The object referebced by b is not eligible for garbage collection
Answer C,F


You may pay attention the line9 and line10!!

9 TestB(TestA a) {
10 this.a = a;

line9 : TestB(TestA a) <== "a" , It's a local variable! so , when the construcotor has completed , a is eligible for garbage collection.

3.Re:這一題Garbage collection 的問題 我不明白為甚麼 c 是對的 [Re: kenny_yan] Copy to clipboard
Posted by: dingligang
Posted on: 2004-06-21 10:15

I think it's an amphibolic question.

after line 15, before main complete, both a and b are eligible for garbage collection, but a is earlier than b, right?

4.Re:這一題Garbage collection 的問題 我不明白為甚麼 c 是對的 [Re: kenny_yan] Copy to clipboard
Posted by: zming
Posted on: 2004-06-28 16:34

the answer is incorrect.i think the correct answer is c and d.
it is a classic "refering island ".


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923