Java OCA OCP Practice Question 2338

Question

Which method calls can be inserted at (1) so that the program compiles without warnings?

public class Main {
  public static void main(String[] args) {
    List<Number> numList = new ArrayList<Number>();
    List<Integer> intList = new ArrayList<Integer>();
    // (1) INSERT CODE HERE
  }

  static <T> void move(List<? extends T> lst1, List<? super T> lst2) { }
}

Select the three correct answers.

(a)  Main.move(numList, intList);
(b)  Main.<Number>move(numList, intList);
(c)  Main.<Integer>move(numList, intList);
(d)  Main.move(intList, numList);
(e)  Main.<Number>move(intList, numList);
(f)  Main.<Integer>move(intList, numList);


(d), (e), and (f)

Note

(a) The arguments in the call are (List<Number>, List<Integer>).

No type inferred from the arguments satisfies the formal parameters (List<? extends T>, List<? super T>).

(b) The arguments in the call are (List<Number>, List<Integer>).

The actual type parameter is Number.

The arguments do not satisfy the formal parameters (List<? extends Number>, List<? super Number>).

List<Number> is a subtype of List<? extends Number>, but List<Integer> is not a subtype of List<? super Number>.

(c) The arguments in the call are (List<Number>, List<Integer>).

The actual type parameter is Integer.

The arguments do not satisfy the formal parameters (List<? extends Integer>, List<? super Integer>).

List<Number> is not a subtype of List<? extends Integer>, although List<Integer> is a subtype of List<? super Integer>.

(d) The arguments in the call are (List<Integer>, List<Number>).

The inferred type is Integer.

The arguments satisfy the formal parameters (List<? extends Integer>, List<? super Integer>).

(e) The arguments in the call are (List<Integer>, List<Number>).

The actual type parameter is Number.

The arguments satisfy the formal parameters (List<? extends Number>, List<? super Number>).

(f) Same reasoning as in (d), but the actual type parameter is explicitly specified in the method call.




PreviousNext

Related