精華區beta NTU-Exam 關於我們 聯絡資訊
課程名稱︰物件導向程式設計 (Object-Oriented Programming) 課程性質︰系定必修 課程教師︰陳俊良 開課學院:電機資訊學院 開課系所︰資訊工程學系 考試日期(年月日)︰97年 6月16日 考試時限(分鐘):120分鐘 (19:00~21:00) 是否需發放獎勵金:是 (如未明確表示,則不予發放) 試題 : 1.Consider the program QuizForTryStatement shown below. What's the output when executing: [6x3%] (a) java QuizForTryStatement (b) java QuizForTryStatement mouse (c) java QuizForTryStatement cow (d) java QuizForTryStatement tiger (e) java QuizForTryStatement rabbit (f) java QuizForTryStatement dragon 1: class QuizForTryStatement { 2: public static void main(String[] args) { 3: int[] data = new int[] {1, 2, 3, 4, 5, 6}; 4: try { 5: char flag = args[0].charAt(3); 6: try{ 7: switch (flag) { 8: case 'b': 9: System.out.println(average(null, 1, 4)); break; 10: case 'e': 11: System.out.println(average(data, 1, 7)); break; 12: case 's': 13: System.out.println(avergae(data, 1, 1)); break; 14: } 15: System.out.println(average(data, 1, 0)); 16: } catch (NullPointerException e) { 17: System.out.println(average(data, 2, 5)); 18: } catch (ArithmeticException e) { 19: System.out.println(average(data, 3, 6)); 20: } 21: } catch (IndexOutOfBoundsException e) { 22: int n = 3; 23: if (e instanceof StringIndexOutOfBoundsException) n *= n; 24: if (e.getMessage().equals("0")) n += n; 25: System.out.println(n); 26: } 27: } 28: static int average(int[] a, int fromIndex, int toIndex) { 29: int sum = 0; 30: for (int i = fromIndex; i < toIndex; i++) { 31: sum += a[i]; 32: } 33: return sum / (toIndex - fromIndex); 34: } 35: } Hint: Hierarchy for some frequently used exceptions ◇ java.lang.Throwable ◇ java.lang.Error ◇ java.lang.Exception ◇ java.io.IOException ◇ java.lang.RunTimeException ◇ java.lang.ArithmeticException ◇ java.lang.ClassCastException ◇ java.lang.IllegalArgumentException ◇ java.lang.NumberFormatException ◇ java.lang.IndexOutOfBoundsException ◇ java.lang.ArrayIndexOutOfBoundsException ◇ java.lang.StringIndexOutOfBoundsException ◇ java.lang.NegativeArraySizeException ◇ java.lang.NullPointerException Hint: ArithmeticException - Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class. Hint: Throwable.getMessage() - Returns the detail message string of this throwable. For IndexOutOfBoundsExcetion, the message is the error index. Hint: String.charAt(n) - Returns the char value at the specified index. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing. 2.Consider the following classes and interface. public interface CharSequence { ... } public final class String implements Serializable, Comparable<String>, CharSequence { ... } public final class StringBuilder implements Serializable, CharSequence { ... } public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable { ... } public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable { ... } Are they legal statemants? Explain your reason briefly (at most 3 lines for each question). [4x(2+3)%] (a) HashSet<String> hs1 = new HashSet<StringBuilder>(); (b) HashSet<String> hs2 = new LinkedHashSet<String>(); (c) HashSet<CharSequence> hc1 = new HashSet<CharSequence>(); (d) HashSet<CharSequence> hc2 = new HashSet<StringBuilder>(); 3.java.util.Arrays provides many useful methods to manipulate arrays. In order to compare elements, java.util.Arrays use two interfaces java.lang.Comparable<T> and java.util.Comparator<T>. Some important statements are extracted below for your reference. 1: package java.lang; 2: public interface Comparable<T> { 3: public int compareTo(T o); 4: } ------------------------------------------------------------------------------ 5: package java.util; 6: public interface Comparator<T> { 7: int compare(T o1, T o2); 8: } ------------------------------------------------------------------------------ 9: package java.util; 10: public class Arrays { 11: public static int binarySearch(Object[] a, Object key) { 12: return binarySearch0(a, 0, a.length, key); 13: } 14: private static int binarySearch0(Object[] a, int fromIndex, 15: int toIndex, Object key) { 16: int low = fromIndex; 17: int high = toIndex - 1; 18: while (low <= high) { 19: int mid = (low + high) >>> 1; 20: Comparable midVal = (Comparable)a[mid]; 21: int cmp = midVal.compareTo(key); 22: if (cmp < 0) low = mid + 1; 23: else if (cmp > 0) high = mid - 1; 24: else return mid; // key found 25: } 26: return -(low + 1); // key not found. 27: } 28: public static <T> int binarySearch(T[] a, T key, 29: Comparator<? super T> c) { 30: return binarySearch0(a, 0, a.length, key, c); 31: } 32: private static <T> int binarySearch0(T[] a, int fromIndex, 33: int toIndex, T key, Comparator<? super T> c) { 34: if (c == null) { 35: return binarySearch0(a, fromIndex, toIndex, key); 36: } 37: int low = fromIndex; 38: int high = toIndex - 1; 39: while (low <= high) { 40: int mid = (low + high) >>> 1; 41: T midVal = a[mid]; 42: int cmp = c.compare(midVal, key); 43: if (cmp < 0) low = mid + 1; 44: else if (cmp > 0) high = mid - 1; 45: else return mid; // key found 46: } 47: return -(low + 1); // key not found. 48: } 49: } (a) Why we need two versions of binarySearch(), lines 11 and line 28? Explain your reason briefly(at most 3 lines). [5%] (b) What is the purpose of lines 34~36? Describe your answer briefly(at most 3 lines). [5%] (c) What happen if we replace lines 20 and 21 by the following statement. Explain your reason briefly(at most 3 lines). [5%] int cmp = a[mid].compareTo(key); (d) What's >>> in line 19? [5%] The following program produces 1 3. Please fill in the missing code. [5x3%] 1: import java.util.*; 2: class Product implements (e) { 3: int price; 4: String name; 5: static NameComparator nc = new NameComparator(); 6: public int (f) (Product other) { 7: return this.price - other.price; 8: } 9: Product(int price, String name) { 10: this.price = price; 11: this.name = name; 12: } 13: } 14: class NameComparator implements (g) { 15: public int (h) (Product p1, Product p2) { 16: return p1.name.compareTo(p2.name); 17: } 18: } 19: class Store { 20: public static void main(String[] args) { 21: Product[] pp = { new Product(100, "C"), 22: new Product(500, "B"), 23: new Product(200, "D"), 24: new Product(400, "A"), 25: new Product(300, "E"), 26: }; 27: Product p = new Product(200, "D"); 28: Arrays.sort(pp); 29: int index = Arrays.binarySearch(pp, p); 30: System.out.print(index); 31: Arrays.sort(pp, Product.nc); 32: index = Arrays.binarySearch(pp, p, (i) ); 33: System.out.println(index); 34: } 35: } 4.java.lang.Thread and java.lang.Runnable are fundamental APIs to support multi.threading. Some important statements are extracted below for your reference. 1: public class Thread implements Runnable { 2: private char[] name; 3: private Runnable target; 4: private long eetop; 5: private void init(ThreadGroup g, Runnable target, String name, 6: long stackSize) { 7: ... 8: this.name = name.toCharArray(); 9: this.target = target; 10: } 11: public Thread(String name) { 12: init(null, null, name, 0); 13: } 14: public Thread(Runnable target, String name) { 15: init(null, target, name, 0); 16: } 17: public synchronized void start() { 18: ... 19: start0(); 20: } 21: private native void start0(); 22: public void run() { 23: if (target != null) target.run(); 24: } 25: } ------------------------------------------------------------------------------ 26: public interface Runnable { 27: public abstract void run(); 28: } Consider the program Party shown below to answer question. (a) Describe the story behind program Party briefly (at most 5 lines). [10%] (b) What happen if we modify line 31 into the following statement. Explain your reason briefly (at most 3 lines). [5%] new Guest("Amy").run(); (c) What happen if we modify line 33 into the following statement. Explain your reason briefly (at most 3 lines). [5%] new Thread(new Waiter()).run(); (d) What happen if we modify line 51 into the following statement. Explain your reason briefly (at most 3 lines). [5%] if (Waiter.publicCake < n) Waiter.class.wait(); (e) Is it safe if we swap line 76 and 77? Explain your reason briefly (at most 3 lines). [5%] 29: class Party { 30: public static void main(String[] args) { 31: new Guest("Amy").start(); 32: new Guest("Bob").start(); 33: new Thread(new Waiter()).start(); 34: } 35: } ------------------------------------------------------------------------------ 36: class Guest extends Thread { 37: static java.util.Random r = new java.util.Random(); 38: int personalCake; 39: Guest(String name) { 40: super(name); 41: } 42: public void run() { 43: while(true) { 44: if (personalCake > 0) eat(); 45: else take(r.nextInt(2) + 1); 46: } 47: } 48: void take(int n) { 49: synchronized(Waiter.class) { 50: try { 51: while (Waiter.publicCake < n) Waiter.class.wait(); 52: } catch (InterruptedException e) { 53: } 54: Waiter.publicCake -= n; 55: personalCake += n; 56: Waiter.class.notifyAll(); 57: } 58: } 59: void eat() { 60: personalCake--; 61: } 62: } ------------------------------------------------------------------------------ 63: class Waiter implements Runnable { 64: static java.util.Random r = new java.util.Random(); 65: static int publicCake; 66: public void run() { 67: while (true) { 68: add(r.nextInt(3) + 1); 69: } 70: } 71: static synchronized void add(int n) { 72: try { 73: while (publicCake + n > 6) Waiter.class.wait(); 74: } catch (InterruptedException e) { 75: } 76: publicCake += n; 77: Waiter.class.notifyAll(); 78: } 79: } Hint: Random.nextInt(n) - Returns a random int value between 0 (inclusive) and the specified value (exclusive). <<完>> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.240.220