• 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

Strings and GC

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is the earliest line in the following code after which the object created on the line marked(0) will be a
candidate for being garbage collected, assuming no compiler optimization are done?
public class Q76a9 {
static String f() {
String a = new String("hello");
String b= new String("bye"); //0
String c = b + "!"; //1
String d = b;
b = a; //2
d = a; //3
return c; //4
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); //5
}
}
A)The line marked(1)
B)The line marked(2)
C)The line marked(3) - -----true
D)The line marked(4)
E)The line marked(5)
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should it be after line 2 is executed, just before line 3?
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer should be on line marked with 4 since d has a reference on "bye". On line (marked 3), d is assigned to a,
So it is after line (marked 3), the "bye" is eligible for garbage collection.
 
Trailboss
Posts: 23773
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is C.
I think you guys aren't getting much response to this because this question was thoroughly discussed just a few days before you posted this.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jerson- You are correct in stating it's eligible after line 3. Note that the question says "Which is the earliest line in the following code after which the object [will be GC'd]". That means if you answer 4, it means "after line 4", which is incorrect. Even though 4 is after 3, the "after" part is already built into the question, so they're looking for the answer 3.
 
Oh the stink of it! Smell my tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic