Self-assessment 04: Sample questions

The self-assessment will have three questions, and cover material through the end of the Sets.

In the first question, you will write a comparator. For example, a question might state:

Write a class DescendingIntegerComparator that implements Comparator<Integer>. If it were used as the argument to List.sort(), it would sort the list in descending order. Note that this question is not asking you to sort a list – just to write the comparator!

For example, if the unsorted list were [2, 1, 4, 3], then the list would be [4, 3, 2, 1] after sorting using this comparator.


In the second and third questions, you will write a short class or method according to a textual description. (Yes, these are the same as last week, though the quiz questions will differ and likely involve Set a little more.) For example, a question might state:

Write a method public static <E> List<E> notIn(List<E> list, Set<E> set). notIn should return a list that contains the elements present in list but not present in set. The original list and set must not be modified. For full credit, your method must correctly use generics, and must not contain unnecessary method calls or loops, even if they do not otherwise impact correctness. You may assume List, Set, ArrayList, and HashSet are correctly imported from java.util. You may not import other classes.


Another question might state:

Write a generic class MyList<E> that extends an existing implementation of List. MyList should include a method public List<E> reversed(). reversed returns a new list consisting of the elements of the current list in reverse order. reversed must not modify the list. For full credit, your method must correctly use generic types, and must not contain unnecessary method calls or loops, even if they do not otherwise impact correctness. You may assume List and ArrayList are correctly imported from java.util. You may not import other classes.

Check your answers here.