Quiz 03 sample questions

The quiz will have two questions; both will require you to write a short method or class. Here are two examples of the kinds of questions you’ll be asked:

Suppose you have a class with an instance variable evensSet of type Set<Integer>. The value of evensSet is non-null. Write an instance method with signature void addEvens(int low, int high) that adds to evenSet the even integers between low and high, inclusive.

Write a static method with signature void updateCount(Map<Variety, Integer> varietyCount, Variety banana, int count). updateCount should increase the Integer associated in the map with the given banana variety by the amount count (or set it equal to count if banana is not yet in the map). Assume varietyCount and banana are non-null, and that Variety has meaningful equals and hashCode methods.

State the approximate worst-case running time for each of the following:

  • ArrayList.add
  • HashSet.size

You can check your answers in Eclipse, or here.