Java OCA OCP Practice Question 1200

Question

Given a method declared as

public static <E extends Number> List<E> process(List<E> nums)

A programmer wants to use this method like this:

// INSERT DECLARATIONS HERE
output = process(input);

Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow the code to compile?

Choose all that apply.

A.   ArrayList<Integer> input = null;
     ArrayList<Integer> output = null;

B.   ArrayList<Integer> input = null;
     List<Integer> output = null;

C.   ArrayList<Integer> input = null;
     List<Number> output = null;

D.   List<Number> input = null;
     ArrayList<Integer> output = null;

E.   List<Number> input = null;
     List<Number> output = null;

F.   List<Integer> input = null;
     List<Integer> output = null;

G.   None of the above//from   w ww .j  a v  a  2  s. c o m


B, E, and F are correct.

Note

The return type of process is definitely declared as a List, not an ArrayList, so A and D are incorrect.

C is incorrect because the return type evaluates to List<Integer>, and that can't be assigned to a variable of type List<Number>.

Of course, all these would probably cause a NullPointerException since the variables are still null-but the question only asked us to get the code to compile.




PreviousNext

Related