Java OCA OCP Practice Question 2528

Question

Which of the following statements can fill in the blank to make the code compile successfully? (Choose all that apply.)

Set<? extends RuntimeException> set = _________________________________ 
  • A. new HashSet<? extends RuntimeException>();
  • B. new HashSet<Exception>();
  • C. new TreeSet<RuntimeException>();
  • D. new TreeSet<NullPointerException>();
  • E. None of the above


C, D.

Note

Set defines an upper bound of type RuntimeException.

This means that classes may specify RuntimeException or any subclass of RuntimeException as the type parameter.

Choice A is incorrect because the wildcard cannot occur on the right side of the assignment.




PreviousNext

Related