Java Generics Deep Dive Quiz

Java
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

Why were generics introduced in Java 5?

A
To provide compile-time type safety and eliminate ClassCastException
B
To make code run faster
C
To reduce memory usage
D
To simplify syntax
2

Question 2

What is type erasure?

A
The process where generic type information is removed at compile-time
B
When generic types are converted to raw types
C
When generics are ignored by the compiler
D
A way to bypass generic restrictions
3

Question 3

What is the syntax for a generic class?

java
public class Box<T> {
    private T item;
    
    public void set(T item) { this.item = item; }
    public T get() { return item; }
}
A
class ClassName<T> { ... }
B
class ClassName[T] { ... }
C
class ClassName(T) { ... }
D
class <T>ClassName { ... }
4

Question 4

What is a generic method?

java
public static <T> T getFirst(List<T> list) {
    return list.isEmpty() ? null : list.get(0);
}
A
A method with its own type parameters
B
A method that works with any type
C
A method in a generic class
D
A method that returns Object
5

Question 5

What is a bounded type parameter?

java
public static <T extends Number> double sum(List<T> numbers) {
    return numbers.stream().mapToDouble(Number::doubleValue).sum();
}
A
A type parameter restricted to a specific class or its subclasses
B
A type parameter with no restrictions
C
A type parameter that can only be a primitive
D
A type parameter for collections only
6

Question 6

What does ? extends T mean in generics?

A
Any type that is T or a subclass of T (upper bounded wildcard)
B
Any type that is T or a superclass of T
C
Exactly type T only
D
Any type except T
7

Question 7

What does ? super T mean in generics?

A
Any type that is T or a superclass of T (lower bounded wildcard)
B
Any type that is T or a subclass of T
C
Exactly type T only
D
Any type except T
8

Question 8

What is a raw type?

java
List rawList = new ArrayList();  // raw type
List<String> genericList = new ArrayList<>();  // parameterized type
A
A generic type used without type parameters
B
A type without generics
C
A primitive type
D
An array type
9

Question 9

What is the output of this generic method example?

java
public static <T> void printType(T item) {
    System.out.println(item.getClass().getSimpleName());
}

printType("hello");
printType(42);
A
String Integer
B
Object Object
C
Compilation error
D
Runtime exception
10

Question 10

Why can't you create generic arrays?

java
List<String>[] array = new List<String>[10];  // Compilation error
A
Type erasure makes it impossible to enforce type safety at runtime
B
Arrays don't support generics
C
It's a language limitation
D
Memory constraints
11

Question 11

What is the difference between List<? extends Number> and List<Number>?

A
List<? extends Number> is more flexible for reading, List<Number> is more restrictive
B
They are identical
C
List<Number> allows writing, List<? extends Number> doesn't
D
List<? extends Number> is deprecated
12

Question 12

What is PECS (Producer Extends Consumer Super)?

A
A mnemonic for when to use ? extends T (producer) vs ? super T (consumer)
B
A type of generic class
C
A wildcard syntax
D
A method naming convention
13

Question 13

What is the result of this bounded parameter example?

java
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);
A
banana
B
apple
C
Compilation error
D
Runtime exception
14

Question 14

Why should you avoid raw types?

java
List raw = new ArrayList();
raw.add("string");
raw.add(123);
String s = (String) raw.get(1);  // ClassCastException
A
They bypass compile-time type checking, leading to runtime ClassCastException
B
They are slower
C
They use more memory
D
They are deprecated
15

Question 15

What is generic type inference?

java
List<String> list = new ArrayList<>();  // Diamond operator infers <String>
A
The compiler's ability to determine generic types from context
B
Manually specifying type parameters
C
Using reflection to determine types
D
Runtime type checking
16

Question 16

What is the output of this wildcard example?

java
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());
A
Integer
B
Number
C
Compilation error
D
Runtime exception
17

Question 17

What is a generic constructor?

java
public class Container<T> {
    private T value;
    
    public <U> Container(U value) {  // Generic constructor
        this.value = (T) value;
    }
}
A
A constructor with its own type parameters
B
A constructor that accepts any type
C
A constructor in a generic class
D
A constructor that returns a generic type
18

Question 18

What is the problem with this generic code?

java
public static <T> T[] toArray(List<T> list) {
    T[] array = new T[list.size()];  // Compilation error
    return list.toArray(array);
}
A
Cannot create generic arrays due to type erasure
B
List doesn't have toArray method
C
T is not a valid type
D
Array size cannot be determined
19

Question 19

What is the difference between T and ? in generics?

A
T is a type parameter, ? is a wildcard for unknown type
B
They are identical
C
? is deprecated
D
T is for classes, ? is for methods
20

Question 20

What is the result of this super wildcard example?

java
List<? super Integer> numbers = new ArrayList<Number>();
numbers.add(42);  // OK
// Integer i = numbers.get(0);  // Compilation error
Object obj = numbers.get(0);  // OK
A
Allows writing Integer but reading only as Object
B
Allows both reading and writing
C
Compilation error
D
Runtime exception
21

Question 21

Why do generics use type erasure?

A
To maintain backward compatibility with pre-generics code
B
To improve performance
C
To reduce memory usage
D
To simplify the language
22

Question 22

What is a generic static method?

java
public class Utils {
    public static <T> boolean isEmpty(List<T> list) {
        return list == null || list.isEmpty();
    }
}
A
A static method with type parameters
B
A method that works with static types
C
A method in a generic class
D
A method that returns static values
23

Question 23

What is the output of this type inference example?

java
public static <T> T pick(T a, T b) { return a; }

String result = pick("hello", "world");
System.out.println(result);
A
hello
B
world
C
Compilation error
D
Runtime exception
24

Question 24

What is a recursive type bound?

java
public static <T extends Comparable<T>> T max(List<T> list) {
    // Implementation
}
A
A bound where the type parameter refers to itself
B
A bound that creates infinite recursion
C
A bound for recursive data structures
D
A deprecated feature
25

Question 25

Why can't static fields be generic?

java
public class Container<T> {
    // private static T value;  // Compilation error
    private T instanceValue;  // OK
}
A
Static fields are shared across all instances, but generic types vary per instance
B
Static fields don't support generics
C
Memory constraints
D
Language limitation
26

Question 26

What is the result of this multiple bounds example?

java
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));
A
6.0
B
Compilation error
C
Runtime exception
D
0.0
27

Question 27

What is heap pollution?

java
List raw = Arrays.asList(1, 2, 3);
List<String> strings = (List<String>) (List) raw;  // Heap pollution
String s = strings.get(0);  // ClassCastException
A
When raw types and parameterized types are mixed, corrupting type safety
B
Memory leaks in generic code
C
Performance issues with generics
D
Thread safety problems
28

Question 28

What is the difference between List<?> and List<Object>?

A
List<?> is a wildcard for unknown type, List<Object> is specifically Object type
B
They are identical
C
List<?> allows any type, List<Object> is more restrictive
D
List<Object> is deprecated
29

Question 29

What is the output of this generic method with wildcards?

java
public static void printList(List<?> list) {
    for (Object item : list) {
        System.out.print(item + " ");
    }
}

List<Integer> numbers = Arrays.asList(1, 2, 3);
printList(numbers);
A
1 2 3
B
Compilation error
C
Runtime exception
D
No output
30

Question 30

Why should you prefer generics over raw types?

A
Compile-time type safety, better IDE support, self-documenting code
B
Faster runtime performance
C
Reduced memory usage
D
Simpler syntax
31

Question 31

What is the result of this bounded wildcard example?

java
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);
}
A
Manual copying is needed because wildcards don't allow direct collection operations
B
addAll works fine
C
Compilation error
D
Runtime exception
32

Question 32

What is generic method overloading?

java
public static <T> void process(List<T> list) { /* general */ }
public static void process(List<String> list) { /* specific */ }
A
Overloading generic methods with specific type parameters
B
Creating multiple generic methods
C
Overriding generic methods
D
Not allowed in Java
33

Question 33

What is the problem with this generic interface?

java
public interface Processor<T> {
    T process(T input);
}

public class StringProcessor implements Processor<String> {
    public String process(String input) {
        return input.toUpperCase();
    }
}
A
Nothing, this is correct generic interface usage
B
Generic interfaces are not allowed
C
Type parameter must be bounded
D
Implementation must be generic
34

Question 34

What is the output of this type erasure example?

java
List<String> strings = new ArrayList<>();
List<Integer> integers = new ArrayList<>();

System.out.println(strings.getClass() == integers.getClass());  // true
System.out.println(strings.getClass().getSimpleName());
A
true ArrayList
B
false ArrayList
C
Compilation error
D
Runtime exception
35

Question 35

Mastering Java generics means you now write code that:

A
Leverages compile-time type safety to prevent runtime casting errors, creates reusable generic algorithms, and uses wildcards appropriately for flexible APIs
B
Avoids generics entirely
C
Uses raw types for simplicity
D
Ignores type safety

QUIZZES IN Java