aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes que from javacamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "que from javacamp" Watch "que from javacamp" New topic
Author

que from javacamp

sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
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

Joined: Dec 16, 2003
Posts: 59
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

Joined: Dec 16, 2003
Posts: 59
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

Joined: Dec 02, 2003
Posts: 56
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

Joined: Jul 21, 2003
Posts: 522
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 ]

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
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
 
subject: que from javacamp