• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

OCAJP 8 study guide Chapter 1 Review questions 19 Have trouble understanding the answer D

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A. The Rabbit object from line 3 is first eligible for garbage collection immediately following line 6.
B. The Rabbit object from line 3 is first eligible for garbage collection immediately following line 8.
C. The Rabbit object from line 3 is first eligible for garbage collection immediately following line 12.
D. The Rabbit object from line 4 is first eligible for garbage collection immediately following line 9.
E. The Rabbit object from line 4 is first eligible for garbage collection immediately following line 11.
F. The Rabbit object from line 4 is first eligible for garbage collection immediately following line 12.


The right answer is BD. I understand B. But for D, why two is eligible on line 9 instead of line 11. One line 10, two is still be used.

Anyone can explain this please? Thanks!
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gem Wang wrote:The right answer is BD. I understand B. But for D, why two is eligible on line 9 instead of line 11. One line 10, two is still be used.

Anyone can explain this please?


The best way to solve this kind of questions (on the mock and actual exams) is to make a little drawing: reference variables on the left, objects on the right and you draw arrows between reference variables and objects based on the code snippet. If a reference variable is set to null, you delete/remove all outgoing arrows from that reference variable. All objects which don't have an arrow anymore are unreachable and thus eligible for GC. In this forum you'll find plenty of drawings to better understand the code snippets. Examples here, here and here. And you'll get a pencil and paper (or equivalent, e.g. erasable pen and small whiteboard) on the actual exam, so you can definitely use the same technique when it matters as well.

Please note the reference variable is completely different from the object it is referring to. So on line3 you'll have a reference variable one (of type Rabbit) and a new object (created by invoking the new operator: new Rabbit()). So options A, B, and C are about the object (created by the new operator), not the reference variable one. The same applies of course to line4: reference variable two and another new object (created by invoking the new operator: new Rabbit()). And options D, E, and F are about the object (created by the new operator), not the reference variable two.

Hope it helps!
Kind regards,
Roel

PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. Jeanne already added the code tags for you. See how much easier the code is to read?
 
Gem Wang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

Thank you so much for your explanation!

Gem
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I just need to clarify before I post an errata topic regarding this.
Choice B is not right, because not all references to the Rabbit object at line 3 are null after line 8.
If you notice, a new reference points to that object at line 7, which is never set to null until the end of the program.

Am I crazy or what?
 
Lemuel Dulfo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lemuel Dulfo wrote:Hi,

I just need to clarify before I post an errata topic regarding this.
Choice B is not right, because not all references to the Rabbit object at line 3 are null after line 8.
If you notice, a new reference points to that object at line 7, which is never set to null until the end of the program.

Am I crazy or what?



Oh okay, I think I get it now. In line 7, the variable four actually refers to no object, because of line 6. I think.
 
author & internet detective
Posts: 42135
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lemuel Dulfo wrote:Oh okay, I think I get it now. In line 7, the variable four actually refers to no object, because of line 6. I think.


Correct. Line 6 makes 1 a null reference so that is what gets copied to four.
 
reply
    Bookmark Topic Watch Topic
  • New Topic