AlgoMaster Logo

Generics - Quiz

Last Updated: December 6, 2025

Generics Exercises

29 quizzes

1
Code Completion

Specify the type parameter when creating a Box that stores integers

java
1
Box<> intBox = new Box<>();

Click an option to fill the blank:

2
Code Completion

Access the second element from a List of strings using generics

java
1
String value = names.get();

Click an option to fill the blank:

3
Code Completion

Declare a generic method that works with any reference type

java
1
public static <T> void process( item) { System.out.println(item); }

Click an option to fill the blank:

4
Multiple Choice

What is the primary benefit of using generics with collections like List<T>?

5
Multiple Choice

Given Box<String> box = new Box<>(); what is the type of box.getItem()?

6
Multiple Choice

Which declaration correctly defines a generic class with two type parameters?

7
Multiple Choice

How is a generic method correctly declared?

8
Multiple Choice

Which bounded type parameter allows only subclasses of Number?

9
Multiple Choice

Which wildcard is suitable for a method that only reads elements from a List of numbers?

10
Multiple Choice

Because of type erasure, which statement is true at runtime?

11
Multiple Choice

Which of these is NOT allowed with Java generics?

12
Multiple Choice

Which declaration best represents a lower-bounded wildcard for consuming Numbers or their subtypes?

13
Multiple Choice

Which of the following is a valid multiple bound for a type parameter?

14
Multiple Choice

What happens if you use a raw type like List instead of List<String>?

15
Sequencing

Order the steps to define and use a generic class Pair<K, V> that stores a key and a value.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to implement and call a generic method that prints any array.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code using a generic Box class?

1class Box<T> {
2    private T item;
3    public void setItem(T item) { this.item = item; }
4    public T getItem() { return item; }
5}
6
7Box<String> box = new Box<>();
8box.setItem("Generics");
9System.out.println(box.getItem());
18
Output Prediction

What is the output of this code using a bounded type parameter?

1public static <T extends Number> double square(T number) {
2    return number.doubleValue() * number.doubleValue();
3}
4
5System.out.println(square(3));
19
Output Prediction

What is the output when using an upper-bounded wildcard?

1import java.util.*;
2
3List<Integer> list = Arrays.asList(1, 2, 3);
4printSum(list);
5
6public static void printSum(List<? extends Number> numbers) {
7    double sum = 0;
8    for (Number n : numbers) {
9        sum += n.doubleValue();
10    }
11    System.out.println(sum);
12}
20
Output Prediction

What is the output of this code demonstrating type erasure behavior?

1import java.util.*;
2
3List<String> a = new ArrayList<>();
4List<Integer> b = new ArrayList<>();
5System.out.println(a.getClass() == b.getClass());
21
Output Prediction

What is the output of this code using a generic method with type inference?

1public static <T> T first(T a, T b) {
2    return a;
3}
4
5String s = first("Hello", "World");
6System.out.println(s);
22
Bug Spotting

Find the bug related to generics in this code that sums a list of numbers.

Click on the line(s) that contain the bug.

java
1
import java.util.*;
2
 
3
public class SumUtil {
4
    public static double sum(List<Number> numbers) {
5
        double result = 0;
6
        for (Number n : numbers) {
7
            result += n.doubleValue();
8
        }
9
        return result;
10
    }
11
 
12
    public static void main(String[] args) {
13
        List<Integer> ints = Arrays.asList(1, 2, 3);
14
        System.out.println(sum(ints));
15
    }
16
}
23
Bug Spotting

Find the bug caused by using generics with arrays in this cache implementation.

Click on the line(s) that contain the bug.

java
1
public class Cache<T> {
2
    private T[] items;
3
 
4
    public Cache(int size) {
5
        items = new T[size];
6
    }
7
 
8
    public void put(int index, T value) {
9
        items[index] = value;
10
    }
11
 
12
    public T get(int index) {
13
        return items[index];
14
    }
15
}
24
Matching

Match each wildcard form with its typical usage.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each generics concept with the restriction or behavior it describes.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the generic method that finds the maximum element in an array of comparable items.

java
1
public static <T Comparable<T>> T max(T[] values) {
2
T max = values[0];
3
for (T v : values) {
4
if (v.compareTo(max) > 0) {
5
max = v;
6
}
7
}
8
return max;
9
}
10
11
Integer[] nums = {1, 5, 3};
12
System.out.println(max);

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to declare a Box that only accepts subclasses of Number and retrieve its double value.

java
1
class Box<T Number> {
2
private T value;
3
public Box(T value) { this.value = value; }
4
public double () { return value.doubleValue(); }
5
}
6
7
Box<Integer> box = new Box<>(10);
8
System.out.println(box.());

Click an option to fill blank 1:

28
Hotspot Selection

Click the line that incorrectly attempts to add to a list declared with an upper-bounded wildcard.

Click on the line to select.

java
1
import java.util.*;
2
 
3
List<? extends Number> numbers = new ArrayList<Integer>();
4
numbers.add(10);
5
System.out.println(numbers.size());
29
Hotspot Selection

Click the line that causes an unchecked warning due to mixing raw and generic types.

Click on the line to select.

java
1
import java.util.*;
2
 
3
List rawList = new ArrayList();
4
List<String> strings = rawList;
5
strings.add("test");
6
System.out.println(strings.get(0));

Premium Content

Subscribe to unlock full access to this content and more premium articles.