Last Updated: December 6, 2025
29 quizzes
Specify the type parameter when creating a Box that stores integers
Box<> intBox = new Box<>();Click an option to fill the blank:
Access the second element from a List of strings using generics
String value = names.get();Click an option to fill the blank:
Declare a generic method that works with any reference type
public static <T> void process( item) { System.out.println(item); }Click an option to fill the blank:
What is the primary benefit of using generics with collections like List<T>?
Given Box<String> box = new Box<>(); what is the type of box.getItem()?
Which declaration correctly defines a generic class with two type parameters?
How is a generic method correctly declared?
Which bounded type parameter allows only subclasses of Number?
Which wildcard is suitable for a method that only reads elements from a List of numbers?
Because of type erasure, which statement is true at runtime?
Which of these is NOT allowed with Java generics?
Which declaration best represents a lower-bounded wildcard for consuming Numbers or their subtypes?
Which of the following is a valid multiple bound for a type parameter?
What happens if you use a raw type like List instead of List<String>?
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.
Order the steps to implement and call a generic method that prints any array.
Drag and drop to reorder, or use the arrows.
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());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));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}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());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);Find the bug related to generics in this code that sums a list of numbers.
Click on the line(s) that contain the bug.
import java.util.*; public class SumUtil { public static double sum(List<Number> numbers) { double result = 0; for (Number n : numbers) { result += n.doubleValue(); } return result; } public static void main(String[] args) { List<Integer> ints = Arrays.asList(1, 2, 3); System.out.println(sum(ints)); }}Find the bug caused by using generics with arrays in this cache implementation.
Click on the line(s) that contain the bug.
public class Cache<T> { private T[] items; public Cache(int size) { items = new T[size]; } public void put(int index, T value) { items[index] = value; } public T get(int index) { return items[index]; }}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.
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.
Complete the generic method that finds the maximum element in an array of comparable items.
public static <T Comparable<T>> T max(T[] values) { T max = values[0]; for (T v : values) { if (v.compareTo(max) > 0) { max = v; } } return max;}Integer[] nums = {1, 5, 3};System.out.println(max);Click an option to fill blank 1:
Complete the code to declare a Box that only accepts subclasses of Number and retrieve its double value.
class Box<T Number> { private T value; public Box(T value) { this.value = value; } public double () { return value.doubleValue(); }}Box<Integer> box = new Box<>(10);System.out.println(box.());Click an option to fill blank 1:
Click the line that incorrectly attempts to add to a list declared with an upper-bounded wildcard.
Click on the line to select.
import java.util.*; List<? extends Number> numbers = new ArrayList<Integer>();numbers.add(10);System.out.println(numbers.size());Click the line that causes an unchecked warning due to mixing raw and generic types.
Click on the line to select.
import java.util.*; List rawList = new ArrayList();List<String> strings = rawList;strings.add("test");System.out.println(strings.get(0));