Type erasure is a mechanism where the Java compiler removes all generic type information during compilation, converting generic code to its raw type equivalent.
Q.2Medium
Which statement about wildcard generics (?) is correct?
Answer: A
Wildcards (?) represent an unknown type. Upper bound (? extends T) accepts T or its subtypes. Lower bound (? super T) accepts T or its supertypes.
Q.3Medium
What will happen when you try to create an array of generics?
List<String>[] array = new List<String>[10];
Answer: A
You cannot create arrays of generic types because of type erasure. The compiler prevents this with a compilation error.
Q.4Medium
Consider the code:
List<? extends Number> list = new ArrayList<>();
list.add(5); // Will this compile?
Answer: A
Upper bounded wildcards (? extends Number) are read-only. You cannot add elements because the compiler doesn't know the exact subtype.
Q.5Medium
Which of the following represents a bounded type parameter?
Answer: A
Bounded type parameters restrict the types that can be used as arguments. T extends Number ensures T is Number or its subclass.
Advertisement
Q.6Medium
What is the difference between List<?> and List<Object>?
Answer: A
List<?> accepts any type but is read-only (can't add). List<Object> explicitly accepts Object type and allows adding objects.
Q.7Medium
Consider this generic interface:
interface Comparable<T> { int compareTo(T o); }
Which class definition correctly implements this?
Answer: A
Option A correctly implements the generic interface by specifying the concrete type parameter as MyClass in the implements clause.
Q.8Medium
What will be the output?
List<Integer> list = new ArrayList<>();
list.add(10);
Object obj = list.get(0);
System.out.println(obj instanceof Integer);
Answer: A
The Integer value is stored in the list. At runtime, type erasure converts it to Object, but the actual stored value remains an Integer instance.
Q.9Medium
Which of these declarations would NOT cause a compilation warning or error?
Answer: A
Option A uses proper diamond syntax with matching type parameters on both sides. Options B, C produce unchecked warnings, and Option D is invalid due to invariance.
Q.10Medium
In the declaration 'List<? extends Number> list', what types can be added to this list at runtime?
Answer: C
Upper-bounded wildcards (? extends T) are used for reading data. Adding elements is unsafe because the compiler doesn't know the exact type.
Q.11Medium
Which statement correctly uses the lower-bounded wildcard in generics?
Answer: A
Lower-bounded wildcards (? super T) allow superclasses of T. This assignment is valid because ArrayList<Number> fits '? super Integer'.
Q.12Medium
Which generic declaration would allow storing both String and Integer in the same collection?
Answer: D
All three allow storing multiple types because they use unbounded or Object-level wildcards, though List<?> is most restrictive for additions.
Q.13Medium
What does the PECS principle stand for in generics context?
Answer: A
PECS guides when to use upper bounds (? extends) for producers/readers and lower bounds (? super) for consumers/writers.
Q.14Medium
In a generic class 'class Box<T extends Comparable<T>>', what constraint is placed on T?
Answer: A
The bound 'T extends Comparable<T>' ensures T is comparable to itself, enabling type-safe comparison operations.
Q.15Medium
Which generic wildcard usage is INCORRECT?
Answer: D
Nested wildcards like '? extends ? super T' are not allowed. Wildcards cannot be combined or nested.
Q.16Medium
What is the relationship between raw types and generics in Java?
Answer: A
Raw types (like 'List' without type parameters) are legacy and should be avoided as they bypass compile-time type safety.
Q.17Medium
Consider the method: public static <T> T getFirst(List<T> list) { return list.get(0); }
How would you call this method for a List<String>?
Answer: D
Type inference in Java allows the compiler to deduce T from the argument type, so both explicit and implicit type specification work.
Q.18Medium
What is the significance of the '&' operator in generic bounds like '<T extends A & B>'?
Answer: A
Multiple bounds (using &) mean T must satisfy all constraints. This is useful when a type needs to implement multiple interfaces.
Q.19Medium
Which statement about generic type parameters is TRUE?
Answer: C
While convention uses single uppercase letters, type parameters can be any valid identifier. Statements B and D are also incorrect.
Q.20Medium
Consider the declaration: List<? super Integer> list. What does this wildcard represent?
Answer: B
'? super Integer' is a lower bounded wildcard that accepts Integer and all its superclasses like Number, Object. This allows write operations with Integer values.