Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training
SCJP Online Training » Discussions

Garbage collection exercise solution

Hi


I am stuck in GC questions. I have trouble solving 3rd and 5th questions (assuming private and static members as public/default members in the exercise questions). In the 3rd question, I am getting number of objects eligible for GC as zero but the answer key says one. And in the 5th question I am getting result as 2 as specified in the answer key but I read in the discussion that it must be 3 (http://www.examlab.org/t/6194238928715776/question-5). Can you please help me resolve this?

Since you have access to our course, I want to ask if you have followed through the lesson on garbage collection?


If yes, can you please roughly explain how you came up with your answers? I will then try to provide you a sketchy illustration on where you might have gotten it wrong.

Hi Frederick


Yes I have followed the GC lesson.


Below are the image links

https://drive.google.com/file/d/0B8xTbRn-d9N9RXh2R3JVemt6eUU/view?usp=sharing

https://drive.google.com/file/d/0B8xTbRn-d9N9Wm96NUd5RC1yQVk/view?usp=sharing


And now I have got 3 as answer for 5th question. Please verify the steps and approach for 3 and 5. Let me know know where I am going wrong.

The problem with your diagram is you seem to have missed the static keyword. As I'm sure you know, when we have a static variable, it has only single existence. In other words, when we say b1.s, b2.s, or B.s, we always refer the same thing. So, it needs to be drawn as a separate spot instead of as a part of each object.


Take a look at this:


As for the second one (question #5), your diagram is correct! It just you didn't count on the objects properly. I have added some highlights to the number of objects that are eligible for GC on your diagram. Please take a look and let me know if this helps:


ExamLab © 2008 - 2024