Java OCA OCP Practice Question 1474

Question

What does the following print?

1:   class SmartCar extends Car { 
2:      private String getType() { return "smart watch"; } 
3:      public String getName(String suffix) { 
4:         return getType() + suffix; 
5:      } //w ww  .j a  v  a2  s . c om
6:   } 
7:   public class Car { 
8:      private String getType() { return "watch"; } 
9:      public String getName(String suffix) { 
10:        return getType() + suffix; 
11:     } 
12:     public static void main(String[] args) { 
13:        Car watch = new Car(); 
14:        SmartCar smartCar = new SmartCar(); 
15:        System.out.print(watch.getName(",")); 
16:        System.out.print(smartCar.getName("")); 
17:     } 
18:  } 
  • A. smart watch,watch
  • B. watch,smart watch
  • C. watch,watch
  • D. None of the above


B.

Note

Line 15 calls the method on line 9 since it is a Car object.

That returns watch, mak- ing Option A incorrect.

Line 16 calls the method on line 3 since it is a SmartCar object and the method is properly overridden.

That returns smart watch, so Option B is the answer, and Option C is incorrect.




PreviousNext

Related