Java OCA OCP Practice Question 425

Question

Given:

public class Main{
        public static void main(String[] args) {
        //from   w w w .j ava 2s .  c  om
          // INSERT DECLARATION HERE
          for (int i = 0; i <= 10; i++) {
            List<Integer> row = new ArrayList<Integer>();
            for (int j = 0; j <= 10; j++)
              row.add(i * j);
            table.add(row);
          }
          for (List<Integer> row : table)
            System.out.println(row);
        }
}

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>>();
  • B. List<List<Integer>> table = new ArrayList<List<Integer>>();
  • C. List<List<Integer>> table = new ArrayList<ArrayList<Integer>>();
  • D. List<List, Integer> table = new List<List, Integer>();
  • E. List<List, Integer> table = new ArrayList<List, Integer>();
  • F. List<List, Integer> table = new ArrayList<ArrayList, Integer>();
  • G. None of the above


B is correct.

Note

A is incorrect because List is an interface, so you can't say new List(), regardless of any generic types.

D, E, and F are incorrect because List only takes one type parameter.

A Map would take two, not a List.

C is tempting, but 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.




PreviousNext

Related