Collections/Generics QuizS2C Home « Collections/Generics Quiz

The questions in this last quiz on Java5 are on the topics covered in the Collections/Generics section of the site. The table below lists the lessons, a description of the lesson content and the the quiz question number range.

Lesson Summary

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

Beginning Java5 Lessons Description Question Range
Lesson 1 - Collections OverviewIn our first lesson on collections we look at the collections framework using various diagrams and explain the terminology used when dealing with collections.1 - 6
Lesson 2 - GenericsIn this lesson we unravel the terminology behind generics and look at some of the the strange syntax we get with it.7 - 14
Lesson 3 - SetsIn the first of three lessons on the different collection types within the Collection hierarchy we look at Sets.15 - 20
Lesson 4 - ListsIn our second lesson on the Collections hierarchy we look at the List interface and three of its concrete implementations.21 - 26
Lesson 5 - QueuesIn our final lesson on the Collections hierarchy we look at the Queue interface and its concrete implementation.27 - 31
Lesson 6 - MapsIn this lesson we look at the Map hierarchy, Map interface and four of its concrete implementations.32 - 39
Lesson 7 - UtilitiesIn this lesson we look at the Utilities hierarchy containing the java.util.Arrays and java.util.Collections classes.40 - 43
Lesson 8 - Sorting CollectionsIn our final look at collection we look at sorting our collections using the Comparable and Comparator interfaces.44 - 48

Java5 Quiz

The quiz below tests your knowledge of the material learnt in the Collections/Generics section of the site.

Question 1 : What are we referring to when we mention java.util.Collections
- The <code>java.util.Collections</code> class contains a lot of static utility methods we can use with our collections.
Question 2 : What type of collection would we use if we wanted no duplicates?
- The Set collection guarantess no duplicates.
Question 3 : What type of List is used with synchronized access?
- The Vector<E> List uses synchronized access.
Question 4 : What type of collection does not extend the Collection<E> interface?
- Although still considered as part of the Java Collections Framework, Map does not extend from the Collection<E> interface.
Question 5 : What type of Map is used with synchronized access?
- The Hashtable<K,V> Map uses synchronized access.
Question 6 : There are no direct implementation of the Collection<E> interface?
- There are no direct implementation of the Collection<E> interface, it is just extended by other interfaces.
Question 7 : What type of collection has always been type safe
- <code>Array</code> objects have always been implicitly declared with a type on array creation, so arrays have always been <em>type safe</em>.
Question 8 : What is the following code snippet an example of <T extends Number> ?
- <code>T extends Number</code> is an example of a <em>bounded type</em>.
Question 9 : We can use generics with any type?
- We can use generics with object types but not primitive types.
Question 10 : How are generics implemented?
- Generics are implemented by <em>erasure</em> (all generic code is removed at compile time) so legacy code can interoperate with generic code.
Question 11 : What is the following code snippet an example of <?> ?
- <code>?</code> is an example of an <em>unbounded wildcard type</em>.
Question 12 : Is the following code snippet valid? List<Object> aList = new ArrayList<String>();
- You may think the code snippet is valid as we know that <code>String</code> extends <code>Object</code> but this is not the case as generic types are <em>invariant</em>.
Question 13 : Is the following code snippet valid?

public class SimpleGenericInterfaceImpl implements SimpleGenericInterface<String> { ?
- The code snippet is valid as actual type specified for implementation, so class is not generic.
Question 14 : What is the following code snippet an example of <? extends Number> ?
- <code>? extends Number</code> is an example of a <em>bounded wildcard type</em>.
Question 15 : What do sets care about
- All sets care about uniqueness.
Question 16 : What type of set would you use if you wanted a fast access ordered set but didn't care about sorting?
- If you wanted a fast access ordered set but didn't care about sorting you would use a LinkedHashSet.
Question 17 : Can we have a sorted and unordered set?
- If a set is sorted, as a TreeSet is, it will always be ordered by natural or comparator order.
Question 18 : What type of set would you use if you wanted fast access and didn't care about ordering?
- If you wanted a fast access set and didn't care about ordering you should use a HashSet.
Question 19 : What type of exception is raised when we run a program with objects that are not mutually comparable in a sorted set?
- When we try to run a program with objects that are not mutually comparable in a sorted set (TreeSet) we get a ClassCastException exception.
Question 20 : What type of set would you use if you wanted it sorted?
- If you wanted a sorted set you would use a TreeSet.
Question 21 : Which type of list implements the Queue interface
- <code>LinkedList</code> implements the <code>Queue</code> interface.
Question 22 : What type of collections are Lists?
- Lists are ordered collections.
Question 23 : Which List class is synchronized?
- The Vector class is synchronized.
Question 24 : What do Lists have in common?
- All lists have an index.
Question 25 : Which List would you use if wanted fast access and were doing lots of insertions and deletions?
- The LinkedList is the best List choice when you want fast access and are doing lots of insertions and deletion.
Question 26 : You can't put null elements into Lists in the API?
- All concrete implementations of Lists in the APIs allow null elements.
Question 27 : You can't put null elements into Queues in the API?
- All concrete implementations of Queues in the APIs do now allow null elements.
Question 28 : What type of ordering does a PriorityQueue have?
- A PriorityQueue is ordered and sorted.
Question 29 : What type of Queue is a PriorityQueue?
- A PriorityQueue is a PIPO (priority-in, priority-out) Queue.
Question 30 : A PriorityQueue is always sorted in natural order?
- A PriorityQueue defaults to natural order but can also be sorted using a custom comparator.
Question 31 : Within a PriorityQueue the elements sorted last are processed first?
- Within a PriorityQueue the elements sorted FIRST are processed first.
Question 32 : What do maps care about
- All maps care about uniqueness, which is achieved via their key.
Question 33 : What type of map would you use if you wanted a fast access ordered map but didn't care about sorting?
- If you wanted a fast access ordered map but didn't care about sorting you would use a LinkedHashMap.
Question 34 : Can we have a sorted and unordered map?
- If a map is sorted, as a TreeMap is, it will always be ordered by natural or comparator order.
Question 35 : What type of map would you use if you wanted fast access and didn't care about ordering?
- If you wanted a fast access set and didn't care about ordering you should use a HashMap.
Question 36 : What type of exception is raised when we try run a program with objects that are not mutually comparable in a sorted map?
- When we try to run a program with objects that are not mutually comparable in a sorted map (TreeMap) we get a ClassCastException exception.
Question 37 : What type of map would you use if you wanted it sorted?
- If you wanted a sorted map you would use a TreeMap.
Question 38 : Which static nested class of Map<K,V> allows us to get a reference to a mapping via an iterator?
- The Map.Entry static nested class allows us to get a reference to a map entry via an iterator.
Question 39 : What type of map has synchronized methods?
- Hashtable contains synchronized methods.
Question 40 : What type of variables can we use with the java.util.Arrays class?
- We can use both primitive and reference variables with the <code>java.util.Arrays</code> class.
Question 41 : If two arrays hold the same elements they are considered equal by the Arrays.equals() method?
- The elements would have to be in the same order as well or the hashcodes would be different and therefore the arrays would be unequal.
Question 42 : We can use any object with the methods of the java.util.Arrays class?
- All the methods in the <code>java.util.Arrays</code> class accept <code>Object</code>, so yes we can use any object.
Question 43 : What type of variables can we use with the java.util.Collections Class?
- We can only use reference variables with the <code>java.util.Collections</code> class.
Question 44 : Which method do we need to implement in the java.lang.Comparable interface?
- We need to implement the <code>compareTo()</code> method in the <code>java.lang.Comparable</code> interface.
Question 45 : Which interface would we use to sort a class that can't be modified?
- We would use the <code>java.util.Comparator</code> interface and write a comparator class we can use in the <code>sort()</code> method constructor.
Question 46 : Which method do we need to implement in the java.util.Comparator interface?
- We need to implement the <code>compare()</code> method in the <code>java.util.Comparator</code> interface.
Question 47 : How many ways does the java.lang.Comparable interface allow us to sort a collection?
- The <code>java.lang.Comparable</code> interface has to be included with the class being sorted, so can only be sorted one way.
Question 48 : What is returned from both the compare() and compareTo() methods?
- The <code>compare()</code> and <code>compareTo()</code> methods return an <code>int</code> denoting the comparison result.
Quiz Progress Bar Please select an answer

What's Next?

That's the end of the Java5 quizzes, the next quiz is about HTML 4.01.

go to home page Homepage go to top of page Top