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

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

TOP