Java OCA OCP Practice Question 1224

Question

Given:

 3. import java.util.*;
 4. class Creature { }
 5. class Animal extends Creature { }
 6. class Pet extends Animal { }
 7. public class Main {
 8.   ArrayList<Animal> go() {
 9.     // insert code here
10.   }
11. }

Which statement inserted independently at line 9 will compile?

Choose all that apply.

  • A. return new ArrayList<Pet>();
  • B. return new ArrayList<Animal>();
  • C. return new ArrayList<Object>();
  • D. return new ArrayList<Creature>();


B is correct.

Note

A is incorrect because polymorphic assignments don't apply to generic type parameters.

C and D are incorrect because they don't follow basic polymorphism rules.




PreviousNext

Related