Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shafkat Talli
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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.
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • 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.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agree with alton... you should only insert one line of code to make the object eligible for GC.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
ive changed my name now, and thanx ppl, i c it now !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic