Raw/Generic Type QuizJ8 Home « Raw/Generic Type Quiz

The questions in this quiz on Java are on the topics covered in the Generics section of the site. The table below lists the lesson used for each quiz, a description of the lesson content and the quiz number and questions associated with that lesson.

Lesson Summary

Click on a lesson in the table to go to that lesson for a refresher on the topics for that quiz.

Click on a quiz in the table to go straight to the quiz for a particular lesson.

Generics Lessons Description Quiz Info.
Lesson 1 - Generic BasicsIn our first lesson on on generics we unravel the terminology behind java generics and look at some of the strange syntax we get when using it.Quiz1
7 questions
Lesson 2 - Raw/Generic Type ComparisonIn this lesson we compare raw and generic types to see the advantages of using generics.This quiz.
6 questions
Lesson 3 - Generic InterfacesIn this lesson we look at generic interfaces and how to apply them in our Java code.Quiz3
6 questions
Lesson 4 - Generic ClassesThis lesson is about generic classes.Quiz4
6 questions
Lesson 5 - Bounded TypeIn this lesson on Java generics we investigate invariance and Bounded Type.Quiz5
7 questions
Lesson 6 - Unbounded Wildcard TypeIn this lesson we learn about generic Unbounded Wildcard Type.Quiz6
6 questions
Lesson 7 - Upper Bounded Wildcard TypeThis lesson on generics is about upper bounded wildcard types.Quiz7
6 questions
Lesson 8 - Lower Bounded Wildcard TypeThis lesson on generics is about lower bounded wildcard types.Quiz8
6 questions
Lesson 9 - Generic MethodsThis lesson is about generic methods.Quiz9
6 questions
Lesson 10 - Generic ConstructorsIn our final lesson on generics we look at generic constructors.Quiz10
7 questions

Generics Quiz 1

The quiz below tests your knowledge of the material learnt in Generics - Lesson 2 - Raw/Generic Type Comparison.

Question 1 : What will the compiler do with the following code  private static final List names = new ArrayList();
- The code compiles but with warning messages for raw types and unchecked calls.
Question 2 : Which is the preferred syntax?
- <code>private static final List<String> names = new ArrayList<>();</code> is preferred as it uses the diamond operator which answer B does not. Answer C uses a raw type and is unsafe.
Question 3 : What happens when we try to compile the following class?
package info.java8;
import java.util.*;

public class RawTypeNameCollection {
    private static final List<String> names = new ArrayList<>();

    public static void main (String[] args) {
        names.add("Bill");
        names.add("Ben");
        names.add(6);
    }
}
- The code will not compile as the compiler stops the <code>int</code> from being put into the <code>String</code> collection.
Question 4 : We can mix raw types and generics
- We CAN mix raw types and generics although this is in no way recommended as it can lead to unsafe code and runtime exceptions.
Question 5 : What happens when we try to compile the following class?
package info.java8;
import java.util.*;

public class RawTypeNameCollection {
    private static final List<String> names = new ArrayList<>();

    public static void main (String[] args) {
        names.add("Bill");
        names.add("Ben");
        names.add("6");
    }
}
- The code compiles normally as we are just putting <code>String</code> objects into a <code>String</code> collection.
Question 6 : Which of the following syntax will compile?
private static final List<String> names = new ArrayList<>(); //1

private static final List<String> names = new ArrayList<String>(); //2

private static final List<String> names = new ArrayList(); //3
- All the syntax is valid although answer B does not use the diamond operator and answer C uses a raw type and is unsafe.
Quiz Progress Bar Please select an answer

What's Next?

In the next quiz we test your knowledge of generic interfaces.