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

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

TOP