COP 3337 Section U05 Spring 2017 PRACTICE FINAL EXAM =================== INSTRUCTIONS ------------ 1. There are 5 questions on the test for a total of 96 points. 2. You have 2 hours to work on the exam. 3. The exam is CLOSED book, CLOSED notebook. You CANNOT use the practice test, the answer to the practice test and your programs. 4. You cannot use cell phones, calculators, or computers. 5. If you do not understand the meaning of a question, ask me during the exam. 6. Write all your answers in the exam book. 7. Good luck! NAME: _________________________________________________________________ PANTHER ID: ___________________________________________________________ Question 1. (30 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, 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 trues if l1 is the empty list. Write the method below. Use good style and put comments. Question 2. (15 points) We define an ordering on the set of strings as follows: The string s is less than the string t if 1. s is shorter than t, or 2. s and t have the same length and t precedes s in alphabetical order. Write the method public static void sort(String[] arr) that sorts array according to this ordering. If arr is null throw a null pointer exception. If arr has 0 or 1 items, do nothing. You may use the class Arrays. Question 3. (15 points) Write the method public static > T intermediateValue(T a, T b, T c) that returns the intermediate value of of a, b, c. For example, if the a, b, c are the numbers 5, 7, 2, the intermediate value is 5 because 2 <= 5 <= 7. Do not sort the 3 numbers, just compare them. Write your answer below. Question 4. (12 points) Each of the following programs will yield an exception. Write the exception below the program. Be very specific. Program a: import java.util.*; public class Exceptions { // test exceptions public static void main(String[] args) { ArrayList pile = new ArrayList<>(); pile.add(2,20); // add at position 2 } } The exception is : ____________________________________ Program b: import java.util.*; public class Exceptions { // test exceptions public static void main(String[] args) { LinkedList list = new LinkedList<>(); list.add("Bill"); ListIterator it = list.listIterator(); it.remove(); } } The exception is : ____________________________________ Program c: import java.util.*; public class Exceptions { // test exceptions public static void main(String[] args) { // find the quotient of 2 integers Scanner scan = new Scanner(System.in); System.out.print("Enter the divident > "); int m = scan.nextInt(); System.out.print("Enter the divisor > "); int n = scan.nextInt(); System.out.print("The quotient is " + m / n); } } This is what the user entered Enter the divident > 10 Enter the divisor > a The exception is : ____________________________________ Program d: import java.util.*; import java.io.*; // display the words in the Gargamel file public class Exceptions { // test exceptions public static void main(String[] args) throws IOException { File inFile = new File("U:/gargamel.txt"); Scanner scan = new Scanner(inFile); while ( scan.hasNext()) { String out = scan.next(); System.out.println(out); } } } If the file "gargamel.txt" is not in the "U" directory, the exception is : ____________________________________ Program e: import java.util.*; // find the size of a file public class Exceptions { // test exceptions public static void main(String[] args) { LinkedList list = null; System.out.println("The size of list is " + list.size()); } } The exception is : ____________________________________ Program f: import java.util.*; // find the size of a file public class Exceptions { // test exceptions public static void main(String[] args) { // read 2 numbers from a list LinkedList list = new LinkedList<>(); list.add(10); list.add(20); ListIterator it = list.listIterator(1); int number1 = it.next(); int number2 = it.next(); } } The exception is : ____________________________________ Question 5. (24 points) Complete the following sentences with the words that make them true. There is no penalty for wrong guesses, but circle only one choice per sentence. 1. The ... is a section of code that responds to exceptions. a. exception generator b. exception manipulator c. exception handler d. exception monitor 2. FileNotFoundException inherits from ... a. Error b. IOException c. JavaException d. FileException 3. The ... informs the compiler of the exceptions that could get thrown from a method. a. throws clause b. parameter list c. catch clause d. method return type 4. You use this statement to throw an exception manually. a. try b. generate c. throw d. System.exit(0) 5. In the generic type notation T is ... a. constrained to an upper bound. b. constrained to a lower bound. c. constrained to an upper and a lower bound. d. not constrained. 6. The process used by the Java compiler to remove generic notation and substitute actual parameters for type parameters is known as ... a. erasure b. removal c. substitution d. masking 7. ListIterator is a subinterface of ... a. Iterator b. Collection c. Comparable 8. The ... makes several passes through the array and causes the larger values to gradually move towards the end of the array with each pass. a. bubble sort b. selection sort c. merge sort d. Quicksort 9. The ... repeatedly divides the array into 2 equal or almost equal halves. a. bubble sort b. selection sort c. merge sort d. Quicksort 10. This ListIterator method replaces an existing element with a new element. a. replace b. update c. set d. add 11. The ... is the part of recursive program that can be solved without recursion. a. base case b. solvable case c. known case d. iterative case 12. A method is called once from the main method and then calls itself 4 times. The depth of the recursion is ... a. 1 b. 4 c. 5 d. 9