File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What's the correct answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What Watch "What New topic

What's the correct answer

Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
Select all valid answers.

a. You can directly free the memory allocated by an object.
b. You can directly run the garbage collector whenever you want to and it will be schedule in due time..
c. The garbage collector informs your object when it is about to be garbage collected.
d. The garbage collector reclaims an object´┐Żs memory as soon as it becomes a candidate for garbage collection.
e. The garbage collector runs in low-memory situations.
I think e is correct. The answer is e b and c. Could anybody explain b and c to me. Thanks.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
The answer b also appaears wrong to me. But I also doubt e. C is true because the garbage collector informs the object and you can check this by having a finalize method which will be run when the object is to be garbaged collected. B appears wrong to me because by calling System.gc() there is no gaurantee that the GC would run then or even would be able to run afterwards(Scheduling). The answer E is generally what may happen but as GC is unpredictable and may be differently implemented by different vendors so you just can't be sure.
As for explaining the options : I guess that in option B by scheduling they mean a call to System.gc() which would either run GC then or later. The above para explains what C means.
Where did you find the question?
[ May 23, 2003: Message edited by: Anupam Sinha ]
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
I mostly agree with Anupam's assessment of this question. This one's tough, because the wording of the answers given are not precise enough; the ones on the real exam are more careful.
For example, even 'C' is not necessarily correct as worded -- because it is not always true!
What you need to know for the exam is that an object will *not* always be informed when it is about to be collected. While finalize() *is* guaranteed to be called before an object is collected, it will be called only ONCE -- the *first* time the object is about to be collected.
But if the object is resurrected *within* the finalize method (by, for example, passing a reference to that about-to-be-collected object out to some other live object), and if the object then LATER becomes eligible for gc again, next time around the garbage collector will NOT invoke finalize(). In other words, the garbage collector will invoke finalize() only ONCE in an object's life, regardless of how many times it becomes eligible.
So it is entirely possible that an object becomes eligible, and the garbage collector checks a list to see, "Ah-ha, I see that I have already called finalize() on this one... so this time I'll skip finalize() and just reclaim the memory." And in that case, the object would NOT be informed even though it is about to be collected, and so answer 'C' would be incorrect.
I actually do not believe *any* of these answers are correct as written. They would need to be worded more precisely to make them valid.

Co-Author of <a href="" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
Sunder Ganapathy
Ranch Hand

Joined: Apr 01, 2003
Posts: 120
The consummate ease with which Kathy clears the doubts with crisp communications, she really performs the role of a GURU, which means Dispeller of Darkness . Too good.
I agree. Here's the link:
subject: What's the correct answer
Similar Threads
Garbage Collection
Garbage Collection Question
a question about GC
Reg. GC