| Author |
Dan's test question on Garbage collection
|
jay vmal
Greenhorn
Joined: Sep 14, 2004
Posts: 2
|
|
Hello all: I am wondering the answer to this question on dan's test: void m1() { Q q1 = null; for (int i = 0; i < 10; i++) { q1 = new Q(); // 1 m2(q1); // 2 } System.out.print("All done"); // 3 } When the processing of line 3 begins, how many objects of type Q that were created at line 1 have become eligible for garbage collection? a. 0 b. 1 c. 9 d. 10 e. Indeterminate. f. Compile-time error g. Run-time error h. None of the above --- Answer according to Dan's site is [e]. Having said that if you observe the question carefully & go nuts about the method 'm2(q1)' and wondering where in the world is the 'meat' for m2(), that made me select the option [F], 'F' like in flower...and after reading the answer, I am not sure if my thinking was normal or abnormal. Any suggestions are appreciated. In the morning I felt greatly motivated to prepare for SCJP. Having read the smallest topic (Garbage C), and after taking the test at dan's, I feel a bit confused ?? --thanks VJ
|
 |
Louie van Bommel
Ranch Hand
Joined: Aug 17, 2004
Posts: 76
|
|
Originally posted by jay vmal: if you observe the question carefully & go nuts about the method 'm2(q1)' and wondering where in the world is the 'meat' for m2(), that made me select the option [F], 'F' like in flower...and after reading the answer, I am not sure if my thinking was normal or abnormal.
m2() is not written. Its code isn't shown. Ah, the case of the old create an object and pass the object to a method that's not shown in the code. Who knows what that unwritten method could do to that object, it might *gulp* assign the object to another variable. Or worse yet, it might still be alive after the current class is garbage collected. Imagine this Here an object s1 is created in main(). The object s1 is passed to m2, and since it gets assigned to a variable, it doesn't get garbage collected after main() ends.
|
 |
jay vmal
Greenhorn
Joined: Sep 14, 2004
Posts: 2
|
|
Dear Louie van Bommel: Thanks for taking time to reply. What I was expecting was your answer for that question. As there is no meat for m2()...if you compile that code it should give a compilation error ? am I right.... -VJ
|
 |
Tom Tolman
Ranch Hand
Joined: Sep 02, 2004
Posts: 83
|
|
It seems like your real question here is: If I am given a chunk of code, and it seems to not be a complete program, should I assume there is another part of the program I can't see? And the answer is: On Dan's test, yes there is another part of the program you can not see. On the real test, I don't know if you should assume other sections of code do or do not exist.
|
 |
Louie van Bommel
Ranch Hand
Joined: Aug 17, 2004
Posts: 76
|
|
Originally posted by jay vmal: As there is no meat for m2()...if you compile that code it should give a compilation error ? am I right.... -VJ
Jay has a valid point. I'll wait for Dan to reply
|
 |
 |
|
|
subject: Dan's test question on Garbage collection
|
|
|