Question 213 / 297:  Given the following incomplete program, based on the given put statements in map, what is considered a correct declaration and instantiation for map ?
import java.util.*;

public class Test {

public static void main(String[] args) {

// insert code here

map.put(new ArrayList<Integer>(), 1);

map.put(new ArrayList<Integer>(), 12);

map.put(new LinkedList<Integer>(), new Integer(1));

map.put(new LinkedList<Integer>(), new Long(1));

}

}

A  1) Map<List<Integer>,? extends Number> map = new HashMap<List<Integer>,Number>();
B  2) Map<List<Integer>,Number> map = new HashMap<List<Integer>,Integer>();
C  3) Map<List<Integer>, Number> map = new HashMap<List<Integer>, Number>();
D  4) Map<List<Integer>,Integer> map = new HashMap<List<Integer>,Integer>();
E  5) Map<ArrayList<Integer>,Integer> map = new HashMap<ArrayList<Integer>,Integer>();
F  6) Map<List<Integer>,Long> map = new HashMap<List<Integer>,Long>();
<< First < Previous Next > Last >>
Explanation:

As we can see map accepts as key values ArrayList<Integer> and LinkedList<Integer>, therefore the correct data type for map keys is List<Integer>. (we exclude answer number 5)

Now moving to the second part of the map, the values, we see that map accepts values of type Integer and Long, therefore the type should cover these two types. Answers number 2,4,6 restrict the type to Integer, therefore they are excluded.

Answer 1, uses ? in declaration type, and when ever ? is used you cannot add to the collection (only adding null is allowed).

Which leaves us with answer 3 as a correct answer.

Exam Home Page
Ask
Donyea Sweets
Start Test
Yasser Ibrahim
Start Quiz
Brooke Delaney
Start Exam
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>