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 happens when you use raw type List instead of List?
AType safety is lost and unchecked warnings appear
BAll elements are treated as Object
CRuntime ClassCastException may occur
DAll of the above
Correct Answer:
D. All of the above
EXPLANATION
Using raw types bypasses generic type checking, losing type safety. Elements are treated as Object at runtime, potentially causing ClassCastException when casting.
Correct Answer:
D. List objects = new ArrayList();
EXPLANATION
Option D fails because List<Object> cannot reference ArrayList<String>. Generics are invariant. Options A, B, C are valid (covariance with extends, contravariance with super).
List<?> is an unbounded wildcard that represents a list of unknown type. You can read from it (get Object), but cannot write to it (except null). Both A, B, and C are correct interpretations.
Consider: public static T findMax(T a, T b) { return a; }. What is the issue with calling findMax(5, 3.5)?
AT cannot be inferred because int and double are different
BThe method will compile and run without issues
CT will be Object type
DRuntime exception will occur
Correct Answer:
A. T cannot be inferred because int and double are different
EXPLANATION
The type parameter T cannot simultaneously be int and double. The compiler cannot infer a single type T that satisfies both arguments, resulting in a compilation error.
Which statement correctly defines a generic class with multiple type parameters and bounds?
Aclass Container { }
Bclass Container extends Number { }
Cclass Container { }
Dclass Container extends { }
Correct Answer:
C. class Container { }
EXPLANATION
Multiple type parameters with bounds must be comma-separated and each can have its own upper bound. Option C shows correct syntax with recursive bound on T and bound on K.