NTU-Exam 板


LINE

課程名稱︰物件導向程式設計 (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







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP