COP 3337 Section U05 Spring 2017 PRACTICE MIDTERM EXAM ===================== INSTRUCTIONS ------------ 1. There are 4 questions on the test for a total of 80 points. 2. You have 1 hour and 15 minutes to work on the exam. 3. The exam is closed book, closed notebook. 4. You cannot use the practice test. 5. You cannot use cell phones, calculators, or computers. 6. Write all your answer on the exam sheet. 7. If you need to go to the bathroom, do it before taking the test. 8. Good luck! 9. Write your name and your student number below. NAME: _________________________________________________________________ PANTHER ID: ___________________________________________________________ QUESTIONS --------- Question 1. (15 points) The concrete class Car implements the interface Vehicle. Write the method public static double CarPercentage(Vehicle[] arr) that returns the percentage of cars in arr. If arr is null or empty throw an illegal argument exception. Write your program below. Don't forget to use good style and to put comments. Question 2. (15 points) What will be printed out by the program below? Do not show your work, just write the answer below. public class Question2 { public static void main(String[] args) { System.out.println("mysteryF(3,5) = " + mysteryF(3,5)); } // the mystery function public static int mysteryF(int m, int n) { if (m < 0 || n < 0) throw new IllegalArgumentException("Negative inputs"); if (n < m) return 0; if (m == 0 || m == n) return 1; return mysteryF(m,n-1) + mysteryF(m-1,n-1); } } // end Question 2 Write your answer beneath. mysteryF(3,5) = _____________________________ Question 3. (30 points) We define the Navlakha strings as follows: 1. abab is a Navlakha string. 2. bab is a Navlakha string. 3. If S is a Navlakha string, then aSSb is a Navlakha string. 4. If U and V are two Navlakha strings, then bUV is a Navlakha string. In rule 4, U and V may be equal or not. In rule 3, the 2 S's are replaced by identical strings. For example, 5. abab is Navlakha by rule 1. 6. Let S = abab. Then aSSb = aababababb is Navlakha. 7. Let U = bab and V = abab. Then bUV = bbababab is Navlakha by rule 4. 8. Let U = bab and V = bab. Then bUV = bbabbab is Navlakha by rule 4 Write the method public static boolean isNavlakha(String in) that returns true if in is a Navlakha string and false if the string is not Navlakha. Write your program below. Question 4. (20 points) For each of the following statements write T if the statement is true and F if it is false. All statements pertain to Java. Do not correct the statements, just write T or F in front of the statement number. 1. If classes C1 and C2 realize the interface Inter, then C1 and C2 are compatible. 2. A class can have both static and object methods. 3. If Inter is an interface, then we cannot define arrays of type Inter . 4. If the classes C1 and C2 are compatible, so are the classes C1[] and C2[]. 5. Let o1 and o2 two objects belonging to the same class. Then o1 == o2 is true only if o1 and o2 point to the same object. 6. In Java the parameters are passed by value. 7. public is the default access type. 8. The program below causes an ArrayListIndexOutOfBoundsException. ArrayList list = new ArrayList<>(); list.add(1); list.add(2); list.add(2,3); 9. If name is a string and it is not null, then its length is given by name.length . 10. System.out is a method in the class System. 11. public boolean equals(Object o) and public String toString() are two methods of the class Object. 12. Math.cos(double a) is an object method. 13. The value of the integer expression 2 + 3 * 2 is 10 . 14. The boolean expression o instanceof C is true if the object o belongs to a subclass of C. 15. We can extend the class String. 16. The method String.compareTo(String s) returns a boolean value. 17. Let C2 be a subclass of C1 and o an object of type C1. We need casting to convert o to type C2. 18. In Java a subclass can have 2 different parent classes. 19. The last location of the array a defined below has index 20. int[] a = new int[20]; 20. We can convert the string s to int by using the statement int i = Integer.parseInt(s);