Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes few doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "few doubts" Watch "few doubts" New topic
Author

few doubts

nishesh chouhan
Ranch Hand

Joined: Nov 06, 2000
Posts: 37
1)Which of the following are true about garbage collection?

a] we can set the object references to null to ensure that garbage collection will run perfectly
b] Local variables are candidate for garbage collection when the method returns (finished).
c] To use finalize() you must use a try-catch block or rethrow the error object.
d] The JVM will never run out of memory
e] Garbage collection mechanism cannot be forced

ans is ace but e garbage colection can be forced by calling System.gc()
10) 8.When is the object referenced s1 is available for garbage collection?

1.class gc{
2.public static void main(String args[]){
3.String s1="hello";
4.String s2=s1;
5.s1=null
6.s1=null
7.}
8.}
ans given is b but jane said that GC is for only heap variable is that
when objecct is created using new ( am i right??)
now if that is right then none shud be the answer but I am just confused with this
GC .here S1 and S2 are in literal pool so gc does not occur
but when method exits then gc takes place or not ps explain me all that
stuff
11)
10.Which of the following are true?
a] Any class that includes a finalize method is not necessary to invoke
super class�s finalize() method
b] Java uses mark sweep garbage collection algorithm
c] It is guaranteed that only objects with no references will be garbage collected
d] you can suggest when garbage collection will happen



ans given is b, c but for me a also seems to be right

.Which of the following main method in java application is correct?

a] public static main(String args[])
b] static public void main(String []a)
c] private static void main(String args[])
d] public void static main(String args[])
e] public static void main(String)
f] static void main(String args[])


the ans given is b,c,f
but what is the problem with e ??? I think that toois right
13)
now he ans given is c but I think a is ALSO correct
tell me if i am wrong
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi
Please find my remarks to your querry:

1)Which of the following are true about garbage collection?
a] we can set the object references to null to ensure that garbage collection will run perfectly.
True as this will speed-up the "gc" process as the JVM will realise that since the refernces are set to "null" it can treat the object eligible for gc.
b] Local variables are candidate for garbage collection when the method returns (finished).
False not neccessarily in case the locally created variables may return the refernce back to the callee method so in such a situation this variables object will not be gc'ed.
c] To use finalize() you must use a try-catch block or rethrow the error object.
True, finalize() method throws a checked exception (of Throwable type) so it must be caught or declared to be thrown in the callee method.
d] The JVM will never run out of memory
False, ofcourse it can run-out of memory, JVM does not
gurantee that it has perpetual supply of memory.
e] Garbage collection mechanism cannot be forced
True , as even by a call to System.gc() it is not
neccessary that JVM will definitely run the garbage collection mechanism.

10. Which of the following are true?
a] Any class that includes a finalize method is not necessary to invoke super class�s finalize() method.
False Please note that finalize() method is a protected method of Object. So when you are overriding it , you must call the super's (i.e, Object class) finalize otherwise you can land up in serious memory issues.
b] Java uses mark sweep garbage collection algorithm
True
c] It is guaranteed that only objects with no references will be garbage collected.
True, JVM does this check before gc'ing an object.
d] you can suggest when garbage collection will happen
False, you can not predict when the garbage collection will happen , you can only speed-up or make object's eligible for garbage collection
Q Which of the following main method in java application is correct?

a] public static main(String args[])
False, return type is "missing".
b] static public void main(String []a)
True, order of static and public doesnt matter.
c] private static void main(String args[])
False, ofcourse the code runs . THIS a
BUG in the JVM.
d] public void static main(String args[])
False, return type can't be "static".
e] public static void main(String)
False, no "args[]" so invalid.
f] static void main(String args[])
True, public access modifier is missing in favour of default access modifier. From SCJP point of view, this is wrong.

Hope this clears your querry.
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 15, 2001).]
devesh singh
Greenhorn

Joined: May 08, 2001
Posts: 9
hi ravindra ,
you say that public static void main(String ) method sinot valid
but we can have overloaded method those were the forms of stndard main() . now what to do whenthis type of ques comes in exam what is the answer to the above question
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Devesh,

hi ravindra ,
you say that public static void main(String ) method sinot valid
but we can have overloaded method those were the forms of stndard main() . now what to do whenthis type of ques comes in exam what is the answer to the above question

In the SCJP exam, you have answer that I have provided in earlier posting. As far your specific querry is concerned,
The <code> public static void main(String ) </code>
is not valid as there is a missing "variable" name after the "String" in the <code> main() </code>. You can overload the <code> main() </code> method, BUT the entry
point to the class would always be the one with declaration type as <code> public static void main(String[] args) </code>.
Hope your doubts are cleared now.
Ravindra Mohan.
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
ravindra,
can you explain to me about finalize method little detailed?
why is it necessary to call super.finalize() ??
i am not getting a clear picture from jcl.please help!!!
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Jeena,
Let me clear your doubt here. Your doubt has nothing to do with
the current post. But anyway, let me clear your querry in here.
Finalise is a protected method
of the Object class and it can throw Throwable, normally you dont need to override it. But, in case you do want to do some optimizations/ clearing up activities before you object is gc'ed , then in the overriden method YOU must give a chance to the super classes object to be cleared up or else you can land up some serious issues. It is for that reason that you make an EXPLICITE call to super.finalize() method as the first statement of the overriden finalise method.
Hope you are clear now.
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 16, 2001).]
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
hi ravindra,
thanks for reply.
i have one more doubt.
"it is guaranteed that finalize method will be called on all objects before being garbage collected"
is the above statement true?
i read in some discussion that it is not guranteed.
mousami bhattacharya
Ranch Hand

Joined: May 13, 2001
Posts: 40
Hi Ravindra,I am having some doubts regarding your answers.

10. Which of the following are true?
a] Any class that includes a finalize method is not necessary to invoke super class�s finalize() method.
False Please note that finalize() method is a protected method of Object. So when you are overriding it , you must call the super's (i.e, Object class) finalize otherwise you can land up in serious memory issues.
/************* I think this should be true ,coz' though its a good practice to call super.finalize(),but it is not at all necessary ,and there wont be any errors thrown.************/
b] Java uses mark sweep garbage collection algorithm
True
/********* I think this should be false ,the java specification says that the GC algorithm is very much JVM implementation dependent,java doesn't specifically specify any GC alogorithm,a JVM implementor can use any GC algo***************/
Do correct me if I am wrong
M
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: few doubts
 
Similar Threads
can agrbage collection be forced???
4 quest diffulties please help
Inheritence
Questions abt Garbage collection
GC question