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.
Q.162Hard
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.163Hard
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.164Hard
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.165Hard
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).
Advertisement
Q.166Hard
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.167Hard
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.168Hard
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.169Hard
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.170Hard
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.171Hard
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.172Hard
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.173Hard
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.174Hard
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.175Hard
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.176Hard
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.
Q.177Hard
What is the difference between List<Object> and List<?> in practical usage?
Answer: C
List<Object> accepts Object and its subclasses for adding. List<?> (unbounded wildcard) is more flexible for reading but restrictive for writing (only null).
Q.178Hard
What is the compiled bytecode signature of this generic method?
public <T extends Comparable<T>> T findMin(T[] arr)
Answer: A
Type erasure replaces T with its bound Comparable. The method signature becomes public Comparable findMin(Comparable[] arr).
Q.179Hard
Which statement about generic array creation is correct?
Answer: C
You cannot create arrays of generic types due to type erasure and heap pollution risks. Use Collection framework instead of arrays.
Q.180Hard
What output will this produce?
java
List<? super Integer> list = new ArrayList<Number>();
list.add(5);
Integer val = (Integer) list.get(0);
Answer: C
With lower-bounded wildcard (? super Integer), you can add Integer but retrieval returns Object. Casting Object to Integer without proper type causes ClassCastException.