Site Search:

OCPJP Simulation Test 36

<Back to OCPJP





Question: What's the output of the following program?


  1 import java.util.*;
  2 class Dog {public String toString(){return "dog";}}
  3 class Cat {public String toString(){return "cat";}}
  4 public class Cats {
  5   public static void main(String[] args) {
  6     List<Cat> cats = new ArrayList<>();
  7     cats.add(new Cat());
  8     cats.add(new Dog());
  9     catDisplay(cats);
 10   }
 11   private static void catDisplay(List objs) {
 12     for(Object obj: objs) {
 13       Cat cat = (Cat)obj;
 14       System.out.println(cat);
 15     }}}


Choice:
A. cat
B. dog
C. ClassCastException at run time
D. compiler error at line 7
E. compiler error at line 8
F. compiler error at line 9
G. compiler error at other lines