Sample Questions for Quiz 2 CSCI 1902, Spring 2008 This is a set of sample questions for Quiz 2. It is intended to give you a sense of what type of questions to expect on the exam; in particular, it illustrates the different "levels of knowledge" of exam questions. There are several things this set of questions is *not*. (1) It is not a comprehensive list of the topics that may be covered. The best source for that is the lecture notes. (2) It is not a sample quiz: the set of questions below would take longer than 25 minutes to complete (3) It is not (syntactically speaking) an example of a successful "Quiz Question Proposal"; it contains more questions than necessary and no answers. ============================== == KNOWLEDGE ============================== Name three methods in the ADT List class. -------------------- Write a Java statement to declare a variable whose value is a List of Customer objects. -------------------- ============================== == COMPREHENSION / APPLICATION ============================== Name one advantage of using a parameterized list, i.e., a list that can only take values of a certain type, over a list that can take values of any type. -------------------- What is the difference between a Java class and a Java interface? -------------------- Given this partially-written implementation of an Array-based list class, you are asked to write the reverse() method, which will reverse the order of items in the list. Complete this method. You may not assume that anything other than what is here is implemented, but you may add methods if you feel you need them. (Hint: you shouldn't need to write more methods.) public class ArrayList { Object[] elements; int index; public ArrayList() { elements = new Object[20]; index = 0; } public void add(Object obj) { elements[index] = obj; index++; } public Object removeLast() { index--; return elements[index+1]; } public void reverse() { // add your code here. } } -------------------- Given the following definition of a Customer, write a compareTo method that will order Customers by their current account balances. public class Customer implements Comparable { String name; double balance; ... // Write the compareTo method here } ============================== == PROBLEM SOLVING ============================== Suppose you're writing a program that stores a number of objects. Give one reason you would use an array rather than a list to store the objects. -------------------- Write a method findLowBalances() for the Customer class that takes a list of Customers and a (double) threshold and returns a new list consisting of all the Customers in the input list whose balance is less than the threshold. -------------------- Suppose you're writing a geographical information system. Among other things, the program needs to keep track of information about the 7 continents -- Africa, Antarctica, Asia, Australia, Europe, North America, South America -- including their area, population, and countries. What Java entities would you use to represent the continents? Explain your answer. -------------------- Write a method sameEnds(int[] nums, int n) that returns true if the sequence of N numbers at the start and end of nums are the same. For example, with nums = {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3. You may assume 0 <= n <= nums.length.