Consumer pattern uses '? super T' (lower bound). This allows adding elements safely. Option A (producer pattern) only allows reading, and Options C and D restrict type flexibility.
Cpublic static T create(T t) { return (T) new Object(); }
Dpublic T create() { return new T(); }
Correct Answer:
B. public static T create(Class clazz) throws Exception { return clazz.newInstance(); }
EXPLANATION
Cannot instantiate generic type T directly. Using Class<T>.newInstance() is a valid pattern (though deprecated in newer Java versions in favor of getConstructor()). Option A is invalid.
Correct Answer:
A. Producer Extends, Consumer Super
EXPLANATION
PECS (Producer Extends, Consumer Super) is a mnemonic for wildcard bounds: use '? extends' when reading (producing), use '? super' when writing (consuming).
Can you create an instance of generic type parameter directly like: T obj = new T();?
AYes, always possible
BNo, because type information is erased at runtime
CYes, only if T extends Serializable
DOnly with reflection
Correct Answer:
B. No, because type information is erased at runtime
EXPLANATION
Due to type erasure, T is replaced with Object at runtime, and you cannot instantiate Object with new T(). You need reflection or pass Class<T> as parameter.
BFirst ensures T is comparable with itself; second is a raw type
CFirst is deprecated in Java 8+
DSecond provides better type safety
Correct Answer:
B. First ensures T is comparable with itself; second is a raw type
EXPLANATION
<T extends Comparable<T>> is a recursive bound ensuring T implements Comparable of its own type. <T extends Comparable> uses raw type, losing type information.
Which generic declaration allows a method to accept List of any type?
Apublic void process(List rawList) { }
Bpublic void process(List list) { }
Cpublic void process(List list) { }
DAll of the above
Correct Answer:
D. All of the above
EXPLANATION
All three approaches allow processing lists of any type, though they differ: raw type (unsafe), unbounded wildcard (read-only), and type parameter (flexible).
What is the key difference between '? extends T' and '? super T' in practical usage?
AExtends is for reading data safely, Super is for writing data safely
BExtends is for writing, Super is for reading
CThey have no practical difference
DExtends is for classes, Super is for interfaces
Correct Answer:
A. Extends is for reading data safely, Super is for writing data safely
EXPLANATION
Upper bounds (? extends T) are safe for reading because the compiler knows it's at least T. Lower bounds (? super T) are safe for writing because you can pass T or any superclass.