• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java 2 Study guide with Kathy Sierra, Self Test question 14, chapter 7

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is about garbage collection. But as i c it, more than 2 answer can be given right (4 in my view).
IF I ANSWER (A) AND (B), THEN THE OBJECT I MAKE AT LINE 12 SHOULD BE ELIGIBLE FOR GARBAGE COLLECTION, DOESNT IT ?
-------------------------------------------------------------------
QUESTION 14) Given the following,
12. X3 x2 = new X3();
13. X3 x3 = new X3();
14. X3 x5 = x3;
15. x3 = x2;
16. X3 x4 = x3;
17. x2 = null;
18. // insert code
What two lines of code, inserted independently at line 18, will make an object eligible for garbage collection? (Choose two.)
A. x3 = null;
B. x4 = null;
C. x5 = null; <- right answer in the book
D. x3 = x4;
E. x5 = x4; <- right answer in the book
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Snowman.
Please read our name policy and adjust your displayed name accordingly.
Either y=x; or y=null; will make the object pointed to by the variable y eligible for g.c. only if y is the sole variable that refers to that object. As long as there are some alias(*), none of the assignments will make the object eligible for g.c.
Note that at line 18 both x3 and x4 are pointing the same object --due to x4=x3;--. Thus neither A, nor B are valid answers. But at line 18 x5 is the unique reference to the second X3 object --created by X3 x3 = new X3();--, thus either C, or D will make that object eligible for g.c.
(*)more variables pointing to the same object.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the keyword here is independently. If you answer A and B, then those 2 statements will be dependent on each other to make one object eligible for GC, i.e. you must put 2 statements at line 18, whereas the question is only asking for 1.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with alton... you should only insert one line of code to make the object eligible for GC.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key word is 'independently'
We are not too happy about the grammer of that question, but we put it in there because you WILL see that kind of grammer on the exam. When you see a question like that it means:
There are X different answers (usually two or three), each answer only uses one line of code, you don't get to use more than one line of code to create a unique, single answer.
Does that help?
-Bert
 
Shafkat Talli
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ive changed my name now, and thanx ppl, i c it now !
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic