Site Search:

OCPJP Simulation Test 54

<Back to OCPJP





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

  1 import java.util.*;
  2 interface A {}
  3 class B implements A {}
  4 class C implements 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 
 14     List<A> a = new ArrayList<>();
 15     a.addAll(b);
 16     a.addAll(cs);
 17 
 18     System.out.print(countBoxes(b));
 19     System.out.print(countBoxes(cs));
 20     System.out.print(countBoxes(a));
 21 
 22     System.out.print(countBoxes2(b));
 23     System.out.print(countBoxes2(cs));
 24     System.out.print(countBoxes2(a));
 25 }}

Choice:
A. 112112
B. 112
C. 1
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