精華區beta NTU-Exam 關於我們 聯絡資訊
課程名稱︰物件導向程式設計 (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
olala7846:感謝 06/15 16:08