Java Generics Deep Dive Quiz
35 in-depth questions (18 with code) covering why generics exist, generic methods, bounded type parameters, wildcards (? extends, ? super), and raw types pitfalls in Java.
Question 1
Why were generics introduced in Java 5?
Question 2
What is type erasure?
Question 3
What is the syntax for a generic class?
public class Box<T> {
private T item;
public void set(T item) { this.item = item; }
public T get() { return item; }
}Question 4
What is a generic method?
public static <T> T getFirst(List<T> list) {
return list.isEmpty() ? null : list.get(0);
}Question 5
What is a bounded type parameter?
public static <T extends Number> double sum(List<T> numbers) {
return numbers.stream().mapToDouble(Number::doubleValue).sum();
}Question 6
What does ? extends T mean in generics?
Question 7
What does ? super T mean in generics?
Question 8
What is a raw type?
List rawList = new ArrayList(); // raw type
List<String> genericList = new ArrayList<>(); // parameterized typeQuestion 9
What is the output of this generic method example?
public static <T> void printType(T item) {
System.out.println(item.getClass().getSimpleName());
}
printType("hello");
printType(42);Question 10
Why can't you create generic arrays?
List<String>[] array = new List<String>[10]; // Compilation errorQuestion 11
What is the difference between List<? extends Number> and List<Number>?
Question 12
What is PECS (Producer Extends Consumer Super)?
Question 13
What is the result of this bounded parameter example?
public static <T extends Comparable<T>> T max(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
String result = max("apple", "banana");
System.out.println(result);Question 14
Why should you avoid raw types?
List raw = new ArrayList();
raw.add("string");
raw.add(123);
String s = (String) raw.get(1); // ClassCastExceptionQuestion 15
What is generic type inference?
List<String> list = new ArrayList<>(); // Diamond operator infers <String>Question 16
What is the output of this wildcard example?
List<? extends Number> numbers = Arrays.asList(1, 2.0, 3L);
Number first = numbers.get(0); // OK
// numbers.add(4); // Compilation error
System.out.println(first.getClass().getSimpleName());Question 17
What is a generic constructor?
public class Container<T> {
private T value;
public <U> Container(U value) { // Generic constructor
this.value = (T) value;
}
}Question 18
What is the problem with this generic code?
public static <T> T[] toArray(List<T> list) {
T[] array = new T[list.size()]; // Compilation error
return list.toArray(array);
}Question 19
What is the difference between T and ? in generics?
Question 20
What is the result of this super wildcard example?
List<? super Integer> numbers = new ArrayList<Number>();
numbers.add(42); // OK
// Integer i = numbers.get(0); // Compilation error
Object obj = numbers.get(0); // OKQuestion 21
Why do generics use type erasure?
Question 22
What is a generic static method?
public class Utils {
public static <T> boolean isEmpty(List<T> list) {
return list == null || list.isEmpty();
}
}Question 23
What is the output of this type inference example?
public static <T> T pick(T a, T b) { return a; }
String result = pick("hello", "world");
System.out.println(result);Question 24
What is a recursive type bound?
public static <T extends Comparable<T>> T max(List<T> list) {
// Implementation
}Question 25
Why can't static fields be generic?
public class Container<T> {
// private static T value; // Compilation error
private T instanceValue; // OK
}Question 26
What is the result of this multiple bounds example?
public static <T extends Number & Comparable<T>> double sum(List<T> numbers) {
return numbers.stream().mapToDouble(Number::doubleValue).sum();
}
List<Integer> ints = Arrays.asList(1, 2, 3);
System.out.println(sum(ints));Question 27
What is heap pollution?
List raw = Arrays.asList(1, 2, 3);
List<String> strings = (List<String>) (List) raw; // Heap pollution
String s = strings.get(0); // ClassCastExceptionQuestion 28
What is the difference between List<?> and List<Object>?
Question 29
What is the output of this generic method with wildcards?
public static void printList(List<?> list) {
for (Object item : list) {
System.out.print(item + " ");
}
}
List<Integer> numbers = Arrays.asList(1, 2, 3);
printList(numbers);Question 30
Why should you prefer generics over raw types?
Question 31
What is the result of this bounded wildcard example?
List<? extends Number> source = Arrays.asList(1, 2.0, 3L);
List<Number> target = new ArrayList<>();
// target.addAll(source); // Compilation error
// Correct way:
for (Number n : source) {
target.add(n);
}Question 32
What is generic method overloading?
public static <T> void process(List<T> list) { /* general */ }
public static void process(List<String> list) { /* specific */ }Question 33
What is the problem with this generic interface?
public interface Processor<T> {
T process(T input);
}
public class StringProcessor implements Processor<String> {
public String process(String input) {
return input.toUpperCase();
}
}Question 34
What is the output of this type erasure example?
List<String> strings = new ArrayList<>();
List<Integer> integers = new ArrayList<>();
System.out.println(strings.getClass() == integers.getClass()); // true
System.out.println(strings.getClass().getSimpleName());Question 35
Mastering Java generics means you now write code that:
