OCA Java SE 8 Building Blocks - OCA Mock Question Building Block 16








Question

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

     public class Main { 
        protected void finalize() { 
          System.out.println("Roar!"); 
        } 
        public static void main(String[] args) { 
           Main bear = new Main(); 
           bear = null; 
           System.gc(); 
        } 
     } 
  1. finalize() is guaranteed to be called.
  2. finalize() might or might not be called
  3. finalize() is guaranteed not to be called.
  4. Garbage collection is guaranteed to run.
  5. Garbage collection might or might not run.
  6. Garbage collection is guaranteed not to run.
  7. The code does not compile.




Answer



B, E.

Note

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

Java virtual machine can ignore the request.

finalize() runs if an object attempts to be garbage collected.