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灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP