• 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

que from javacamp

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is from javacamp mock exam.
public void countDown() {
2. for (int i = 10; i >= 0; i --) {
3. String tmp = Integer.toString(i);
4. System.out.println(tmp);
5. }
6. System.out.println("Boom!");
7. }
When the program reaches line 6, how many of the String objects created in line 3.
will have been garbage collected?(Assume that the System.out object is not keeping a reference.)
A. none
B. There is no way to tell
C. 9
D. 10
E. 11
I selected the option e but it was incorrect. Moreover the answers were not listed. what is the answer and why?
-Sanjana
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would guess B because we dont know if the GC ran in the first place. javacamp tricksed us, no javacamp is our friend suggestions anybody?
-sriram
 
Sriram Chintapalli
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and yes just wanted to add that the 11 objects were just ELIGIBLE for gc but we never know if they have been collected for sure. now that makes sense
-sriram
 
Ranch Hand
Posts: 56
  • 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 10.
The loop goes on creating the objects and assign it to tmp. When it creates a new object the reference of last object is lost. But for the last object(11). reference will be present hence that will not be gc'ed.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jayant Kulkarni:
I think the answer is 10.
The loop goes on creating the objects and assign it to tmp. When it creates a new object the reference of last object is lost. But for the last object(11). reference will be present hence that will not be gc'ed.


I disagree because the 'tmp' reference is defined inside the for-loop and is not available outside it.
The correct answer is B as Sriram suggested.
[ January 18, 2004: Message edited by: Vicken Karaoghlanian ]
 
Sriram Chintapalli
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for backing me vick hi jayanth I almost fell for 10 myself but realized "any variable declared before a for loop but assigned values in the loop is alive even after the loop but a variable that is both declared and assigned a value inside the for loop is dead after the loop". check this out:
public class forTest
{
public static void main(String[] args)
{
int k;
for(k=0;k<10;k++)
{
int j=k;
System.out.println("in:"+k);
System.out.println("j:"+j);
}
System.out.println("final:"+k);
System.out.println("j:"+j); //this gives a compiler error
}
}
-sriram
 
reply
    Bookmark Topic Watch Topic
  • New Topic