What is the correct way to declare a generic method that accepts a list and returns the maximum element?
Apublic T getMax(List list) { ... }
Bpublic static T getMax(List
Cpublic
Dpublic T getMax(List list) { ... }
Correct Answer:
D. public T getMax(List list) { ... }
EXPLANATION
Option D correctly ensures T is comparable with itself using F-bounded polymorphism. Option C has inverted logic with '? super'. Option B is unnecessarily complex.
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.
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.
Consider:
public class Pair { }
Which declaration is invalid?
APair pair = new Pair();
BPair pair = new Pair();
CPair pair = new Pair();
DPair
Correct Answer:
D. Pair
EXPLANATION
Option D is invalid because we cannot instantiate a generic class with wildcard type parameters. Wildcards are only for variable declarations, not for instantiation with 'new'.
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.
What is the output of this code?
List strings = new ArrayList();
List raw = strings;
raw.add(123);
String str = strings.get(0);
ACompiles and runs without error
BCompile-time error: cannot add Integer to List
CRuntime ClassCastException when retrieving the element
DCompile-time error: raw type cannot be assigned
Correct Answer:
C. Runtime ClassCastException when retrieving the element
EXPLANATION
The code compiles (with warnings about raw types) but throws ClassCastException at runtime when trying to cast Integer to String. Type safety is lost with raw types.
We can read from map.get() and assign to List<?>, but cannot add to List<?> or put into the map due to wildcard restrictions. Option C only reads the value.
What does the following generic method signature indicate?
public static T findMax(T[] array)
AT must implement Comparable interface parameterized with itself
BT must be a Comparable type but not necessarily of itself
CComparable is optional for T
DT is comparable only with Object types
Correct Answer:
A. T must implement Comparable interface parameterized with itself
EXPLANATION
The bounded type parameter 'T extends Comparable<T>' enforces that T must implement Comparable and be comparable with its own type. This is the F-bounded polymorphism pattern.