Java Programming: Final Examination 姓名:______________ 學號:__Solution_____ 6/14 2013 分數:______________ I (40 pts) 是非題 (ANS: xxoox xoxoo xoooo ooxxo ) 1. After compilation some abstract Java classes may have no constructor. 2. Since a generic class/interface like Set<E> has many instantiations such as Set<String>, Set<Integer> etc., it will be compiled into many java bytecode classes. 3. A Java class can extend one superclass but can implement more than one interface. 4. We should use ArrayList instead of LinkedList if we need to randomly access or update element contents of a list frequently. 5. We cannot change the content of a java object of the type java.lang.StringBuffer after it has been created. 6. If A is a class with a public method m(), we can override m() with a new protected m() in a subclass of A. 7. If a catch block in a try-statement can catch instances of an exception class, it can also catch instances of all its subclasses. 8. The code in the finally block of a try-statement will be executed only if the statements in the try-statement are executed normally, i.e., without causing any exception. 9. When declaring a method, we have to declare a checked exception if it might be thrown from within the method, which is unable to catch it. 10. If method m has the head: int m( Object[] x), then we can call it using m( new String[]{}). 11. If method m has head: int m( String[] x), then we call it using m( new Object[10] ). 12. If method m has head: int m( Object x), then we call it using m( new int[]{1,2,3} ). 13. A class need not implement all methods in the interfaces which it implements. 14. Inside a static method m of a class, we cannot use the keyword this. 15. According to Java Object contract, if two Java objects x and y have different hash codes (i.e., x.hashCode() != y.hashCode() ) then they must not be equal (i.e., x.equals(y) == false) . 16. If method m has head: int m( Comparable x), then we can call it using m( "abc" ). 17. Suppose the method m has the head: int m( HashSet<? extends Number> x). Then we can call it using m( new HashSet<Integer>() ). 18. Suppose the method m has the head: int m( Set<? super Number> x). Then we can call it using m( new HashSet<Integer>() ). 19. There is no error in the code: java.util.TreeSet s = new java.util.TreeSet(); s.add("abc"); s.add(new Object() ); 20. There is no error in the code: java.util.HashSet s = new java.util.HashSet(); s.add("abc"); s.add(new Integer(1)); II (50 pts) 單選與填充 (ANS:bcccb bbccc bbbac cdbba c ) Answer Question 1-3 according to the following assumption: Suppose that list1 is a list that contains the Integer objects 1, 3, 3, 5 and that list2 is another list that contains the Integer objects 3,4. Note we use [1, 3, 3, 5] to denote a list containing the sequence 1,3,3, 5 of elements. 1 1. What is list1 after executing list1.addAll(list2)? (a).[1, 3, 3, 3, 4,5] 2. (c) [1,3,5,4] (d) [1,3,3, 5, [3,4] ] What is the value of new HashSet(list1) .size() ? (a). 1 3. (b) [1,3,3, 5,3, 4] (b) 2 (c) 3 (d) 4 What is list1 after executing list1.retainAll(list2)? (a). [1,3,5] (b) [1,5] (c) [3,3] (d) [2,4] Answer Question 4-6 after the execution of the following code: Map map1 = new TreeMap(); Map1.put(“C”, 20), map1.put(“D”,10), map1.put(“C”, 30), map1.put(“B”, 50); Map map2 = new LinkedHashMap(map1); map2.put(“A”, 60); 4. What will be the return value of the invocation: map1.get("C") ? (a). 10 5. (b) B (c) 30 (c) C 5 (c)3 (d) 2 For an instance c of Collection, you can obtain its iterator using ________________. (b) c.iterator() (c). c.iterators() (d). c.iterable() You can use a for-each loop to traverse all elements in a container object that implements _____. (a). Iterator 9. (d) D (b) 4 (a). c.getIterator() 8. (d) 50 What will be the value of the invocation: map2.size()? (a). 7. 20 What will be the return value of the code: map2.keySet().iterator().next() ? (a). A 6. (b) (b) Collection (c) Iterable (d) ArrayList What will be displayed by the following code? List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); for (int i = 0; i < list.size(); i++) System.out.print(list.remove(i)); (a). ABCD (b). AB (c). AC (d). AD (e). ABC 10. The __________ method in the Queue interface retrieves, but does not remove, the head of this queue, throwing an exception if this queue is empty. (a). poll() (b) remove() (c). element() (d). peek() 11. What will be the output of the following code: PriorityQueue<Integer> queue = new PriorityQueue<>( Arrays.asList(60, 10, 50, 30, 40, 20) ); while (!queue.isEmpty()) System.out.print(queue.poll() + " "); (a). 60 10 50 30 40 20 (b). 10 20 30 40 50 60 (c). 60 50 40 30 20 10 (d). 20 40 30 50 10 60 12. 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.getS(); (b). Foo.getI(); (c). Foo.getS(); (d). f.getI(); 13. Suppose class Foo and object reference f is defined as in the preceding problem. Then which of the following expressions is NOT legal ? (a). f.i (b) Foo.i (c) Foo.s (d) f.s 14. Which of the following definitions define a legal abstract class? 2 (a) abstract class A { void m() {} } (b) abstract class A { void m(); } (b) class A { abstract void m() {} } (c) class A { abstract void m() ; } // m() is concrete! // should be: …{ abstract void m() ; } 15. What kind of error would occur in the following code? 1. Class Student extends Person {} 2. Class Person { 3. public static void main(String[] args) { 4. Object person = new Student(); 5. Student student = person; }} (a) compile-time error at line 4 (b) run-time error at line 4 (d) run-time error at line 5 (e) no error (c) compile-time at line 5 16. Which of the following is a checked exception. (a). RuntimeException (b). Error (c). Exception (d). NumberFormatException 17. If the statement S1 cause an exception of type Exception2 (and all other Sk( k = 0..6) do not) in the following statement: try { S0; S1; S2; } catch(Exception1 e1) { S3; } catch(Exception2 e2) { throw new Exception(); } } catch(Exception3 e3) { S4; } finally { S5; } S6; Then which of the following statement sequences is the correct sequence of statements executed after S1? Suppose that Exception1 is a subclass of Exception2 while Exception2 is a subclass of Exception 3. (a) S2,S5,S6 (b) S3,S5,S6 (c) S4,S5,S6 (d) S5 (e) S5,S6 18. Which of the following statements will not throw an exception? (a). char c = “abc”.charAt(3); (b). Number n = new Double(10.0); (c). Double d = new Integer(10); (d). int x= (new int[4]) [4]; 19. Given the following code: String s1 = “Welcome to Java”; String s2 = new String(“Welcome to Java”); 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 20. To create a generic type E bounded by Number, use (a). <E extends Number> (b). <E extends Object> (c). <E> (d). <E extends Integer> 3 21. Given the following generic class head: class Cls<E> { … }, which of the following constructs is allowed to occur inside the class? (a). E e = New E(); (b) E[] e = new E[10]; (c) List<E> list = null ; (d) static void m(E e) {…} 22. [8 pts] Given an object o, complete the following methods to find all classes (which are NOT interfaces) of which o is an instance. static Class[] classes(Object o) { List<Class> list = new ArrayList<>() ; Class c = o._getClass()______ ; While(c != null ) { list.add( _c___) ; c = _c.getSuperclass()_________ ; } return __list.toArray(new Class[list.size()])__; } III (40 pts) 程式設計 1. [20pts] Multiset is an extension of a set in which an element may occur more than once. For example, as a set, {1,2,3} == {1,2,3,2} but as a multiset {1,2,3} != {1,2,3,2} since element 2 occurs twice in the second multiset. The number of times an element x occurring in a multiset ms is called its multiplicity(written ms.multicity(x)). We can use a Map<K,Integer> to represent a finite multiset ms consisting of elements of type K with the property that if element x occurs k times in ms then ms.get(x) returns k. Under such representation, implement the following methods to find the union() and intersection() of two input multisets s1 and s2. Note that the values of s1 and s2 should not be changed by the computation. The definitions of union and intersection of two multisets are given as follows: If x occurs in s1 k1 times and in s2 k2 times, then x occurs in (s1 s2) maximum(k1, k2) times and occurs in (s1 s2) minimum(k1, k2) times. (a) <K> static Map<K,Integer> union(Map<K,Integer> s1, Map<K,Integer> s2) { Map<K,Integer> rlt = new ____HashMap<>(s1)____ ; // clone s1 // Find all elements which occurs in s2 at least once. Set<K> keys = ___s2.keySet()____________________ ; Iterator<K> iter = ___keys.iterator()______ ; // form an iterator of all elements in s2 from keys while(iter.hasNext()) { K e = ____iter.next()_________ ; find current elements // update rlt so that rlt.get(e) = max(rlt.get(e), s2.get(e) ) __rlt.put(e, Math.max(rlt.get(e) == null ? 0 : rlt.get(e) , s2.get(e) );____ } return rlt ; (b) <K> static Map<K,Integer> intersection(Map<K,Integer> s1, Map<K,Integer> s2) { 4 Map<K,Integer> rlt = new HshMap<>() ; // clone s1 ?? No! Iterator<Map.Entry<K,Integer>> = s2.entrySet() ; while(s2.hasNext()) { Map.Entry kv = s2.next(); Integer I = Math.min(kv.getValue(), s1.get(kv.getKey()) == null ? 0 : s1.get(kv.getKey())); rlt.put(kv.getKey(), I) ; }; return rlt ; 2. } [20pts] A rectangle class parallel to the x and y axes is defined as follows: public class Rectangle { public int x,y,w,h ; // (x,y) is the coordinate of the left-upper corner of the rectangle. // (w,h) is the width and height of the rectangle. public Rectangle(int least_x, int least_y, int width, int height){ x = least_x; y = least_y; w = width; h = height } } Implement the following two methods to determine if two Rectangle r1 and r2 have intersection (i.e., there are at least one point located in both rectangles) and to determine the intersection area of both rectangles. (a) static boolean intersect(Rectangle r1, Rectangle r2) { // r1 and r2 do not intersect iff the projections of r1 and r2 in X or Y axis have no intersection. if( r1.x + r1.w < r2.x ) return false; if( r2.x + r2.w < r1.x ) return false; if( r1.y + r1.h < r2.y ) return false; if( r2.y + r2.h < r1.y ) return false; return true; } (b) static int area(Rectangle r1, Rectangle r2) { if(! intersect(r1,r2)) return 0; int[] xs = new int[]{r1.x, r1.x+r1.w, r2.x, r2.x+r2.w}; int[] ys = new int[]{r1.y, r1.y+r1.h, r2.y, r2.y+r2.h}; Arrays.sort(xs); Arrays.sort(ys); int xl = 0; if( xs[0] = r1.x && xs[3] = r1.x + r1.w) x1 = r2.w ; if( xs[0] = r1.x && xs[3] = r2.x + r2.w) x1 = r1.x+ r1.w - r2.x ; if( xs[0] = r2.x && xs[3] = r2.x + r2.w) x1 = r1.w ; if( xs[0] = r2.x && xs[3] = r1.x + r1.w) x1 = r2.x+ r2.w - r1.x ; // The above can be simplified to : x1 = xs[2] - xs[1] ; int yl = 0; if( ys[0] = r1.y && ys[3] = r1.y + r1.h) y1 = r2.h ; if( ys[0] = r1.y && ys[3] = r2.y + r2.h) x1 = r1.y+ r1.h - r2.y ; if( ys[0] = r2.y && ys[3] = r2.y + r2.h) x1 = r1.h ; if( ys[0] = r2.y && ys[3] = r1.y + r1.h) x1 = r2.y+ r2.h - r1.y ; // The above can be simplified to : y1 = ys[2] - ys[1] ; return xl * yl ; } 5