Skills101
← All practice quizzes

Quiz: Java Generics

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

Write one class or method that works safely for many types. Generic classes and methods, bounded types, why List<Integer> is not a List<Number>, wildcards with extends and super, and type erasure.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    What is the main benefit of generics?

  2. Question 2

    What does this print?

    class Box<T> {
        T item;
        Box(T item) { this.item = item; }
        T get() { return item; }
    }
    public class Main {
        public static void main(String[] args) {
            Box<String> b = new Box<>("pen");
            Box<Integer> n = new Box<>(5);
            System.out.println(b.get().length() + n.get());
        }
    }
    
  3. Question 3

    Does List<Number> nums = new ArrayList<Integer>(); compile?

  4. Question 4

    Which parameter type lets a method ADD Integer values to a List<Number> or a List<Object>?

  5. Question 5

    In static <T extends Comparable<T>> T max(List<T> list), why is the bound needed?

🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →