Java OCA OCP Practice Question 284

Question

Which of the following is the output of this code, assuming it runs to completion?

package store; //from w ww  . j  a va2 s .  c o  m
public class Toy { 
        public void play() { 
           System.out.print("play-"); 
        } 
        public void finalizer() { 
           System.out.print("clean-"); 
        } 
        public static void main(String[] fun) { 
           Toy car = new Toy(); 
           car.play(); 
           System.gc(); 
           Toy doll = new Toy(); 
           doll.play(); 
        } 
} 
  • A. play-
  • B. play-play-
  • C. play-clean-play-
  • D. play-play-clean-clean-


B.

Note

If there was a finalize() method, this would be a different story.

However, the method here is finalizer().

That's just a normal method that doesn't get called automatically.

Therefore clean is never output.




PreviousNext

Related