Which statement correctly defines a generic class with multiple type parameters and bounds?
Answer: C
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.
Q.22Medium
Consider: public static <T> T findMax(T a, T b) { return a; }. What is the issue with calling findMax(5, 3.5)?
Answer: A
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.
Q.23Medium
What does List<?> represent in Java generics?
Answer: D
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.
Q.24Medium
Which of the following will NOT compile?
Answer: D
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).
Q.25Medium
Consider a generic interface: interface Comparable<T> { int compareTo(T obj); }. How should a class implement this for String comparison?
Answer: D
Option B directly specifies String, while Option C makes the class generic. Both are valid approaches to implement a generic interface.
Advertisement
Q.26Medium
What happens when you use raw type List instead of List<String>?
Answer: D
Using raw types bypasses generic type checking, losing type safety. Elements are treated as Object at runtime, potentially causing ClassCastException when casting.
Q.27Medium
What is the output?
List<String> strings = new ArrayList<String>();
List list = strings;
list.add(123);
String str = strings.get(0);
System.out.println(str);
Answer: C
Adding Integer 123 to raw type reference bypasses generics. When retrieving as String, a ClassCastException occurs at runtime.
Q.28Medium
How would you define a generic method that works with a pair of objects and returns the first one?
Answer: D
Both A and C define valid generic methods with multiple type parameters. C is more explicit with 'public' modifier, but both are functionally equivalent.
Q.29Medium
Consider the following code snippet:
public <T> T processData(List<? extends T> list) { return list.get(0); }
What is the relationship between the wildcard and type parameter T?
Answer: B
The wildcard '? extends T' means the list can contain any type that is a subtype of T. This allows reading from the list safely but restricts writing.
Q.30Medium
Which of the following will cause a compile-time error?
Answer: B
Java does not allow covariance with generics. List<Integer> cannot be assigned to List<Number> because you could add a Double to the latter, breaking type safety.
Q.31Medium
What does the following generic method signature indicate?
public static <T extends Comparable<T>> T findMax(T[] array)
Answer: A
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.
Q.32Medium
Given: Map<String, ? extends List<?>> map = new HashMap<>();
Which operation is valid?
Answer: C
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.
Q.33Medium
What is the output of this code?
List<String> strings = new ArrayList<>();
List raw = strings;
raw.add(123);
String str = strings.get(0);
Answer: C
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.
Q.34Medium
Consider:
public class Pair<T, U> { }
Which declaration is invalid?
Answer: D
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'.
Q.35Medium
What is the correct way to declare a generic method that accepts a list and returns the maximum element?
Answer: D
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.
Q.36Medium
Given the declaration: List<List<String>> listOfLists;
Which assignment is valid?
Answer: D
Nested generics must match exactly or use appropriate wildcards. Only option D provides exact type match. Option B would work with wildcards, but that's not listed correctly here.
Q.37Medium
What happens when you try to cast a generic object?
List<String> strings = (List<String>) getSomeList();
Answer: B
Due to type erasure, casts involving generic types are unchecked. The compiler warns about this but allows it. The actual cast check only verifies the raw type.
Q.38Medium
In the context of generics, what does 'invariance' mean?
Answer: B
Generics are invariant in Java. List<Integer> is NOT a subtype of List<Number> to maintain type safety. This is a key feature preventing runtime errors.
Q.39Medium
What does the following generic declaration mean?
public <T extends Number & Comparable<T>> T findMax(T a, T b)
Answer: B
Multiple bounds in generics use '&' to specify that the type must satisfy all constraints. T must extend Number AND implement Comparable<T>.
Q.40Medium
Consider the code:
java
List<Number> list = new ArrayList<Integer>();
Will this compile?
Answer: B
Generics are invariant. ArrayList<Integer> cannot be assigned to List<Number> even though Integer is a subtype of Number. This prevents type safety issues.