aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes prob in grabage collector....? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "prob in grabage collector....?" Watch "prob in grabage collector....?" New topic
Author

prob in grabage collector....?

Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
This is a prob from Tata mC graw Hill scjp & scjd Java 2 self guide pg452
12. X3 x2 = new X3(); //obj1
13. X3 x3 = new X3(); //obj2
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;
D. x3 = x4;
E. x5 = x4;
//answer given in book--->>.E and C

I agree with answer C but whats the use of assigning x4 to x5 in E.as C is sufficient to remove the last ref to obj 2.inserting statement E in the code would add one more reference var to obj 1.


please help


---------------
Shashank
Sanket Meghani
Greenhorn

Joined: Jan 15, 2007
Posts: 14
You are correct. Only answer C is suffecient.

i.e inserting x5 = null will remove last reference to obj2.

Simliarly, if we insert x5 = x4 (i.e option E), it will also remove last reference to obj2 and hence making obj2 eligible for garbage collection.

So here the question is "what two lines of code, inserted independently at line 18, will make an object eligible for
garbage collection?"

So it is asking for two options which can be used independant of each other to make an object eligible for garbage collection.

Answer is you can use either of oprion C or E.

HTH,
Sanket
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

answer C and E are both correct


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
thanks
Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
if some question ask for two individual answers will there be any points for one correct answers.is there any negative marking in paper?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
If a question has multiple correct answers you must get all of them correct or you get no credit. In other words there is no such thing as partial credit on a question.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
karthik damodaran
Greenhorn

Joined: Jun 24, 2008
Posts: 3
Hi

Can anyone explain about that option x5=x4,

Will that be eligible for Garbage collection?

I didnt get that...


Karthikeyan.D
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by karthik damodaran:
Can anyone explain about that option x5=x4,

Will that be eligible for Garbage collection?


Yes, by the statement



what actually gets executed is, "the object being pointed by the reference variable x4 is now being assigned to the reference variable x5".

As such, the side effects are:

  • The object being pointed by x4 now has two references - x4 and x5 (newly added). Remember, still x4 is pointing to the same object until it is reassigned with some other object.
  • The previous object being pointed by x5 prior to the execution of this assignment statement has lost one of its active references (x5). Had x5 been the only reference to that object, now it is eligible for GC.


  • Hope this helps!


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: prob in grabage collector....?