• 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

anilbachi question?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
this question is from http://www.anilbachi.8m.com mocks please can any one guess the solution?

class X extends object {
String str;
void setstr() {
for(int i=0;i<5;i++){
str=Integer.toString(i);
}
}
}
after setstr has been executed,how many String objects are eligible for garbage collection.
a).none
b).5
c).4
d).1
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dear

I think the answer is c ie. 4 .
the thing is like this String str is declared out side the function body. so when str is created by means of for loop ,earlier one created is getting garbage collected,this keep on happning till i= 4, after this for loop getsa terminated, and the only reference left is when the value of i is 4.
which is not garbage collected.
lokesh mahajan
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel it is 5 because, the number of iteration the for loop runs is 5... correct me if i am wrong;
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result of the integer.tostring(i) is stored in the same variable str, which is updated every time. So there is nothing for garbage collection.The answer should be none. Correct me if I am wrong.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
I believe the answer is technically: 4 are available for garbage collection. Why?
1. str = "0" inside loop
2. str = "1" inside loop
3. str = "2" inside loop
4. str = "3" inside loop
5. str = "4" inside loop
Since str still points to "4" that is not eligible for garbage collection. That would not be the case if str was local variable but it is an instance variable in this case.
Therefore from the line numbers above we can see that there are 4 String objects ready for garbage collection:
"0",
"1",
"2", and
"3".
Regards,
Manfred.
[This message has been edited by Manfred Leonhardt (edited February 16, 2001).]
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
Is there any code which we can develop by which we can count the numbers of String to be garbage collected. Manually speaking i think the answer is 5. Correct me if i am wrong
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Manfred is correct. There are 5 strings created, but the last one, "4", is still be referenced by the variable str. So it cannot be collected yet.
Bill
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With each iteration of for statement, a new object is
created and assigned to Str. When a new object is
assigned to Str, the previous object that was referenced
by the Str is not accessible by Str or by any object or
variable for that matter. Hence the object become
eligible for garbage collection.
In the above question, five interations indeed takes
places and breaks at that. Hence the objects are
garbage collected upto the penultimate object i.e 4
objects are garbage collected.
venkat
 
venkatesan Rajagopalan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i should admit I was late in replying and before which replies had already been posted.
venkat
[This message has been edited by venkatesan Rajagopalan (edited February 16, 2001).]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
this is a good question and I think theanswer is 5.
mohit.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is 4.
The answer will be 5 if str is a local variable of setstr method or a local variable of the for loop.
Or if the object reference to class X is eligible to be collected. (But this time, the answer may be 6)
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well man i think 4th one;
 
Waseem Akhtar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry man i thought that 4th one
have
answer 4.
i go for (c).. 4
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
How it can be four? please explain.Detail explanation will be much better.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class Xx extends Object {
String str;
void setstr() {
for(int i=0;i<5;i++){
str=Integer.toString(i);
}
}
public static void main(String args[]){
Xx obj = new Xx();
obj.setstr();
System.out.println(obj.str);//Here I am accessing the var and geting
//4 as output so it means that the last string has yet not gone for
//garbage collection
// so the answer is c.
}
}
//reply if I am wrong
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too think the answer is 4.
I remember I have seen the similar post before also and encountered the same question in mock exams also. The answer is 4 according to them.
 
malathi latha
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
thankyou for your responses, please a better explanation is anticipated here.
Thankyou.
 
mohit raju
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi wong,
yes the answer is 4
thankyou.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
can anyone explain when are objects eligible for garbage collection? In the above example if the objects are reassigned then if the older ones are eligible for garbage collection then the answer should be 4
Please correct my concept of garbage collection.
Can we know programatically of how many objects are eleigible for garbage collection???
Noel
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has been a hot topic in the past. Click here to follow a related link...
http://www.javaranch.com/ubb/Forum24/HTML/008155.html
and here Peter Tran show's how to solve this question programatically: http://www.javaranch.com/ubb/Forum24/HTML/007585.html
Joe

[This message has been edited by Joseph Russell (edited February 20, 2001).]
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic