Govt. Exams
Entrance Exams
Generic methods require type parameter declaration before return type. The collection parameter must be of type Collection<T> to extract element of type T.
java
List list = new ArrayList();
list.add("Java");
Object obj = list;
System.out.println(list.getClass() == obj.getClass());
Generic type information is erased at runtime. Both list and obj refer to the same ArrayList class, so getClass() returns the same class object.
Java does not allow creation of arrays with parameterized types due to type erasure. List<String>[] would be unsafe. You must use List<String>[] or List[] instead.
List<?> represents unknown type with read-only capabilities. Elements can only be read as Object. You cannot add elements (except null) due to type safety.
List list = Arrays.asList("a", "b", "c");
The compiler infers the most specific common type of the arguments. Since all arguments are String literals, T is inferred as String.
Type erasure removes all generic type parameters during compilation. For unbounded types, they're replaced with Object; for bounded types, they're replaced with the upper bound.
Valid generic class declarations allow multiple type parameters with upper bounds. Option A is syntactically correct. Option C uses invalid 'super' keyword in class declaration. Option D has malformed syntax.
Generics provide compile-time safety, code reusability through parameterization, and clearer API contracts. These are essential for modern Java development standards.
List list = new ArrayList();
list.add(10);
Object obj = list.get(0);
System.out.println(obj.getClass().getName());
Although obj is declared as Object, it holds an Integer instance. getClass() returns the actual runtime type, which is Integer.
This causes a compilation error due to type mismatch. List<Integer> cannot hold ArrayList<String>. Generics do not support implicit type conversion.