Exam weight Low
Exam weight Low
Exam weight
Medium
- Generic methods and classes: one declaration for a set of related types, with compile-time type safety.
Exam weight Low
- Motivation: three overloaded printArray methods differ only in the element type → one generic method.
Exam weight
High
- A generic method declares its type parameter before the return type: public static <T> void printArray(T[] a).
🎯 How this can come up · 1 question
MCQ
Which header declares a generic method?
Correct: public static <T> void print(T[] a).
Exam weight Low
Exam weight
High
- To compare with >, the type must be bounded: <T extends Comparable<T>> T maximum(T x, T y, T z).
Exam weight Low
Exam weight Low
Exam weight
High
- Comparable is the upper bound; the default bound is Object. extends is used even for interfaces.
🎯 How this can come up · 1 question
True / False
In a bounded type parameter you write implements for interfaces, e.g. <T implements Comparable<T>>.
False. Always extends.
Exam weight Low
Exam weight Low
Exam weight Low
- Generic class: class Sample<T> { private T data; … }
Exam weight
High
- Sample<String> s = new Sample<String>(); — the type argument can’t be primitive (use Integer, not int).
🎯 How this can come up · 1 question
MCQ
Which is NOT allowed as a type argument?
Correct: int.
Exam weight Low
Exam weight Low
Exam weight Low
- LinkedList<E> is a generic class; E is the element type.
Exam weight Low
No slides match this filter.