Which statements about garbage collection are true? [a] The garbage collector runs in low memory situations [b] You can run the garbage collector when ever you want. [c] When it runs it releases the memory allocated by an object. [d] Garbage collector immediately runs when you set the references to null. Ans: a,c I have a doubt regarding [d]. When an object reference is set to null, then the object becomes ELIGIBLE for garbage collection, but garbage collector MAY NOT run immediately. Am I right? Thanks, Cathy.
You are correct Kathy. There is no guarantee that the garbage collector will run, and that is the concept that the exam focuses on. I would argue that answer A is not always true, because such behavior is not guaranteed. For the purposes of the exam, I would say that it is false.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Another ambiguous question (I'm starting to dislike this book): [a] The garbage collector runs in low memory situations It runs whenever it wants. A VM implementation might decide to run each Sunday after breakfast - ideally, an implementation runs when memory is low or CPU activity is down, I would say this is not a correct answer. The keyword here being "ideally" - nothing is guaranteed. [b] You can run the garbage collector when ever you want. No you can't. The API provides both System.gc() and Runtime.gc() that "suggest" to the garbage collector that now is a good time to run. It is not gauranteed when or even if the garbage collector will honour your suggestion. I'd say this is incorrect. [c] When it runs it releases the memory allocated by an object. No it doesn't. It releases the memory allocated by an object "if there are no valid references to it". This is incorrect. [d] Garbage collector immediately runs when you set the references to null. Incorrect. Setting references to null "might" flag the object that it referred to as being eligible for garbage collection. I say "might" because there "might" be other references to it - if there aren't, then it "will" be flagged as eligible for garbage collection. Whether or not the garbage collector runs does not come into this scenario at all. Another incorrect option. Conclusion: There are no correct answers. I think it's important to be as accurate as possible about the wording of questions - in particular, offer them to an independant audience to request feedback; these type of ambiguities are immediately noticeable (at least they are to me anyway).
Cathy - What was the source of that question? (Jeez I hope I didn't write that one ) On the real exam, the GC questions are VERY 'trickily' worded, although I'd say they're worded better than this question. I don't think any of the answers are strictly correct, C *might* be but it's worded very ambiguously. I don't think you'll see ambiguous writing like this on the real exam.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
When I did the exam in December 2002, the questions were worded such that they were assessing your knowledge of the language - of course, some of these were tricky, however, I wouldn't say that any of them were "indeterminate".