Site Search:

OCPJP Simulation Test 58

<Back to OCPJP





Question: What is the result of running the following program?

  1 import java.util.*;
  2 class A {}
  3 class B extends A {}
  4 class C extends A {}
  5 public class Boxes {
  6   private static int countBoxes(List<? extends A> list) { return list.size();}
  7   private static int countBoxes2(List<? super A> list) { return list.size();}
  8   private static int countBoxes3(List<?> list) { return list.size();}
  9   private static int countBoxes4(List<A> list) { return list.size();}
 10   public static void main(String[] args) {
 11     List<B> b = new ArrayList<>();
 12     b.add(new B());
 13     List<C> cs = new ArrayList<>();
 14     cs.add(new C());
 15     List<A> a = new ArrayList<>();
 16     a.addAll(b);
 17     a.addAll(cs);
 18     System.out.print(countBoxes(b));
 19     System.out.print(countBoxes(a));
 20     System.out.print(countBoxes2(a));
 21     System.out.print(countBoxes3(a));
 22     System.out.print(countBoxes3(b));
 23     System.out.print(countBoxes4(a));
 24 }}

Choice:
A. 121121
B. 122212
C. 211121
D. 133313
E. code throw exceptions at runtime
F. compiler error at line 7
G. compiler error at line 18
H. compiler error at other lines