Java OCA OCP Practice Question 1442

Question

What is the output of this code?.

public class Car { 
   public void startup() { 
      System.out.print("computer-"); 
   } /* w w w.  j ava2 s.  c  om*/
   public static void main(String[] args) { 
      Car computer = new MyCar(); 
      MyCar laptop = new MyCar(); 
      computer.startup(); 
      laptop.startup(); 
   } 
} 
class MyCar extends Car { 
    public void startup() { 
        System.out.print("laptop-"); 
    } 
} 
  • A. computer-laptop-
  • B. laptop-computer-
  • C. laptop-laptop-
  • D. None of the above


C.

Note

Both objects are instances of the class MyCar.

The startup() method in the MyCar class gets called both times thanks to polymorphism.




PreviousNext

Related