Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

gc

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the question from practice questions in Exan Cram.
Here is a method that creates a number of String objects in the
cource of printing the countdown
1 public void countDown()
2 {
3 for(int i=10;i>=0;i--){
4 String tmp=Integer.toString(i);
5 System.out.println(tmp);
6 }
7 System.out.println("BOOM");
8 }
When the program reaches line 7,how many of the String objects
in line 4 are eligible for garbage collection?(Assume the System.out does not keep a reference)
a. none
b. 1
c. 10
d. 11
Answer given is c(10).
They say d is incorrect because eventhough tmp is out of scope in line 7, the local var still has reference to String created.
=============
What I feel is ans should be 11 since tmp is out of scope at line 7 it is eligible for gc.
Why is it given wrong
Pl help me.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Girish ,
This qn has come up two or three times in this Forum.
Try a search on Garbage collection.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic