Site Search:

OCPJP Simulation Test 56

<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<A> list) {return list.size();}
  7   private static int countBoxes2(List<? extends A> list) { return list.size();}
  8   public static void main(String[] args) {
  9     List<B> b = new ArrayList<>();
 10     b.add(new B());
 11     List<C> cs = new ArrayList<>();
 12     cs.add(new C());
 13     List<A> a = new ArrayList<>();
 14     a.addAll(b);
 15     a.addAll(cs);
 16     System.out.print(countBoxes(a));
 17     System.out.print(countBoxes2(b));
 18     System.out.print(countBoxes2(cs));
 19     System.out.print(countBoxes2(a));

 20 }}

Choice:
A. 112112
B. 112
C. 2112
D. 11211
E. code throw exceptions at runtime
F. compiler error at line 7
G. compiler error at line 18
H. compiler error at line 19