NTU-Exam 板


LINE

課程名稱︰物件導向程式設計 課程性質︰選修 課程教師︰林軒田 開課學院:電資學院 開課系所︰資工系 考試日期(年月日)︰2013.4.15 考試時限(分鐘):120分鐘 是否需發放獎勵金:是 (如未明確表示,則不予發放) 試題: This is a open-book exam. You can use any printed materials as your reference during the exam. Any electronic devices are not allowed. Any form of cheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of misconducts. Both English and Chinese (if suited) are allowed for answering the questions. We do not accept any other languages. There are 6 problems in the exam, each worth 35 points─the full credit is 210 points. Some problems would be divided to two or three sub-problems. There is another bonus problem with 10 points. The midterm would be weighted as 2-3 homework sets, but the exact weight would depend on your performance today. Good luck! 1. Java and C (1) (20%+15%) Describe to someone who knows C about two differences between C and Java. The difference can be a big one or a small one, and it is the correctness and clarity of your description that the TAs will grade on. There is no need to be overly long in your description─an ideal answer would be concise. Also, we want the differences to be "different" between the two by themselves. The first difference you list would be worth 20 points, the second would be worth 15 points. A sample answer would look like (and no, you cannot use these two): [0] Java uses a "Garbage Collector" to manage memory automatically, while C assumes that the progrpmmers have the power and the responsibility to allocate/de-allocate memory slots on their own. [1] C allows flexible "goto" statements, while Java does not. 2. Constructors 1 class A{ } 2 class B extends A { 3 int u; C other; 4 B(int u){ this.u = u; other = new C(u); } 5 B(){ this(20); } 6 } 7 class C extends A { 8 int v; 9 C(int v){ this.v = v; } 10 } 11 class D extends B { 12 double x; 13 D(int u, double x){ super(u); this.x = x; } 14 } (1) (15%) Line 1 of the code below would compile normally while line 2 results in HAHAHA (compile error). Why? 1 A ref = new A(); 2 A ref = new C(); (2) (20%) List the order of constructors that are called after executing new D (1, 2.0). You need to list all the constructors, including the constructor of java.lang.Object and the constructor(s) automatically added by javac. 3. Mutable versus Immutable We know that java.lang.String is an immutable class, which makes it "safer" to use the instances of the class. The following class Color, however, is mutable. (1) (20%) Rewrite Color such that it is immutable. Your re-written Color must be fully compatible with the original one in its methods. You can just explain the "differences" of your new class to the TAs instead of listing every line (unless you really want to). (2) (15%) Can your re-written Color still be immutable if some other programmers write a ColorChild that extends your Color class? That is, can someone use a Color-type reference with a ColorChild type instance to change some contents of the Color part? If so, how could you prevent such from happening? If not, why is your Color class safe from such? 1 public class Color { 2 public int red; 3 public int green; 4 public int blue; 5 6 public Color(int _red, int _green, int _blue){ 7 red = _red; green = _green; blue = _blue; 8 } 9 10 public int getColor() { return ((red << 16) | (green << 8) | blue); } 11 public Color invert () { 12 red = 255 - red; 13 green = 255 - green; 14 blue = 255 - blue; 15 return this; 16 } 17 } 4. Lucky-tons (1) (35%) Consider writing a class License that only allows at most 1126 licenses to be used at the same time. Each licenses needs to be created when needed for the first time, but can be re-used (after being released) afterwards. Please complete the following code that does the job. 1 public class License{ 2 private static License [] pool = new License [1126]; 3 private static boolean [] used = new boolean [1126]; 4 5 /* (1) (10%) You need a constructor to allocate your instances, 6 but at the same time preventing external code from directly 7 using it. */ 8 9 /* if there is an unused license, return that; 10 if not, return null */ 11 public static License getLicense(){ 12 /* (2) (15%) You need code here */ 13 } 14 15 /* if there is a matching license being used, release it; 16 if not, do nothing */ 17 public static void releaseLicense (License toRelease){ 18 /* (3) (10%) You need code here */ 19 } 20 } 5. Encapsulation (1) (20%) Assume that the class ntu.csie.YoungProfessor extends class ntu.Professor, and the class ntu.csie.SeniorProfessor extends ntu.Professor as well. Which of the following can be invoked within an instance method of ntu.csie.YoungProfessor? (a) a call to a public method of ntu.Professor (b) a call to a protected method of ntu.Professor (c) a call to a default method of ntu.Professor (d) a call to a private method of ntu.Professor (e) a call to a public method of ntu.csie.SeniorProfessor (f) a call to a protected method of ntu.csie.SeniorProfessor (g) a call to a default method of ntu.csie.SeniorProfessor (h) a call to a private method of ntu.csie.SeniorProfessor (i) a call to a protected method of ntu.Student (j) a call to a default method of ntu.Student (2) (10%) The "finalizer" we discussed in class was actually declared in java.lang.Object with the protected access modifier, and can be overridden with the finalizing steps of different classes. Which of the following classes (for simplicity, please just consider regular classes, not the array types) can call java.lang.Object.finalize()? (a) classes that are descendants of java.lang.Object (b) classes that are of package java.lang (3) (5%) If your answer above include "classes that are descendants of java.lang.Object", then you agree that every class in Java shall be able to call the finalizer. Then, guess why the finalizer shouldn't just come with the public access modifier (what's the difference between using public and protected here?). If your answer above does not include "classes that are descendants of java.lang.Object", then describe what classes cannot call java.lang.Object.finalize() in their methods. 6. References and Instances I know that you guys studied hard for Java night. What did you drink? Java coffee, of course! Let's play with Java coffee a bit. 1 class Drink{ 2 public double amount; 3 } 4 class Java extends Drink{ 5 public double caffeine; 6 public Java(double a, double c){ 7 amount = a; 8 caffeine = c; 9 } 10 public Java{ this(1.0, 2.0); } 11 } 12 class Milk extends Drink{ } 13 class FatlessMilk extends Milk{ public double fat; } 14 class BlendedJava extends Java{ 15 public Milk milk; 16 public double ratio; 17 } 18 19 public class JavaDemo{ 20 public static void main(String[] argv){ 21 Milk m1 = new Milk(); 22 Milk m2 = new Milk(); 23 Java j1 = new Java(3.0, 2.0); 24 BlendedJava b1 = new BlendedJava(); 25 BlendedJava b2 = new BlendedJava(); 26 b1.milk = m1; 27 j1 = b1; 28 b1.milk = new FatlessMilk(); 29 Milk[] marr = new Milk[3]; 30 marr[0] = bl.milk; 31 marr[l] = m2; 32 } 33 } (1) (10%) Assume that you want a line of 1 System.out.println(j1); around the end of JavaDemo.main(String []) to print out a line of "Java coffee of caffeine ratio 2/3". How would you do that? (2) (25%) Illustrate the memory layout of JVM at the end of JavaDemo.main(String[]). You need to show all the instances and self-defined local variables that are "alive" from the stack frame of JavaDemo.main(String[]), their connections (references) and states (primitive-variable values). 7. java.lang.Object (1) (Bonus 10%) In class, we discussed about why java.lang.Object shouldn't contain abstract methods─if so, there is a huge burden on programmers when designing their own classes. On the other hand, the remaining question is why java.lang.Object shouldn't be an abstract class. Is there really a need of constructing an instance of java.lang.Object? Please reason why the designers of Java may have chosen to keep java.lang.Object concrete. --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.166.250.175







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