Generic Classes QuizS2C Home « Generic Classes Quiz

Generics Quiz 4

The quiz below tests your knowledge of the material learnt in Generics - Lesson 4 - Generic Classes.

Question 1 : What's the difference between creating generalized classes using an Object reference and generics?
- Using an <code>Object</code> reference isn't type safe and generics are.
Question 2 : How would we declare a generic class that accepts any object?
public class SimpleGenericClass<> { ... } //1
public class SimpleGenericClass<Object> { ... } //2
public class SimpleGenericClass<T> { ... } /3
public class SimpleGenericClass<?> { ... } //4
- We would declare a generic class that accepts any object using answer 3.
Question 3 : Which of the following code snippets is invalid?
SimpleGenericClass genStringObj = new SimpleGenericClass<String>("A stitch in time"); //1
SimpleGenericClass genDoubleObj = new SimpleGenericClass<double>(123.456); //2
SimpleGenericClass genIntegerObj = new SimpleGenericClass<Integer>(123); //3
- Answer 3 is invalid as you can't use primitive types with generics. We used double instead of Double.
Question 4 : In the following code snippet what is the T between the angled brackets known as?
public class SimpleGenericClass<T>();
- The <code>T</code> between the angled brackets is known as a <i>formal</i> type parameter.
Question 5 : Is the following code snippet valid?
private final <T> genericObj;
- The code snippet is valid as its perfectly acceptable to use generics in a declaration.
Question 6 : In a generic class we can use a T formal type parameter within a declaration, constructor or method.?
- In a generic class we CAN we can use a <code>T</code> <i>formal type parameter</i> within a declaration, constructor or method.
Quiz Progress Bar Please select an answer

What's Next?

In the next quiz we test your knowledge of Bounded Type.



What's Next?

In the next quiz we test your knowledge of bounded types.