QuizzesGenerics — Concept Check
intermediate
java

Generics — Concept Check

Questions on Generics. Write type-safe reusable classes and methods with generics — eliminate casts, catch type errors at compile time, and understand type erasure.

8 minutes 8 questions Pass at 70%
Time Limit
8 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

8 questions on Generics — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.You have a generic class `Box<T>` and you want to store integers in it. How would you instantiate it?

    • ABox<Integer> box = new Box<>();
    • BBox<Object> box = new Box<>();
    • CBox<int> box = new Box<>();
    • DBox<Number> box = new Box<>();
  2. 2.A method `process(List<? super Integer> list)` is given. What can you do with `list`?

    • AAdd Integer values to it
    • BRead Double values from it
    • CRead Integer values from it
    • DAdd Double values to it
  3. 3.Given `List<? extends Number> numbers`, why can't you add elements to it?

    • AIt only accepts null
    • BIt only accepts Object
    • CIt only accepts Number
    • DThe exact subtype is unknown
  4. 4.What happens if you try `new T[10]` in a generic class?

    • ACompile error due to type erasure
    • BRuntime error
    • CArray of T is created
    • DArray of Objects is created
  5. 5.Why might `instanceof List<String>` cause a compile error?

    • AList cannot be checked
    • BString is not a valid type
    • CGenerics require runtime type
    • DGenerics use type erasure
  6. 6.What is the role of type erasure in Java generics?

    • AAllows runtime type checks
    • BReplaces type parameters with Object
    • CSupports primitive types
    • DEnables method overloading
  7. 7.A method `max(T a, T b)` uses `<T extends Comparable<T>>`. What does this imply?

    • AT must be an Object
    • BT must implement Comparable
    • CT must be a String
    • DT must be a Number
  8. 8.You have a `Stack<E>` class using an Object array internally. Why is this safe?

    • AUnchecked cast is used when popping
    • BObject arrays support generics
    • CGenerics are checked at runtime
    • DType parameters are preserved