Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

que from javacamp

 
sanjana narayanan
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Sriram Chintapalli
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jayant Kulkarni
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic