QuizzesJava Generics
intermediate
java

Java Generics

Test your understanding of Java Generics: type parameters, bounded wildcards, type erasure, generic methods, and real-world usage.

25 minutes 20 questions Pass at 70%
Time Limit
25 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

20 questions on Java Generics. Work through them here, then start the quiz to answer them and see your score.

  1. 1.What is the main purpose of Generics in Java?

    • ATo improve runtime performance of collections
    • BTo allow classes to extend multiple parents
    • CTo enable automatic memory management
    • DTo write type-safe code that works with any type, catching type errors at compile time
  2. 2.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            list.add("hello");
            // list.add(42);  // would not compile
            String s = list.get(0);  // no cast needed
            System.out.println(s.toUpperCase());
        }
    }
    • AHELLO
    • Bnull
    • Chello
    • DCompilation error
  3. 3.What is a generic class?

    • AA class that extends Object
    • BA class parameterised with one or more type variables declared in angle brackets
    • CA class with only static methods
    • DAn abstract class with type hints
  4. 4.What does this code print?

    class Pair<A, B> {
        A first;
        B second;
        Pair(A first, B second) {
            this.first = first;
            this.second = second;
        }
    }
    public class Main {
        public static void main(String[] args) {
            Pair<String, Integer> p = new Pair<>("score", 100);
            System.out.println(p.first + ": " + p.second);
        }
    }
    • Anull: 0
    • Bscore: 100
    • Cscore: null
    • DCompilation error
  5. 5.What is a generic method?

    • AA method that declares its own type parameter independently of the class
    • BA method that returns a generic collection
    • CA method that accepts any number of arguments
    • DA static method in a generic class
  6. 6.What does this code print?

    public class Main {
        static <T> T firstOrDefault(T[] arr, T def) {
            return arr.length > 0 ? arr[0] : def;
        }
        public static void main(String[] args) {
            String[] names = {"Alice", "Bob"};
            String[] empty = {};
            System.out.println(firstOrDefault(names, "Nobody"));
            System.out.println(firstOrDefault(empty, "Nobody"));
        }
    }
    • ACompilation error
    • BAlice Alice
    • CNobody Nobody
    • DAlice Nobody
  7. 7.What is an upper-bounded wildcard in Java generics?

    • AList<? super Integer> — accepts Integer or any supertype
    • BList<? extends Number> — accepts Number or any subtype
    • CList<T extends Number> — a type parameter constraint
    • DList<?> — accepts any type
  8. 8.What is a lower-bounded wildcard?

    • AList<? super Integer> — accepts Integer or any supertype like Number or Object
    • BList<T super Integer>
    • CList<?> — unbounded wildcard
    • DList<? extends Object>
  9. 9.What does this code print?

    import java.util.*;
    public class Main {
        static double sumList(List<? extends Number> list) {
            double sum = 0;
            for (Number n : list) sum += n.doubleValue();
            return sum;
        }
        public static void main(String[] args) {
            List<Integer> ints = Arrays.asList(1, 2, 3);
            List<Double> doubles = Arrays.asList(1.5, 2.5);
            System.out.println(sumList(ints));
            System.out.println(sumList(doubles));
        }
    }
    • A6 4
    • B6.0 4.0
    • C3.0 2.5
    • DCompilation error
  10. 10.What is type erasure in Java generics?

    • ARemoving unused generic types from the source code
    • BAutomatically deleting type parameters that are never used
    • CThe JVM removes generic type information at compile time — all type parameters become Object (or their bound) in bytecode
    • DCasting a generic type to a raw type at runtime
  11. 11.What does this code cause at compile time?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            List<String> strings = new ArrayList<>();
            List raw = strings;         // raw type
            raw.add(42);                // unchecked warning
            String s = strings.get(0); // ClassCastException at runtime
        }
    }
    • ANo warnings, runs fine
    • BCompilation error on strings.get(0)
    • CUnchecked warning on raw.add(42); ClassCastException at runtime on strings.get(0)
    • DCompilation error on raw.add(42)
  12. 12.What is a bounded type parameter?

    • AA type parameter constrained with extends to only accept certain types
    • BA wildcard with no constraint
    • CA type parameter used only in static methods
    • DA type parameter with a default value
  13. 13.What does this code print?

    public class Main {
        static <T extends Comparable<T>> T max(T a, T b) {
            return a.compareTo(b) >= 0 ? a : b;
        }
        public static void main(String[] args) {
            System.out.println(max(10, 7));
            System.out.println(max("zebra", "apple"));
        }
    }
    • A10 apple
    • B7 zebra
    • C10 zebra
    • D7 apple
  14. 14.Why can you NOT create an array of a generic type like new T[10]?

    • AArrays are immutable in Java
    • BArrays require a fixed size and generics are dynamic
    • CGeneric types cannot be used with arrays at all
    • DDue to type erasure — the JVM does not know T at runtime, so it cannot create a typed array
  15. 15.What is the PECS principle?

    • APrimitive, Erasure, Cast, Safe
    • BPublic, Encapsulated, Concrete, Singular
    • CProducer Extends, Consumer Super — a guide for choosing wildcard bounds
    • DParameter, Extends, Class, Sub
  16. 16.What does this code print?

    import java.util.*;
    public class Main {
        static <T> void copy(List<? extends T> src, List<? super T> dest) {
            for (T item : src) dest.add(item);
        }
        public static void main(String[] args) {
            List<Integer> ints = new ArrayList<>(Arrays.asList(1, 2, 3));
            List<Number> nums = new ArrayList<>();
            copy(ints, nums);
            System.out.println(nums);
        }
    }
    • A[null, null, null]
    • B[]
    • CCompilation error
    • D[1, 2, 3]
  17. 17.Can a generic class extend another generic class in Java?

    • AOnly with interfaces, not classes
    • BOnly if the type parameters match exactly
    • CNo, only non-generic classes can be extended
    • DYes — class Child<T> extends Parent<T> is valid
  18. 18.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            List<Integer> ints = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5));
            List<String> strs = new ArrayList<>(Arrays.asList("banana", "apple"));
            Collections.sort(ints);
            Collections.sort(strs);
            System.out.println(ints.get(0));
            System.out.println(strs.get(0));
        }
    }
    • A1 apple
    • B3 banana
    • C5 banana
    • D1 banana
  19. 19.What is a raw type and why should you avoid it?

    • AA primitive type used in a generic context — avoid boxing overhead
    • BA deprecated type — avoid for compatibility reasons
    • CA type with no methods — avoid because it has no API
    • DA generic class or interface used without a type argument — avoid because it bypasses compile-time type checking
  20. 20.What is the difference between List<?> and List<Object>?

    • AList<?> can only hold null; List<Object> holds any object
    • BList<?> accepts any parameterised List but is read-only; List<Object> only accepts List<Object> and you can add Object values
    • CThey are identical
    • DList<Object> is safer than List<?>