Saturday, August 7, 2010

Lists: Tables and Rows

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 7, Question 1












Which statements could be inserted at // INSERT DECLARATION HERE to allow this code to compile and run? (Choose all that apply.)

A. List<List<Integer>> table = new List<List<Integer>>();

Incorrect because List is an interface, so you can’t say new List() regardless of any generic types

B. List<List<Integer>> table = new ArrayList<List<Integer>>();

Correct. The type argument <List<Integer>> must be the same for both sides of the assignment, even though the constructor new ArrayList() on the right side is a subtype of the declared type List on the left.

C. List<List<Integer>> table = 
                           new ArrayList<ArrayList<Integer>>();

Incorrect. The type argument <List<Integer>> must be the same for both sides of the assignment, even though the constructor new ArrayList() on the right side is a subtype of the declared type List on the left.

D. List<List, Integer> table = new List<List, Integer>();

Incorrect because List only takes one type parameter (a Map would take two, not a List).

E. List<List, Integer> table = new ArrayList<List, Integer>();

Incorrect because List only takes one type parameter (a Map would take two, not a List).

F. List<List, Integer> table = 
                           new ArrayList<ArrayList, Integer>();

Incorrect because List only takes one type parameter (a Map would take two, not a List).

G. None of the above

I love this choice ;-)





No comments:

Post a Comment