Which of the following correctly declares a generic method that returns the first element of any collection?
Apublic T getFirst(Collection collection)
Bpublic T getFirst(Collection collection)
Cpublic T getFirst(Collection collection)
Dpublic Collection getFirst(T collection)
Correct Answer:
B. public T getFirst(Collection collection)
EXPLANATION
Generic methods require type parameter declaration before return type. The collection parameter must be of type Collection<T> to extract element of type T.
What will be the output of the following code?
java
List list = new ArrayList();
list.add("Java");
Object obj = list;
System.out.println(list.getClass() == obj.getClass());
Atrue
Bfalse
CCompilation error
DRuntime exception
Correct Answer:
A. true
EXPLANATION
Generic type information is erased at runtime. Both list and obj refer to the same ArrayList class, so getClass() returns the same class object.
What is the relationship between generics and arrays in Java?
AGeneric arrays can be created freely like List[]
BGeneric arrays are not supported; you cannot create arrays of parameterized types
CGeneric arrays work only with unbounded wildcards
DArrays of generics work only for primitive types
Correct Answer:
B. Generic arrays are not supported; you cannot create arrays of parameterized types
EXPLANATION
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.
BAny type, but with restricted capabilities - can only read as Object
CUnknown type that extends Object
DA placeholder for generic type parameter
Correct Answer:
B. Any type, but with restricted capabilities - can only read as Object
EXPLANATION
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.
What is the result of type erasure in Java generics?
AGeneric type information is preserved at runtime
BAll generic type information is removed at compile time, replaced with raw types or bounds
CGeneric types are converted to Object only
DType erasure only applies to wildcards
Correct Answer:
B. All generic type information is removed at compile time, replaced with raw types or bounds
EXPLANATION
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.
Which of the following is a valid generic class declaration in Java?
Apublic class Box { }
Bpublic class Box { }
Cpublic class Box { }
Dpublic class Box { }
Correct Answer:
A. public class Box { }
EXPLANATION
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.
What is the advantage of using generics over raw types in terms of 2024-25 Java standards?
ACompile-time type checking prevents ClassCastException
BEnables code reuse without sacrificing type safety
CImproves API clarity and documentation
DAll of the above
Correct Answer:
D. All of the above
EXPLANATION
Generics provide compile-time safety, code reusability through parameterization, and clearer API contracts. These are essential for modern Java development standards.