Which of these correctly demonstrates the Producer Extends Consumer Super (PECS) principle?
Answer: A
PECS principle: use 'extends' when reading from a collection (source), use 'super' when writing to it (dest). Option A reads from source and writes to dest correctly.
Q.2Hard
What will happen with this code?
List list = new ArrayList<String>();
list.add(123); // Adding Integer
String s = (String) list.get(0);
Answer: A
Raw type List accepts any object. At runtime, the Integer 123 cannot be cast to String, causing ClassCastException.
Q.3Hard
Consider this code:
public <T extends Comparable<T>> T getMax(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
What is the benefit of this recursive bound <T extends Comparable<T>>?
Answer: A
Recursive bound <T extends Comparable<T>> ensures that T implements Comparable interface specifically for comparing with its own type, providing type safety.
Q.4Hard
Which statement is true about generic inheritance?
Answer: B
ArrayList<Integer> is a subtype of List<Integer> because ArrayList is a subclass of List with the same type parameter.
Q.5Hard
What will be the result of this code?
List<String> list = new ArrayList<>();
list.add("Hello");
List raw = list; // Unchecked assignment
raw.add(123); // Adding Integer to raw type
String s = list.get(1);
Answer: A
Raw type assignment bypasses generics. Integer 123 is added to the list. When casting to String at get(1), ClassCastException occurs.
Advertisement
Q.6Hard
What will happen when you try to create an array of generic types like 'new ArrayList<String>[10]'?
Answer: A
Arrays of generic types are not allowed in Java due to type erasure and heap pollution prevention.
Q.7Hard
Consider: Map<String, ? extends List<?>> map = new HashMap<>();
What can you safely retrieve from this map?
Answer: A
The value type is '? extends List<?>', so you get a List with unknown element type. The exact subtype is unknown at compile-time.
Q.8Hard
What is the key difference between '? extends T' and '? super T' in practical usage?
Answer: A
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.
Q.9Hard
Which generic declaration allows a method to accept List of any type?
Answer: D
All three approaches allow processing lists of any type, though they differ: raw type (unsafe), unbounded wildcard (read-only), and type parameter (flexible).
Q.10Hard
What is the difference between <T extends Comparable<T>> and <T extends Comparable>?
Answer: B
<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.
Q.11Hard
Can you create an instance of generic type parameter directly like: T obj = new T();?
Answer: B
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.
Q.12Hard
What is the PECS principle in generics?
Answer: A
PECS (Producer Extends, Consumer Super) is a mnemonic for wildcard bounds: use '? extends' when reading (producing), use '? super' when writing (consuming).
Q.13Hard
Which statement about generic constructors is TRUE?
Answer: D
Generic constructors can exist in non-generic classes. Constructor's type parameters are separate from and independent of the class's type parameters.
Q.14Hard
Which statement correctly implements a generic factory method?
Answer: B
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.
Q.15Hard
Which generic wildcard usage follows the consumer pattern correctly?
Answer: B
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.
Q.16Hard
What is the compiled signature of this generic method?
public <T extends Number & Comparable<T>> void sort(T[] array)
Answer: D
With multiple bounds using '&', type erasure replaces T with the first bound. Here, Number is the first bound, so T is replaced with Number.
Q.17Hard
Which of the following represents valid bounded wildcard usage for a method that processes collections?
Answer: B
Only option B allows adding elements safely. '? super Integer' means the collection can hold Integer or any supertype. Option A prevents adding, Option C prevents adding, Option D restricts to exactly Number.
Q.18Hard
How would you correctly use generics in a recursive type bound scenario?
Answer: A
Option A demonstrates recursive type bound (also called F-bounded polymorphism), which allows a type to reference itself in its bound. This is commonly used in builder patterns and comparators.
Q.19Hard
What will happen when you compile and run this code?
java
List<String> strings = Arrays.asList("a", "b", "c");
List raw = strings;
raw.add(123);
String s = strings.get(3);
Answer: C
Assigning generic type to raw type suppresses type checking. The Integer added bypasses the type system, causing ClassCastException when retrieved as String.
Q.20Hard
Consider this declaration: public static <T> List<T> createList(). Which statement about type inference is correct?
Answer: B
Java's type inference deduces T from the assignment context. E.g., 'List<String> list = createList()' infers T as String.