Bounded Type QuizJ8 Home « Bounded Type Quiz

Generics Quiz 5

The quiz below tests your knowledge of the material learnt in Generics - Lesson 5 - Bounded Type.

Question 1 : How does the diamond operator work ?
- The diamond operator works through <i>inference</i>.
Question 2 : Which of the following code snippets are valid?
List<Object> aList = new ArrayList<String>(); // 1
List<String> aList = new ArrayList<String>(); // 2
- Generics are by design non-polymorphic, which means for any two distinct types Type1 and Type2, Type1 is neither a supertype or subtype of ListType2. So only answer 3 is correct.
Question 3 : Generics are by design what?
- Generics are by design <i>invariant</i> (non-polymorphic).
Question 4 : What is the following code snippet an example of <T extends Number> ?
- T extends Number is an example of a <i>bounded type</i>.
Question 5 : Which object type can be passed to this classes constructor?
public class BoundedType<T extends Number> {

    // Generic object declaration
    private final T genericNumberObj;
    // Pass reference to object of type T to our constructor

    public BoundedType(T genericNumberObj) {
        this.genericNumberObj = genericNumberObj;
- Only objects that extend <code>Number</code> are valid and so <code>Byte</code> is the answer.
Question 6 : Can we use primitives with Bounded Type?
- Primitives are invalid for any generic.
Question 7 : Is the following code snippet an example of a bounded type?
<? extends Number>
- The code snippet <code>? extends Number</code> is an example of a <i>bounded wildcard type</i> not a <i>bounded type</i>.
Quiz Progress Bar Please select an answer

What's Next?

In the next quiz we test your knowledge of Unbounded Wildcard Type.