In a generic class, what happens to type parameter when instantiated?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

In a generic class, what happens to type parameter when instantiated?

Explanation:
Generics use a placeholder for a type, and that placeholder gets fixed to a concrete type when you create an instance. When you instantiate a generic class with a specific type, the type parameter is replaced by that real type for that instance, so all the type checks and operations inside the class use that concrete type. For example, a generic container like Box<T> instantiated as Box<String> treats T as String, enabling methods to work with String values safely. This substitution is what gives generic code its safety and flexibility. Note that in some languages runtime type information may not be preserved (type erasure), but the fundamental idea is that the placeholder becomes a real type for the specific instance. The other options don’t fit because the type parameter isn’t discarded, it doesn’t become a runtime value, and it isn’t restricted to a primitive type.

Generics use a placeholder for a type, and that placeholder gets fixed to a concrete type when you create an instance. When you instantiate a generic class with a specific type, the type parameter is replaced by that real type for that instance, so all the type checks and operations inside the class use that concrete type. For example, a generic container like Box instantiated as Box treats T as String, enabling methods to work with String values safely. This substitution is what gives generic code its safety and flexibility. Note that in some languages runtime type information may not be preserved (type erasure), but the fundamental idea is that the placeholder becomes a real type for the specific instance. The other options don’t fit because the type parameter isn’t discarded, it doesn’t become a runtime value, and it isn’t restricted to a primitive type.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy