aspose file tools*
The moose likes Mock Exam Errata and the fly likes Mock Quesion on Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Mock Quesion on Garbage Collection " Watch "Mock Quesion on Garbage Collection " New topic
Author

Mock Quesion on Garbage Collection

Mukti Bajaj
Ranch Hand

Joined: Dec 11, 2000
Posts: 59
Hi,
Question 13: There are a number of labels in the source code below. These are labeled a through j. Which label identifies the earliest point where, after that line has executed, the object referred to by the variable first may be garbage collected?
class Riddle {
public static void main(String[] args) {
String first, second;
String riddle;
if (args.length < 2)
return; a: first = new String(args[0]);
b: second = new String(args[1]);
c: riddle = "When is a " + first;
d: first = null;
e: riddle += " like a " + second + "?";
f: second = null;
g: System.out.println(riddle);
h: args[0] = null;
i: args[1] = null;
j:
}
}
Select the one right answer.
1)d:
2)e:
3)h:
4)i:
5)j:
Lot of confusion about Garbage collection.....Can someone explain.
Thanks.
Mukti
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
An object is eligble for garbage collection as soon as there are no more references to that object. Since they are talking about the object that first is references, the String args[0], when will that object no longer have any references to it?
Take a look at it and tell me what you think.
Bill
Mukti Bajaj
Ranch Hand

Joined: Dec 11, 2000
Posts: 59
Hi Bill,
If the reference to a object is set to null....the object is still there in the memory.And will be set to null when the value is set to null...as in this case...args[0] = null;
Sorry, for the ambiguous explanation.
Can you please let me know if, I am on the right track.
Anyway, Thanks
Mukti
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
If there are no references anymore, then there is no way you can get to the object. So you are correct, once args[0] and the variable first is set to null, you can no longer see the object, so it is eligble for garbage collection. This one is a bit trickier because it is easy to think that once first is set to null after line "d" that the object would be eligble for garbage collection, but that is not true because you can still see the object by looking at args[0]. So it is not until after line "h" that the object can no longer be seen.
Bill
Lori Battey
Ranch Hand

Joined: May 17, 2000
Posts: 37
What mock is this from?? Looks like one I'd like to take!!

Lori Battey<br />SCJP2
Harry Chou
Greenhorn

Joined: Jan 31, 2001
Posts: 4
according to Java API
String(String value)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
Doesn't it means the new String(arg[0]) has created a new String object?
I think the answer is still d:
Neeraj Thakkar
Ranch Hand

Joined: Dec 06, 2000
Posts: 80
Hi Mukti,
I think the correct answer is D.
The reason:
First of all an object is ready to be garbage collected once it is not referenced by any variable.
But here we are deling with Strings. Do u know the concept of STRING POOL.
A string pool is maintained by java which is checked whenever u create an object of type String. So if the value for which u r creating the String object exists in the String pool then a new object is not created instead the variable refers to the object which already exists.
e.g.
String str="hello";
String str1="hello";
here both these variable of string type refer to the same object in memory.
Here if I say
str1=null;
then the object "hello" is not available for garbage collection b'coz it is still referenced by str;
BUT YOU CAN CHANGE THIS BEHAVIOR USING new.
one u create a object using new then the String Pool is not checked and a new object is created.
e.g.
String str="hello";
String str1=new String("hello");
here both str and str1 store the same value but are seperate objects because 'new' forces the creation of new object.
PROBLEM ABOVE
in above problem first is created using 'new' so a seperate object is created for first and it is different from args[0] so when u say
first=null;
then the object is available for garbage collection.
So the correct answer is D.
This is my opinion.
In case I am wrong do inform me I shall be thankful
Neeraj Thakkar
neeraj-thakkar@usa.net


Neeraj Thakkar<br />"Try to get what you like,Else you will be forced to like what you get."
Nasser Aboobaker
Ranch Hand

Joined: Dec 06, 2000
Posts: 104
Hi folks,
Dont you think at line d: first become null(Its handler become null), but at line c: riddle is still pointing to the same address of first. So though, we make first as null the object which has the id is still referenced by riddle at line c:
So till riddle points to a different object at line e: the string object created is not eligible for GC. I believe the correct answer is after line e:.
Correct me if I am wrong.
Thanks.
Nasser Aboobaker
nasser@cnnsimail.com
[This message has been edited by Nasser Aboobaker (edited March 23, 2001).]
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hi all,
I think d) is right, because
c: riddle = "When is a " + first;
does not point to the object first, but to a newly created String object.
Up to what I think its like riddle = new String ("When is a " + first);
correct me if I am wrong
Axel
Nasser Aboobaker
Ranch Hand

Joined: Dec 06, 2000
Posts: 104
Hi again,
I believe all first is having an objects address. Let us say if Object is at memory address 15000, all first is having that address. So if the object at memory address 15000 is type of String and having a value of "Hello", first is pointing to that object.
When you say anotherfirst = first then it means anotherfirst is also pointing to the same address as first (15000). anotherfirst does not get a value of "Hello". All it gets is the copy of the address which is pointing to the object "Hello".
So when you say riddle = "another string" + first, actually we are saying riddle = "another string" + (the object at memory 15000)
So the string object "Hello" is referenced by riddle tooo. (That is what I believe)
If I am wrong please correct me.
Nasser
[This message has been edited by Nasser Aboobaker (edited March 23, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock Quesion on Garbage Collection
 
Similar Threads
GC - tricky question
Garbage Collection
garbage collection
Garbage collection
Garbage Collection