• 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

prob in grabage collector....?

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
answer C and E are both correct
 
Shashank Sharma
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Shashank Sharma
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if some question ask for two individual answers will there be any points for one correct answers.is there any negative marking in paper?
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Can anyone explain about that option x5=x4,

Will that be eligible for Garbage collection?

I didnt get that...
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic