Java OCA OCP Practice Question 3135

Question

Which statements will compile without errors and unchecked warnings when inserted at (1)?

public class Main<T> {
   public void test() {
     // (1) INSERT ASSIGNMENT HERE.
   }
}

Select the four correct answers.

(a)  T ref = new T();
(b)  T[] arrayRef = new T[10];
(c)  List<T>[] arrayOfLists0 = { new List<T>(), new List<T>() };
(d)  List<T>[] arrayOfLists1 = new List<T>[10];
(e)  List<?>[] arrayOfLists2 = new List<?>[10];
(f)  List   [] arrayOfLists3 = new List<?>[10];
(g)  List<?>[] arrayOfLists4 = new List[10];
(h)  List   [] arrayOfLists5 = new List[10];
(i)  List<String>[] arrayOfLists6 = new List[10];


(e), (f), (g), and (h)

Note

  • (a) Cannot instantiate a type parameter.
  • (b) Cannot create an array whose component type is a type parameter.
  • (c) Cannot create a generic array of List<T>, as List<T> is not reifiable type..
  • (d) Cannot create an array of a type parameter.
  • (i) Unchecked assignment conversion warning, as the assignment is from a non- generic type to a generic type.



PreviousNext

Related