• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Regarding Finalize method

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)The finalize method is invoked by JVM atmost once in its life cycle.
2)if finalize method is overridden,JVM will definitely invoke it.
3)Exceptions thrown by finalize() is ignored by JVM.
4)Calling System.gc will definitely call finalize().
5)the finalize method may throw checked exceptions
Which is true? -- is the Question

I know that option 1 and 3 are correct.
i want to clarify one thing.
1)i know that finalize() can be overriden,but not sure that JVM will definitely invoke it.please confirm it.
2)the finalize method throws exception of type Throwable.any checked exception is a subclass of Exception, and Exception itself is a subclass of Throwable.i believe option 5 is also correct.tell me if i am wrong.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)The finalize method is invoked by JVM atmost once in its life cycle.
ANS: Yes.
2)if finalize method is overridden,JVM will definitely invoke it.
ANS: No, Its not necessary to override finalize() method in order to be called by GC.

3)Exceptions thrown by finalize() is ignored by JVM.
ANS: Yes. This is from Sun API:


If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.



4)Calling System.gc will definitely call finalize().
ANS: No, Not necessary. If an obejct is still being referenced by some objects, JVM will not execute finalize().

5)the finalize method may throw checked exceptions
ANS: Yes, the signature of finalize() says protected void finalize() throws Throwable. And since all exceptions and errors extend Throwable, finalize() can throw any kind of exception.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

yes, the Exception will be ignored.

prints "Watch out for Banana skins!"
but no Exception is thrown.


Point two: if finalize method is overridden,JVM will definitely invoke it.
No, it is not guarantied that finalize is executed, beeing overridden or not.


Yours,
Bu.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi B -

It's totally cool to post mock questions on this forum. However, when you post mock questions you should do two things:

1 - mention the source of the question
2 - copy the question EXACTLY!!!

A small change in the phrasing of a question can COMPLETELY CHANGE its meaning!!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic