Java OCA OCP Practice Question 1004

Question

What is the output of the following code?

1: class Animal { 
2:   public Animal(int age) { 
3:     System.out.print("Animal"); 
4:   } //from w w w.  ja  v a 2  s. c  om
5: } 
6: public class Pet extends Animal { 
7:   public Pet() { 
8:     System.out.print("Pet"); 
9:   } 
10:   public static void main(String[] args) { 
11:     new Animal(5); 
12:   } 
13: } 
A.  Pet 
B.  Animal 
C.  PetAnimal 
D.  AnimalPet 
E.  The code will not compile because of line 8. 
F.  The code will not compile because of line 11. 


E.

Note

The code will not compile because the parent class Animal doesn't define a no-argument constructor, so the first line of a Pet constructor should be an explicit call to super(int age).

If there was such a call, then the output would be AnimalPet, since the super constructor is executed before the child constructor.




PreviousNext

Related