NTU-Exam 板


LINE

課程名稱︰物件導向程式設計 (Object-Oriented Programming) 課程性質︰系定必修 課程教師︰陳俊良 開課學院:電機資訊學院 開課系所︰資訊工程學系 考試日期(年月日)︰民國97年(西元2008年) 4月24日 考試時限(分鐘):120 是否需發放獎勵金:是 (如未明確表示,則不予發放) 試題 : 1. Please write down the output of the following program. (4%) 1: class Expression { 2: public static void main(String[] args) { 3: System.out.println(1 / 2 + 1 / 3); // (a) 4: System.out.println(1F/ 2 + 1 / 3); // (b) 5: } 6: } (註: 這題是要寫(a)和(b)那兩行會印出的東西。) 2. The following is a matrix multiplication program. It produces 10 13 28 40. Please fill in the missing code. (20%) 1: class MatrixMultiplication { 2: public static void main(String[] args) { 3: int[][] a = {{0, 1, 2}, {3, 4, 5}}; 4: int[][] b = {{0, 1}, {2, 3}, {4, 5}}; 5: int[][] c = Matrix.multiplication(a, b); 6: System.out.println(c[0][0] + " " + c[0][1] + " " + 7: c[1][0] + " " + c[1][1]); 8: } 9: } 10: class Matrix { 11: static int[][] multiplication(int[][] a, int[][] b) { 12: _(a)_ c = new int[_(b)_.length][_(c)_.length]; 13: for (int i = 0; i < _(d)_.length; i++) { 14: _(e)_ ai = a[i]; 15: _(f)_ ci = c[i]; 16: for (int j = 0; j < _(g)_.length; j++) { 17: int cij = 0; 18: for (int k = 0; k < _(h)_.length; k++) { 19: cij += ai_(i)_ * b[k][j]; 20: } 21: ci_(j)_ = cij; 22: } 23: } 24: return c; 25: } 26: } (註: 這題就是填空題,答案很多種的任選一個寫就好。) 3. Please write down the output of the following program. (20%) 1: class HidingAndOverriding { 2: public static void main(String[] args) { 3: SubClass b1 = new SubClass(); 4: SuperClass p1 = b1; 5: System.out.println(p1.variableA); // (a) 6: System.out.println(b1.variableA); // (b) 7: System.out.println(SubClass.variableA); // (c) 8: System.out.println(SuperClass.methodB(1)); // (d) 9: System.out.println(SubClass.methodB(2)); // (e) 10: System.out.println(p1.methodC(3)); // (f) 11: System.out.println(b1.methodC(4)); // (g) 12: System.out.println(p1.methodD(5)); // (h) 13: System.out.println(b1.methodD(6)); // (i) 14: System.out.println(b1.methodD()); // (j) 15: } 16: } 17: class SuperClass { 18: String variableA = "super"; 19: static String methodB(int parameter) { 20: return Subclass.variableA + 1; 21: } 22: String methodC(int parameter) { 23: return variableA + 2; 24: } 25: String methodD(int parameter) { 26: return variableA + 3; 27: } 28: } 29: class SubClass extends SuperClass { 30: static String variableA = "sub"; 31: static String methodB(int parameter) { 32: return variableA + 4; 33: } 34: String methodC(int parameter) { 35: return variableA + 5; 36: } 37: String methodD() { 38: return ((SuperClass) this).variableA + 6; 39: } 40: } (註: 跟第一題一樣) 4. The following program has 4 files. It produces (A((())CD)B). Please add access modifiers as tightly as you can. For the default level, please write "package". (20%) 1: import composite.*; 2: import composite.pattern.*; 3: ___(a)__ class Client { 4: ___(b)__ static void main(String[] args) { 5: Component c1 = new Composite(), c2 = new Composite(), 6: c3 = new Composite(), c4 = new Composite(); 7: c1.add(new Leaf('A')); 8: c1.add(c2); 9: c1.add(new Leaf('B')); 10: c2.add(c3); 11: c2.add(new Leaf('C')); 12: c2.add(new Leaf('D')); 13: c3.add(c4); 14: System.out.println(c1.toString()); 15: } 16: } -------------------------------------------------------------------------- 17: package composite; 18: ___(c)__ class Component { 19: ___(d)__ Composite parent; 20: ___(e)__ char name; 21: ___(f)__ void add(Component c) { 22: } 23: } -------------------------------------------------------------------------- 24: package composite; 25: inport java.util.Vector; 26: _as_(c)_ class Composite extends Component { 27: ___(g)__ Vector<Component> children = new Vector<Component>(); 28: _as_(f)_ void add(Component c) { 29: children.add(c); 30: c.parent = this; 31: } 32: ___(h)__ Component getChild(int index) { 33: return children.elementAt(index); 34: } 35: ___(i)__ String toString() { 36: StringBuilder sb = new StringBuilder(); 37: sb.append('('); 38: for (int i = 0; i < children.size(); i++) 39: sb.append(getChild(i).toString()); 40: return sb.append(')').toString(); 41: } 42: } -------------------------------------------------------------------------- 43: package composite.pattern; 44: import composite.Component; 45: _as_(c)_ class Leaf extends Component { 46: ___(j)__ Leaf(char name) { 47: this.name = name; 48: } 49: _as_(i)_ String toString() { 50: return name + ""; 51: } 52: } (註: 填空題,能限制越嚴格越好(能用private的就不要用其他的)。) 5. Please write down the output of the following program. (16%) 1: class OverriddenMethodInvocation { 2: public static void main(String[] args) { 3: SubClass b1 = new SubClass(); 4: b1.method(4); 5: System.out.println(); // (a) 6: SuperClass p1 = new SuperClass(); 7: b1.method(3); 8: System.out.println(); // (b) 9: } 10: } 11: class SuperClass { 12: void method(int parameter) { 13: if (parameter <= 0) return; 14: System.out.print(" super" + parameter); 15: parameter--; 16: method(parameter); 17: } 18: } 19: class SubClass extends SuperClass { 20: void method(int parameter) { 21: if (parameter <= 0) return; // 這行老師說刪掉 22: System.out.print(" sub" + parameter); 23: parameter--; 24: super.method(parameter); 25: } 26: } (註: (a)和(b)是要寫換行前印的東西,就是一整行輸出。) 6. Please point out all compile-errors (don't care runtime-errors) and write down the reasons. (20%) 1: clazz Polymorphism { 2: public static void main(String[] args) { 3: Book b1; 4: TextBook t1; 5: ComicBook c1; 6: Peanuts p1; 7: c1 = new Peanuts(); 8: p1 = c1; 9: b1 = c1; 10: t1 = c1; 11: t1 = b1; 12: t1 = (TextBook) b1; 13: c1 = (TextBook) b1; 14: c1 = (ComicBook) b1; 15: } 16: } 17: class Book { } 18: class ComicBook extends Book { } 19: class Peanuts extends ComicBook { } 20: class TextBook extends Book { } 21: class TheJavaProgrammingLanguage extends TextBook { } 22: class HeadFirstJava extends ComicBook, TextBook { } (註: 答案卷上有個範例 Line 1: class拼錯。) --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.31.132.139
1F:推 olala7846:感謝 06/15 16:08







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

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

TOP