Java OCA OCP Practice Question 558

Question

What is true about the following code? (Choose all that apply)

public class Main { 
     protected void finalize() { 
         System.out.println("Roar!"); 
     } //from  ww w  .  ja v a  2 s.co  m

     public static void main(String[] args) { 
       Main m = new Main(); 
       m = null; 
       System.gc(); 
     } 
}
      
  • A. finalize() is guaranteed to be called.
  • B. finalize() might or might not be called
  • C. finalize() is guaranteed not to be called.
  • D. Garbage collection is guaranteed to run.
  • E. Garbage collection might or might not run.
  • F. Garbage collection is guaranteed not to run.
  • G. The code does not compile.


B, E.

Note

Calling System.gc() suggests that Java might wish to run the garbage collector.

Java is free to ignore the request, making option E correct.

finalize() runs if an object attempts to be garbage collected, making option B correct.




PreviousNext

Related