Generics enable type safety at compile time and eliminate the need for explicit type casting, reducing runtime errors.
Q.2Easy
Which keyword is used to declare a generic class in Java?
Answer: A
<T> is the standard syntax for declaring a generic type parameter in Java classes.
Q.3Easy
What will be the output of the following code?
List<String> list = new ArrayList<>();
list.add("Java");
System.out.println(list.get(0).length());
Answer: A
"Java" has 4 characters. The generic type ensures the element is a String, so .length() method is available without casting.
Q.4Easy
Which of the following is a valid generic method declaration?
Answer: A
The correct syntax for a generic method is: access_modifier <T> returnType methodName(T parameter). The type parameter <T> must come before the return type.
Q.5Medium
What is type erasure in Java Generics?
Answer: A
Type erasure is a mechanism where the Java compiler removes all generic type information during compilation, converting generic code to its raw type equivalent.
Advertisement
Q.6Medium
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.7Medium
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.8Medium
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.9Medium
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.
Q.10Medium
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.11Medium
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.12Medium
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.13Hard
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.14Hard
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.15Hard
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.16Easy
What is the output of this generic code?
class Pair<K, V> {
public void display(K key, V value) {
System.out.println(key + ": " + value);
}
}
Pair<String, Integer> p = new Pair<>();
p.display("Age", 25);
Answer: A
The generic class Pair with two type parameters K and V correctly accepts String and Integer. The display method prints the key-value pair.
Q.17Hard
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.18Hard
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.19Medium
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.20Easy
In Java generics, what does the wildcard '?' represent?
Answer: A
The wildcard '?' in generics represents an unknown type, allowing flexibility when the exact type is not known or not important.