Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Garbage collection?

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The answer is given by K & B is C and E. My answer is A and B
Because the two objects references X3 and X4 references the object in line 12.
I am making null for those references for garbage collection. So what is wrong with my answer pls explain to me to understand the concept.

Thanks, Raghu.K



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;
452 Chapter 7: Objects and Collections
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;
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice what happens after each line executes.

X3 x2 = new X3();

x2 -> O1, an object

13. X3 x3 = new X3();

x3 -> O2, an object

14. X3 x5 = x3;

x2 -> O1, x3,x5 -> O2

15. x3 = x2;

x2,x3 -> O1, x5 -> O2

16. X3 x4 = x3;

x2,x3,x4 -> O1, x5 -> O2

17. x2 = null;

x3,x4 -> O1, x5 -> O2

Now if we look at the options.

A. x3 = null, x4 still points to O1 and x5 still points to O2
B. x4 = null, x3 still points to O1 and x5 still points to O2
C. x5 = null, x3 and x4 point to O1, but O2 is eligible for collection
D. X3 = x4, x3 and x4 still point to O1, and x5 still points to O2
E. x5 = x4, x3,x4, and x5 point to O1, but O2 is eligible for collection
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome Keith, I got it.

Thanks, Raghu.K
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic