Java OCA OCP Practice Question 2706

Question

Which of these statements can fill in the blank so that the Main class compiles successfully?

(Choose all that apply.)

import java.util.*; 

public class Main { 
   public void showSize(List<?> list) { 
      System.out.println(list.size()); 
   } //ww w. j a  va  2s .  c  o  m
   public static void main(String[] args) { 
      Main card = new Main(); 
      ____________________________________ 
      card.showSize(list); 
   } 
} 
  • A. ArrayDeque<?> list = new ArrayDeque<String>();
  • B. ArrayList<? super Date> list = new ArrayList<Date>();
  • C. List<?> list = new ArrayList<?>();
  • D. List<Exception> list = new LinkedList<java.io.IOException>();
  • E. Vector<? extends Number> list = new Vector<Integer>();
  • F. None of the above


B, E.

Note

showSize() can take any type of List since it uses an unbounded wildcard.

Option A is incorrect because it is a Queue and not a List.

Option C is incorrect because the wild card is not allowed to be on the right side of an assignment.

Option D is incorrect because the generic types are not compatible.

Option B is correct because a lower-bounded wildcard allows that same type to be the generic.

Option E is correct because Integer is a subclass of Number.

Vector is an old type of List.

It isn't common in new code, but you still need to know it for the exam and in case you encounter old code.




PreviousNext

Related