Java Programming: Final Exam 姓名:______________ 學號:_______________ 6/20 2014 分數:______________ I (40 pts) 是非題 1. The object content of a final variable cannot be changed. 2. If an assert statement is executed, it will throw an exception. 3. We can use a try block with neither a catch block nor a finally block. 4. ArrayList<Integer> is a subtype of ArrayList<Number>. 5. List<int> is not a java type. 6. A non-static inner class can access all variables of its enclosing object even if they are static or private. 7. All nested interfaces are static. 8. It is permitted to declare a static nested class inside a member class. 9. Instances of a member class must be created within the containing class of the member class. 10. A local class in a method can access all local variables which are in the scope of the local class. 11. Inside a static method m of a class, we cannot use the keyword this. 12. The statement: List<String> x = new ArrayList<String>(); is a legal java statement. 13. Object[] is a subtype of Object. 14. If A is a subtype of B in Java, then A[] is a subtype of B[]. 15. Every Interface can serve as the type of variables. 16. If a class implements an interface, it must also implement all superinterfaces of the interface. 17. ArrayList<Integer> is a subtype of List<Integer>. 18. java.lang.Integer is a subclass of java.lang.Double 19. Inside a static method m of a class, we can use the keyword super. 20. If x is a 2d array containing more than one row, then it is possible that x[0].length != x[1].length. II (60 pts) 單選 21. Which of the following is the type of all Java exceptions. (a) RuntimeException (b) Exception (c) Error (d) Throwable 22. What exception would be thrown during the execution of the following code. int number = Integer.MAX_VALUE + 1; (a) RuntimeException (b) Exception (c) Error (d) Throwable (e) no exceptions 23. A method must declare to throw ________. (a) unchecked exceptions (b) checked exceptions (c) Error (d) RuntimeException 24. Which of the following is a checked exception? (a) RuntimeException (b) IOException (c) Error (e) NumberFormatException 25. Which of the following statements is correct? (a) Number d = 4.5; (b) Double d = 4.5; (c) Long l = 4.5; (d) Object o = 4.5; 26. What type of exception does the following code segment would throw? { Object o = new Object(); (a) ArithmeticException String d = (String) o; } (b) ArrayIndexOutOfBoundsException 1 (c) StringIndexOutOfBoundsException (d) ClassCastException 27. What type of exception does the following code segment would throw? { Object o = null; System.out.println(o.toString()) ; } (a) ArithmeticException (b) NullPointerException (c) StringIndexOutOfBoundsException (d) ClassCastException 28. Which class contains the method for checking whether a file exists? (a) File (b) PrintWriter (c) Scanner (d) System 29. Fill in the code in Comparable______ c = new Date(); (a) <String> (b) <Object> (c) <Date> (d) <E> 30. Which of the following code has no error? (a) Comparable<Object> c = "abc"; (b) Comparable<? extends Object> c = "abc"; (c) Comparable<? super Object> c = "abc"; (d) Comparable<Object> c = (Object) "abc"; 31. Suppose C is an inner class in a top level classA. C is compiled into a file named _________. (a) C.class (b) A$C.class (c) C$A.class (d) A&C.class 32. Suppose L1 is a list with content [1, 2, 1] and list L2 is a list with content [2, 6, 6]. Then after L1.addAll(L2), L1 is ________. (a) [1, 2, 1, 2, 6, 6] (b) [1,2,6] (c) [1, 1,2,2,6,6] (d) [1, 2, 1, 6] 33. Suppose list L1 is [1, 2, 1, 3, 3] and list L2 is [2, 3, 6]. After L1.retainAll(L2), L1 is ________. (a) [3] (b) [2, 3, 3] (c) [1, 2, 1, 3, 3, 2,3,6] (d) [1,1] 34. Which of the following kind of nested class cannot access instance variables of its containing class ? (a) static nested class (b) non-static member class (c) local class (d) anonymous class 35. Which of the following kind of class cannot have more than one instance? (a) static nested class (b) non-static member class (c) local class (d) anonymous class 36. Which of the following method would cause compile error if we put the code : "L.add(e) ;" into their bodies? (a) <E> void m(List<E> L, E e) {… } (c) <E> void m(List<? super E> L, E e) {… } (b) <E> void m(List<? extends E> L, E e) {… } (d) void m(List L, E e) {… } 37. Given a method with the head: public static <E> void m(List<? super E> L, E e) {… }, which of the following invocation cause error? (a) m(new ArrayList<String>(), "abc") (b) m(new ArrayList<String>(), new Object()) (c) m(new ArrayList<Object>(), "abc") (c) m(new ArrayList<Object>(), new Object()) 38. Object-oriented programming allows you to derive new classes from existing ones. This is called _______. (a) inheritance (b) abstraction (c) encapsulation (d) generalization 39. Variables of a supertype can refer to a subtype object. This is called _____________ (a) inheritance (b) polymorphism (c) encapsulation (d) dynamic binding 40. After JDK 1.5, you may pass an int value to a method which require an Integer object. This is called ________. (a) auto unboxing (b) auto conversion (c) auto boxing (d) auto casting III (30 pts) 程式設計 1. [15 pts; Write local and anonymous classes] Read the following definition of classes and fill in the blanks: public class P{ int x, y ; public P(int x1, int y1) { x = x1; y = y1;} public interface LessThan<E> { boolean le(E e1, E e2); } 2 public P min(P p, LessThan<P> cmp){ if( cmp.le(this, p) return this; else return p ;} class LessThen2 implementing LessThan<P> { public boolean le(P e1, P e2} {return e1.x <= e2.x && e1.y <= e2.y ; } } public static void main(String[] args) { P p1 = new P(1,4), p2 = new P(2,3) ; LessThan cmp2 = new LessThan2 (); System.out.println( "p1 <= p2 ?", p1.min(p2, cmp2) == p1 ? true : false) ; // give a redefinition of LessThan2 as a local class called LessThan3 __________________________________ ___________________________________________________________________________________ // We make use of local class LessThan3 here! out.println( "p1 <= p2 ?", p1.min(p2, new LessThan3()) == p1 ? true : false) ; // this should has the same effect // as previous println() out.println("p1 <= p2 ?", p1.min(p2, ________________ // use anonymous class to achieve the same effect as previous // new LessThan3(). ) == p1 ? true : false ) ; // this should has the same effect as previous println() }} 2. [15 pts] A rectangle class parallel to the x and y axes is defined as follows: public class Rectangle { // (x1,x2) is the smallest and largest value of the x coordinate of the rectangle. public int x1,x2,y1,y2 ; // (y1,y2) is the smallest and largest value of the y coordinate of the rectangle. public Rectangle(int x1, int x2, int y1, int y2){ this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2 } } Design a method coveringRectangle which can be used to find a minimal Rectangle enclosing all input rectangles stored in an ArrayList. static Rectangle coveringRectangle(List<Rectangle> list ) { // assume list is not empty ; 3