COP 3337 Section U03 Spring 2017 PRACTICE FINAL EXAM ANSWERS =========================== Question 1. (25 points) Write the method public static boolean isSublist(LinkedList l1, LinkedList l2) that takes as input two lists, l1 and l2, and returns true if l1 is a sublist of l2 and false if it is not. We say that l1 is a subslist of l2 if the elements of l1 occur in l2 in the same order in which they occur in l1, though the elements that are adjacent in l1 may not be adjacent in l2. For example, a, b, c is a sublist of b, a, c, b, a, c, b - - - because the first list is formed by taking the underlined items of the second list. On the other hand a, b, a is not a sublist of b, a, c, c, b, c Let's see why. The second list must contain an a followed by, not necessarily immediately, by a b, and the b must be followed by an a. So, we must take the first a, and then the b that follows it. However, the b is not followed by an a, as shown below b, a, c, c, b, c - - The method throws a null pointer exception if either l1 or l2 or both are null and return true if l1 is the empty list. Write the method below. Use good style and put comments. // return true if l1 is a sublist of sublist of l2 // l1 is a sublist of list if the items of sub occur in // the same order in l2. For example, a, b, c is a sublist of l2 if // there are 3 items with the values a, b, and c in this order // in list such that a precedes a and b precedes c. // throw a null pointer exceptione if either list is null public static > boolean sublist(LinkedList l1, LinkedList l2) { if (l1 == null || l2 == null) throw new NullPointerException("Null list"); if ( l1.size() == 0) return true; // l1 is empty if (l2.size() == 0) return false; // l1 is not empty, but l2 is // get two iterators, one for list and one for sub ListIterator it1 = sub.listIterator()B; ListIterator it2 = list.listIterator(); while (it1.hasNext()) { // get the next item in sublist T item = it1.next(); boolean found = false; // true if found in list1 // look for item in list starting with the current position while (it2.hasNext()) { T curr = it2.next(); if ( curr == null && item == null || curr.equals(item)) { found = true; break; } } if (!found) return false; } return true; } Question 2. (10 points) public static void sort(String[] names) { // define an inner comparator class class NameComparator implements Comparator { public int compare(String s, String t) { if (s.length() > t.length()) return 1; if (t.length() > s.length()) return -1; return t.compareTo(s); // line ** } } Arrays.sort(names, new NameComparator()); } Question 3. (10 points) public static > T intermediateValue(T a, T b, T c) { // a number is the intermediate value if it is // <= one of the remaining 2 and // >= the other remaining number if ( a.compareTo(b) <= 0 && a.compareTo(c) >= 0 || a.compareTo(c) <= 0 && a.compareTo(b) >= 0) return a; if ( b.compareTo(c) <= 0 && b.compareTo(a) >= 0 || b.compareTo(a) <= 0 && b.compareTo(c) >= 0) return b; return c; } // the second problem is more elegant, but it can run into trouble if // one of the products causes an overflow. public static > T intermediateValue(T a, T b, T c) { // a number is the intermediate value if it is // <= one of the remaining 2 and // >= the other remaining number if ( (a.compareTo(b) * a.compareTo(c)) <= 0) return a; if ( (b.compareTo(c) * b.compareTo(c)) <= 0) return b; return c; } Question 4. (10 points) a. IndexOutOfBoundsException b. IllegalStateException c. InputMismatchException d. FileNotFoundException e. NullPointerException f. NoSuchElementException Question 5. (24 points) 1. c 2. b 3. a 4. c 5. b 6. a 7. a 8. a 9. c 10. c 11. a 12. b