Java OCA OCP Practice Question 2992

Question

Given:

3. public class Main {  
4.   static Main bd;  
5.   public static void main(String[] args) {  
6.     new Main().doShape();  
7.     // do lots of memory intensive stuff   
...     // JVM finds an eligible Main object for GC  
...     // JVM invokes finalize()  
...     // do more stuff  
48.   }  /*ww w .j  a  v  a2  s.  c  om*/
49.   void doShape() {  }  
50.   // insert code here  
51.     bd = this;  
52.   }  
53. }  
54. class MyException extends Exception { } 

and the following four fragments:

  • I. protected void finalize() throws Throwable {
  • II. protected void finalize() {
  • III. protected void finalize() throws MyException {
  • IV. void finalize() {

If the fragments are inserted, independently, at line 50, which are true? (Choose all that apply.)

  • A. Fragment I compiles.
  • B. Fragment II compiles.
  • C. Fragment III compiles.
  • D. Fragment IV compiles.
  • E. Of those that compile, the GC will collect any given object after the JVM has called finalize() on that object.
  • F. Because of the way finalize() has been overridden, the GC will never collect eligible objects of type Main.


A, B, and C are correct

Note

A, B, and C are correct because Fragments I-III are legal signatures to override finalize().

D is incorrect because the access privilege is weaker.

E is incorrect because finalize() copies a reference of the object to the static variable bd.

F is incorrect.

For a given object, the JVM will never call finalize() again, but it might still GC it.




PreviousNext

Related