• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Which objs are eligible for GC?

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. class X {
2. static long story;
3. public static void main(String [] args) {
4. if(story==0) {
5. Long tale = 343L;
6. story = go(tale);
7. }
8. // do stuff
9. System.out.print(story);
10. }
11. static long go(Long t) { return t++; }
12. }


Which is true?


A) The output is 344.
B) Compilation fails due to an error at line 5 or Line 11.
C) At line 8, an object which tale refers tois eligible for garbage collection.
D) At Line 11, two objects eligible for garbage collection.
E) At Line 8 two objects eligible for garbage collection.

ANSWER C

Why isn't the answer A since there is a print statement?
Also, why not D since the method also uses Integer just as line 8?
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Firas Zureikat

Why isn't the answer A since there is a print statement?
Also, why not D since the method also uses Integer just as line 8?



At line--->>> return t++;

It increments it by 1. Again the value of the expression t++ is 343. So A is not correct.

Answer D is not correct since Two references are pointing to the same object. t is also pointing to the same tale Long object.

regards

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


Answer D is not correct since Two references are pointing to the same object. t is also pointing to the same tale Long object



"t++" will create a new instance of Long. After line 11, this one will eligible for garbage collection.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
When i happen to run the following code i got the output has

Intial value----> 0

Final Value ---->343




Let me know if this is what u need..

Thanks
Balaji.S
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all but how come the t in return t++ was assigned and then incremented. I though it gets incremented after the statement:

retun t++;

And then since we have another statement (the assignement statement) we are guaranteed it's now 344 making the answer A according to this (which is wrong but I don't know why).
[ June 13, 2006: Message edited by: Firas Zureikat ]
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PostIncrementOperatorAndAssignment in JavaRanch.


You can think "return t++;" roughly translates to


Long oldValue = t;
t = t + 1;
return oldValue;
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it...

It's just breaking that short return statement makes sense...The value is returned before it was really incremented.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic