COP 3337 Section U05 Spring 2017 PRACTICE MIDTERM EXAM ANSWERS ============================= Question 1. (15 points) // return the percentage of cars in the array public static double CarPercentage(Vehicle[] arr) { // check for empty array if (arr == null || arr.length == 0) throw new IllegalArgumentException("Empty array"); int cars = 0; // the number of cars; // count the number of cars for (Vehicle v : arr) if (v instanceof Car) cars++; // compute the percentage return (double) cars / arr.length * 100.00; } Question 2. (15 points) mysteryF(3,5) = 10 Question 3. (30 points) // the isNavlakha method recognizes the Navlakha // strings public static boolean isNavlakha(String in) { if (in == null) return false; // the base cases if (in.equals("abab") || in.equals("bab")) return true; int len = in.length(); // the base cases are the only Navlakha strings // shorter than 7 and every Navlakha string ends in b if (len < 7 || in.charAt(len-1) != 'b') return false; // treat case 3 if (in.charAt(0)=='a' && len % 2 == 0) { // get the 2 S's int mid = len/2; String s1 = in.substring(1,mid); String s2 = in.substring(mid,len-1); if (s1.equals(s2) && isNavlakha(s1)) return true; } // check rule 4 if (in.charAt(0) == 'b') for (int i = 4; i < len -3; i++) { // extract u and v String u = in.substring(1,i); String v = in.substring(i); if (isNavlakha(u) && isNavlakha(v)) return true; } // all 4 cases failed return false; } Question 4. (20 points) 1. F 2. T 3. F 4. T 5. T 6. T 7. F 8. F 9. F 10. F 11. T 12. F 13. F 14. T 15. F 16. F 17. T 18. F 19. F 20. T