Java Programming: Final Examination 姓名:______________ 學號:___Solution________ I (40 pts) 是非題 XXOXO XOOOO OOXXO OOOXX 1. 6/26 2015 分數:______________ After defining a generic class such as List<E>, we will use many instances of it like List<String>, List<Number> etc. Hence the Java compiler will create different bytecode for each instance of it. 2. Let C be a Java class which overrides the equals(Object) and hashCode() methods of the Object class. Now if c1 and c2 are both instances of C, then, according to the java Object contract, it is admissible that c1.equals(c2) returns true while c1.hashCode() and c2.hashCode() have different values. 3. If t is an instance of java class A and A implements interface C, then t is also an instance of C. 4. A concrete class may leave some abstract methods of its super class unimplemented. 5. After compiled into Java byte code, every java class must have at least one constructor even if it is abstract. 6. The line: List<String> x = new List<>(); is a legal java statement. 7. Integer[] is a subtype of Number[]. 8. int[] is a subtype of Object. 9. If A is a class with a protected method m(), we can override m() with a new public m() in a subclass of A. 10. We can change the content of a java.lang.StringBuffer instances after it has been created. 11. In the body of a static method m of a class, we cannot access an instance variable x of the same class by using the expression this.x. 12. We should use LinkedList instead of ArrayList if we need to insert or remove elements of a list frequently. 13. Suppose the method m has the head: int m( List<Object> x). We call it using m(new ArrayList<Integer>() ). 14. Suppose the method m has the head: int m( List<? super Number> x). Then we can call it using m( new ArrayList<Integer>() ). 15. List<Integer> is a subtype of List<Integer>. 16. For a method with the head: int m( List<?> x), we can call it using m( new ArrayList<Integer>() ). 17. ArrayList<Integer> is a subtype of List<Integer>. 18. Every java class has at most one parent class. 19. Every java interface can extend at most one interface. 20. Every abstract class must have at least one abstract method. II (50 pts) 單選題 ECACC DADDB BDDDC DADCC 21. What is the value of list1 after executing list1.retainAll(list2) if list1 has content [1,2,3,4] and list2 has contents[1,3,5]? (a). [5] (b) [1] (c) [1,2,3,4,5] (d) [2,4] Answer Question 22-23 according to the following code: Map<String, Integer> map = new LinkedHashMap<>(); map.put(“D”, 12), map.put(“B”,11), map.put(“D”, 45), map.put(“A”, 32); 1 (e) [1,3] 22. What is the value of the invocation: map.size() ? (a). 5 (b) 4 (c) 3 (d) 2 23. What is the value of the code: map.keys().iterator().next() ? (a). "D" (b) 12 (c) "A" (d) 32 (e) "B" 24. What is the value of the java expression: "book".split("o").length (a). 1 (b) 2 (c) 3 (d) 4 (e) 5 25. To declare a generic class named A with one or more type parameters, use (a). public class A(E) { ... } (c) (b) public class A(E, F) { ... } public class A<E, F> { ... } (d) public class A<? extends E> { ... } 26. Which of the following is the head of a legal declaration of a method with variable-length argument? (a). public void m(String … s, double… ns ) (b). public void m(double … ns, String s) (c). public double … m(String s, double ns) (d). public double m(String s, double … ns) 27. Suppose class A extends class B, class B extends class C, and all 3 classes declare a public field f. In order for program inside class A to refer to the field f declared at class C, which of the following expressions can be used : (a) ((C) this). f (b) super.super.f (c) C.super.f (d) (C) f 28. Suppose class A extends class B, class B extends class C, and all 3 classes declare a public field m(). In order for program inside class A to refer to the method m() declared at class C, which of the following expressions can be used : (a) super.super.m() (b) (C (this)). m() (c) super.m() (d) none of the above 29. What kind of error would occur in the following code? 1. Class Apple extends Fruit {} 2. Class Fruit { 3. public static void main(String[] args) { 4. Object fruit = new Apple(); 5. Apple apple = fruit; }} (a) runtime-time error at line 4 (b) compile-time error at line 4 (d) compile-time error at line 5 (e) no error (c) run-time at line 5 30. What is the output of the following program? class Test { public static void main(String[] args) { try { System.out.print("A"); int i = 0; int y = 2 / i; System.out.print("B"); } finally { System.out.print("C"); }}} (a). "A", then an error message. (b) "AC", then an error message. 2 (c) "ABC", then an error message. (d) An error message only. 31. Polymorphism means that ________. (a). a class can contain another class (b) subtype objects can be used in place of supertype objects. (c) (d) a class can extend another class data fields should be declared private 32. Which of the following statements will not throw an exception? (a). int (c) x= (new int[3]) [4]; c = “abcd”.charAt(-1); (b) char double d = 10 / 0; (d) int x= Integer.MAX_VALUE + Integer.MAX_VALUE ; 33. Given the following code: String s1 = “AB" + "C” ; String s2 = new String(“ABC”); String s3 = s2.intern(); Which of the following expressions evaluates to true? (a) s1 == s2 (b) s1 == s3 (c) s2 == s3 (d) none of the above 34. The visibility of Java modifiers decrease in which of the following order: (a). private, package, protected, public. (b) public, package, protected, private. (c) public, protected, package, private. package, private, protected,public. (d) 35. Suppose A is an abstract class, B is a concrete subclass of A, C is a subclass of B, and both A and B have a default constructor. Which of the following is correct? (a). A a = new A(); (b) B b = new A(); (c) A a = new B(); (d) C c = new B(); 36. Which of the following is a checked exception? (a). java.lang.IllegalArgumentException (b) java.lang.RuntimeException (c) (d) java.lang.Exception java.lang.ArithmeticException 37. Variables that are shared by every instances of a class are called ________. (a). class variables (b) public variables (c) instance variables (d) private variables 38. Which of the following classes cannot be extended? (a). class A { private A();} (b) class A { } (c) class A { protected A();} (d) final class A { } 39. Which of the following classes are NOT immutable? (a). Integer (b) String (c) ArrayList (d) BigInteger 40. Suppose that class Foo is defined as follow and let f be an instance of Foo: public class Foo { int i; static String s; void getI() {}; static void gets(){} } Then which of the following invocation statement is NOT correct? (a). f.getI(); (b) f.getS(); (c) Foo.getI(); (4) Foo.getS(); III (50 pts) 程式設計 1. [20 pts] The String class is provided in the Java library. Provide your own implementation using char[] as internal data structure for the methods listed below. The new class is named MyString. public class MyString { private char[] chars ; 3 public MyString(char[] chars ) { _____this.chars = chars;__________ } public MyString() { /* new empty string */ ____chars = new char[0]; /* or this(new char[0] ); */____ } public char charAt(int I ) { return chars[i] ; } public int length () { ______return chars.length;_____________________ } public MyString subString(int f, int t){ char[] rlt = new char[t-f] ; for(int k = f; k < t; k++) { rlt[k-f] = chars[k] ; } // or System.arraycopy(chars, t, rlt, 0, t- f) ; return new MyString(rlt) ; } public int compare(MyString s) { int len = Math.min(chars.length, s.length()); for(int k = 0; k < len; k++){ if(chars[k] != s.chars[k]) return chars[k] – s.chars[k] ; } return chars.length - s.length() } public static MyString valueOf(int t) { String s = String.valueOf(t) ; // or simply chars chars = s.toCharArray(); char[] chars = new char[s.length()]; for(int k = 0; k < s.length(); k++){ chars[k] = s.charAt(k); } return new MyString(chars) ; } 2. } [15pts] Using Java to create two sets of Integers s1 = { 1, 2, 3, 4, 5 } and s2 = {2, 4, 6,7 }, and then find their union s3, intersection s4 and symmetric difference s5 = (s1 - s2) (s2-s1). Note that the contents of s1 and s2 should not be changed by the computation. public class Main { pubic static void main(String[] args ) { Set<Integer> s1 = new HashSet<>( Arrays.asList(1,2,3,4,5)) ; Set<Integer> s2 = new HashSet<>(Arrays.asList(2,4,6,7)); Set<Integer> s3 = new HashSet<>(s1); s3.addAll(s2); Set<Integer> s4 = new HashSet<>(s1); s4.retainAll(s2); Set<Integer> s5 = new HashSet<>(s3); s5.removeAll(s4); } 4 3. [15pts] Implement a Java method to find all non-negative integer solutions to the equality a0x0+a1x1 + …+ an-1xn-1 = s where s is a positive integer. Suppose the coefficients a0,a1,…,an-1 are all positive integers and stored in an input array a and every solution (v1,v2,…,vn-1) to it is represented as a list [v1,v2,…,vn] of Integer. public static List<List<Integer>> allSolutions(int[] a, int s) { // assume a.length >= 1. return allSolutions(a, a.length-1, s); } // Find all solutions to a0x0 + … + amxm = s, where m <= a.length - 1. public static List<List<Integer>> allSolutions(int[] a, int m, int s) { // m <= a.length -1 ; List<List<Integer>> rlt = new ArrayList<>(); if(m == 0){ // basis case: m = 0. i.e., find solution to a0x0 = s __if( s % a[0] != 0) return rlt;___________ ; __List<Integer> sol = new ArrayList<>(); sol .add(s / a[0] ) ; rlt .add( sol)_ ; return rlt; } else { // inductive case: m > 0. // Find solutions to (a0x0 + … + am-1xm-1)+ amxm = s // Note: (v0,…,vm-1, vm) is a solution to (a0x0 + … + amxm) = s iff there is // amxm = t and (v0,…,vm-1) is a solution to (a0x0 + … + am-1xm-1) = s-t. for(int k = 0; a[m]* k <= s; k++ ) { List<List<Integer>> subSolutions = allSolutions(a, m -1, s – a[m]* k ) ; for(List<Integer> sol : subSolutions) sol.add(k) ; rlt.addAll(subSolutions); } return rlt; }} 5 0 <= t <= s such that