This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another mock question for Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another mock question for Garbage Collection" Watch "Another mock question for Garbage Collection" New topic
Author

Another mock question for Garbage Collection

Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
From whizlabs,

Which of the following statements are true?
Select multiple options:


What's your answer for the questions?

Thank you.


Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
I think

A) The finalize method is invoked by JVM at most once in the life of an object.
--- True.. because the garbage collection might never happen during the run time of an application at all or might happen . In first case finalize() method is never called, in second case its called just once per object.So yes the method is invoked at most once.

(B) If the finalize method is overriden, the JVM will definitely invoke it.
True...finalize method is over ridden so that you can perform final operations like closing connections stuff, just before garbage collection removes objects from memory.

(C) Exceptions thrown by the finalize method are ignored by the garbage collector.
True...the gc process continues, only the code in the finalize might be skipped after exception.

(D) Calling System.gc() will definitely call the finalize() method.
false...System.gc() can just request garbage collection, not gurantee it, so we cant guarantee that finalize() will run

(E) The finalize method may throw checked exceptions.
False...the defination of finalize does not throw checked exceptions...so while overriding it you cannot declare it to throw any checked exceptions either as per the overriding rule.
[ April 24, 2007: Message edited by: megha joshi ]
Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
My Answer to it is A and
(A) True.
(B) If the finalize method is overriden, the JVM will definitely invoke it.
--False. Since JVM will not definitely invoke a finalize method.
(C)false, but I am not sure about this.
(D)false. Obvious.
(E)true. Since finalize() method in object throw Throwable. SO it can throw Exceptions.
sharan vasandani
Ranch Hand

Joined: Feb 22, 2007
Posts: 100
A,C,E are true,

By the way what is the score which you are getting in the whizlab mocks?
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hi,

I get why A and C are true.

But can you please elaborate on why E. is true?

Thanks!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
All is well but what goes wrong with this poor option!


(B) If the finalize method is overriden, the JVM will definitely invoke it.


Does anybody know?





Thanks,
cmbhatt


cmbhatt
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hi,

I think this option is wrong becuase of the word "definately".

We know if the finalize() method is overridden it would be invoked by the JVM if garbage collection is started. But just as garbage collection is not definate, calling the finalize() is not definate and thus calling the overridden version of finalize() is not definate.

I think if the option would be like,

If the finalize() method is overridden, than the JVM can/may invoke it...
it would be more True.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Are you sure Megha? Should I lock it?


Anyways good explanation!


Thanks,
cmbhatt
Tina Tibrewal
Ranch Hand

Joined: Apr 19, 2007
Posts: 40
Originally posted by megha joshi:
Hi,

I get why A and C are true.

But can you please elaborate on why E. is true?

Thanks!


This is because finalize in defined as
protected void finalize() throws Throwable.

And since it can throw throwable it can definitely throw Exception which is a subclass of Throwable.
sharan vasandani
Ranch Hand

Joined: Feb 22, 2007
Posts: 100
exactly
Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
I only get 73% of the practice 4, and I did not do the final mock yet. The first three practice, I get really low score.

So I think I may pass the exam with low score. I don't have too much time to prepare the exam.

Megha 's explain for C is reasonable.B is wrong because of "Definitely".

Thank you for all your reply.
[ April 25, 2007: Message edited by: Sam Sunamin ]
 
 
subject: Another mock question for Garbage Collection
 
Similar Threads
finalize() ..JLS
Garbage collection will be triggered and finalize method will be called ?
Garbage collection
Connection leak exception.
Regarding Finalize method