Microsoft Word - JAVA Programming Language Homework I ans

Similar documents
3.1 num = 3 ch = 'C' 2

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Chapter 9: Objects and Classes

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Microsoft Word - 物件導向編程精要.doc

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

untitled

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

Microsoft PowerPoint - L17_Inheritance_v4.pptx

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

untitled

Strings

Microsoft Word - ch04三校.doc

Microsoft Word - 01.DOC

EJB-Programming-4-cn.doc

EJB-Programming-3.PDF

Learning Java

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

Java

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

javaexample-02.pdf


Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Obj

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

D C 93 2

The Embedded computing platform

untitled

<4D F736F F F696E74202D20B5DA3035D5C220C3E6CFF2B6D4CFF3B8DFBCB6B3CCD0F2C9E8BCC6>

2009年3月全国计算机等级考试二级Java语言程序设计笔试试题

chp6.ppt

任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class

c_cpp

Microsoft Word - JAVA Programming Language Homework VI_ans.doc

Chapter 9: Objects and Classes

Microsoft PowerPoint - plan08.ppt

《大话设计模式》第一章

无类继承.key

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

級 任 介 紹 班 主 任 其 他 級 任 2A 羅 彩 岫 主 任 吳 毅 蕙 老 師 2B 徐 玲 老 師 陳 詠 詩 老 師 2C 梁 麗 興 老 師 尹 南 老 師 2D 曾 康 老 師 李 樂 雯 老 師 2E 黃 蕙 明 老 師

JavaIO.PDF

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课

JBuilder Weblogic

投影片 1

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

Java 1 Java String Date

java2d-4.PDF

CC213

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

從篤加有二「區」談當代平埔文化復振現相

Microsoft PowerPoint - P766Ch06.ppt

高中英文科教師甄試心得

Chapter 9: Objects and Classes

運算子多載 Operator Overloading

ENGG1410-F Tutorial 6

untitled

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

C/C++语言 - C/C++数据

OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点

全国计算机技术与软件专业技术资格(水平)考试

C++ 程式設計

CC213

時 間 : 180 分 鐘 四 考 試 特 點 (a) 程 式 易 懂 撰 寫 簡 單, 不 必 經 過 長 時 間 的 課 程 訓 練 (b) 物 件 導 向 的 程 式 設 計, 將 重 點 擺 在 資 料 介 面 的 技 巧 (c)java 有 一 個 很 完 善 且 分 散 式 的 程 式

1 C++ 2 Bjarne Stroustrup C++ (system programming) 6 (infrastructure) C++ 7 Herb Sutter 8 C++ (efficiency) (flexibility) 9 (abstraction) (productivity

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

Microsoft Word - chap10.doc

穨control.PDF

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

untitled

2/80 2

Chn 116 Neh.d.01.nis

软件工程文档编制

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

天 主 教 輔 仁 大 學 社 會 學 系 學 士 論 文 小 別 勝 新 婚? 久 別 要 離 婚? 影 響 遠 距 家 庭 婚 姻 感 情 因 素 之 探 討 Separate marital relations are getting better or getting worse? -Exp

119,,,,,, (, ), : (1),,,,,, (2),,,, (3),,,,,20,,,,,,,,

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis

2015年4月11日雅思阅读预测机经(新东方版)

FY.DOC

用手機直接傳值不透過網頁連接, 來當作搖控器控制家電 ( 電視遙控器 ) 按下按鍵發送同時會回傳值來確定是否有送出 問題 :1. 應該是使用了太多 thread 導致在傳值上有問題 2. 一次按很多次按鈕沒辦法即時反應

K301Q-D VRT中英文说明书141009

Microsoft PowerPoint - 13_ClassAndObj.ppt

雲端 Cloud Computing 技術指南 運算 應用 平台與架構 10/04/15 11:55:46 INFO 10/04/15 11:55:53 INFO 10/04/15 11:55:56 INFO 10/04/15 11:56:05 INFO 10/04/15 11:56:07 INFO

untitled

untitled


Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

尽 管 Java 语 言 是 在 C++ 语 言 基 础 上 发 展 起 来 的, 但 是 有 别 于 C++,Java 是 一 种 纯 粹 的 面 向 对 象 语 言 (Object-oriented language) 在 像 Java 这 样 纯 粹 的 面 向 对 象 语 言 中, 所 有


untitled

A Study on JI Xiaolan s ( ) Life, Couplets and Theories of Couplets 紀 曉 嵐 ( ) 生 平 資 料 斠 正 及 對 聯 聯 論 研 究 LI Ha 李 夏 THE UNIVER

穨1-林聖欽.doc

1505.indd

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

Transcription:

JAVA Programming Language Homework I - OO concept Student ID: Name: 1. Which of the following techniques can be used to prevent the instantiation of a class by any code outside of the class? A. Declare all constructors with a void return type. B. Declare all constructors using the private access modifier. C. Do not declare any constructors inside a class definition. D. Do not include a return statement in the constructor. E. None of the above. Answer: B 當建構子被定義成 private, 任何的類別, 除了此建構子本身的類別, 誰都無法透過此建構子建立一個新的物件 2. Which of the following statements are true? A. A constructor can invoke the constructor of the direct superclass using the superclass constructor invocation statement super. B. By using constructor invocation statement this, a constructor can invoke another constructor of the same class. C. The constructor invocation statement, this, can legally appear anywhere in the constructor body. D. By using the constructor invocation statement this, a constructor can invoke itself. E. None of the above. Answer: A, B 關鍵字 this 是指本身的類別, 所以可用來呼叫本類別中其他的建構子, 但卻不允許用 this 來呼叫本身 ( 建構子 ) 呼叫建構子的 this 敘述, 必須放在陳述中的第一行 因為 super 代表父類別或 superclass, 所以可用來呼叫父類別的建構子 3. Given the following Java code: 1. public class Hello { 2. String title= ; 3. int value; 4. public Hello( ) {

5. title = title + World ; 6. System.out.print(title); 7. } 8. public Hello(int value) { 9. this.value = value; 10. title = Hello ; 11. this( ); 12. } 13. public static void main(string[] args){ 14. Hello b = new Hello (5); 15. } 16. } A. Hello B. Hello World C. Compilation fails D. Hello World 5 E. Hello Hello Answer: C 呼叫建構子的 this 敘述, 必須放在陳述中的第一行 4. Given the following Java code: 1. class Num { 2. public static String b( ) { return One ; } 3. public static String b( int i ) { return Two ; } 4. public static String b( int i, int j ) throws Exception { return Three ; } 5. public static void main( String[] args ) { 6. System.out.println( b(2) ); 7. } 8. } A. One B. Two C. Compilation fails D. Three E. None of the above Answer: B 因為 System.out.println(b(2)) 中的 b(2) 和 public static String b(int i)

中的參數列相同 5. Given the following Java code: Exhibit: 1. public class SimpleCalc { 2. public int value; 3. public void calculate( ) { value += 7; } 4. } And: 1. Public class MultiCalc extends SimpleCalc { 2. public void calculate( ) { value -= 3; } 3. public void calculate( int multipier) { 4. calculate( ); 5. super.calculate( ); 6. value *= multipier; 7. } 8. public static void main(string[] args) { 9. MultiCalc calculator = new MultiCalc( ); 10. calculator.calculate(2); 11. System.out.println( Value is: + calculator.value); 12. } 13. } A. Value is: 8 B. Compilation fails. C. Value is: 12 D. Value is: -12 E. The code runs with no output. Answer: A 從 MultiCalc 類別的 main() 開始, 建構出 MultiCalc(), 無傳參數的建構子, 呼叫 calculate(int) 方法再呼叫本類別的 calculate(), 使 value = 0-3 = -3 再呼叫父類別的 calculate(), 使 value = -3 + 7 = 4 最後將 value x 傳遞的參數值 2 = 4 * 2 = 8 再度返回 main() 方法, 將該 value 屬性印出 8

6. Given the following Java code: 1. public class Base { 2. public static final String FOO = foo ; 3. public static void main(string[] args) { 4. Base b = new Base( ); 5. Sub s = new Sub( ); 6. System.out.println(Base.FOO); 7. System.out.println(Sub.FOO); 8. System.out.println(b.FOO); 9. System.out.println(s.FOO); 10. System.out.println(((Base)s).FOO); 11. } 12. } 13. class Sub extends Base {public static final String FOO= bar ;} A. foofoofoofoofoo B. foobarfoobarbar C. foobarfoofoofoo D. foobarfoobarfoo E. foofoofoobarbar Answer: D Base.FOO 是 class Base 的 static 成員, 所以印出 "foo" Sub.FOO 是 class Sub 的 static 成員, 所以印出 "bar" b.foo 是 class Base 物件, 可以存取 static 成員, 所以印出 "foo" s.foo 是 class Sub 物件, 可以存取 static 成員, 所以印出 "bar" (Base)s 之後成為 Base 物件, 呼叫 Base 的 FOO, 所以印出 "foo" 7. Given the following Java code: 1. public class TestPoly { 2. public static void main(string[] args) { 3. Parent p = new Child( ); 4. } 5. } 6. 7. class Parent { 8. public Parent( ) { 9. super();

10. System.out.println( instantiate a parent ); 11. } 12. } 13. 14. class Child extends Parent { 15. public Child( ) { 16. System.out.println( instantiate a child ); 17. } 18. } A. instantiate a child B. instantiate a parent C. instantiate a child instantiate a parent D. instantiate a parent instantiate a child E. Compilation fails Answer: D 在 Child 建構子中的程式執行之前, 類別 Child 的建構子會先呼叫類別 Parent 的建構子, 當 Parent 建構子的程式執行時, 會列印出第一行, 然後再把控制項回傳給 Child 的建構子 8. Given the following Java code: 1. public class TestPoly { 2. public static void main(string[] args) { 3. Parent p = new Child( ); 4. } 5. } 6. 7. class Parent { 8. public Parent( ) { 9. super(); 10. System.out.println( instantiate a parent ); 11. } 12. } 13. 14. class Child extends Parent {

15. public Child( ) { 16. System.out.println( instantiate a child ); 17. super( ); 18. } 19. } A. instantiate a child B. instantiate a parent C. instantiate a child instantiate a parent D. instantiate a parent instantiate a child E. Compilation fails Answer: E Line 17 程式會使編譯程式失敗, 對 super() 的呼叫必須放在建構子的第一行陳述式中 9. Given the following Java code: 1. class C { 2. public static void main(string[] args) { 3. A tmp = new B( ); 4. tmp.m1( ); 5. tmp.m2( ); 6. ((B)tmp).m1( ); 7. ((B)tmp).m2( ); 8. } 9. } 10. class A {public void m1( ) { System.out.println ( A );}} 11. class B extends A { 12. public void m1( ) { System.out.println ( B1 );} 13. public void m2( ) { System.out.println ( B2 );} 14. public void m3( ) { System.out.println ( B3 );} 15. public void m4( ) { System.out.println ( B4 );} 16. } A. AB2B1B2 B. B1B2B1B2

C. Compiler Error D. Runtime Error E. None of the above Answer: C tmp 的型態為 A, 因為類別 A 中沒有函式 m2, 所以函式的多型不能應用, 亦即無法使用型類為 A 的參照來呼叫類別 B 中的函式 m2 10. Given the following Java code: 1. public class Bootchy { 2. int botch; 3. String snootch; 4. public Bootchy() { 5. this( snootchy ); 6. System.out.print( first ); 7. } 8. public Bootchy(String snootch) { 9. this(420, snootchy ); 10. System.out.print( second ); 11. } 12. public Bootchy(int bootch, String snootch) { 13. this.bootch=botch; 14. this.snootch = snootch; 15. System.out.print( third ); 16. } 17. public static void main(string[] args){ 18. Bootchy b = new Bootchy(); 19. System.out.print(b.snootch + + b.bootch); 20. } 21. } (A) snootchy 420 third second first (B) snootchy 420 first second third (C) first second third snootchy 420 (D) third second first snootchy 420 (E) third first second snootchy 420 Answer: D 建構出 b, 以無傳參數的建構子建構 public Bootchy(), this( "snootchy" ) 呼叫本類別建構子 public Boothchy( String snootch ),

this( 420, "snootchy" ) 呼叫本類別建構子 public Bootchy(int bootch, String snootch) 然後 bootch = 420; snootch = "snootchy", 印出 "third", 再度返回 public Boothchy( String snootch ), 印出 "second ", 再度返回 public Bootchy(), 印出 "first", 最後回到 main(), 印出 "snootchy" 與 420 11. Given the following Java code: 1. class A { 2. private static int tmp = 1; 3. static void m(int i) { tmp++; i++;} 4. public void n(int i) { tmp = tmp + 2;} 5. static void n() { tmp = tmp + 2;} 6. public static void main(string[] args) { 7. int tmp2 = 3; 8. m(tmp2); 9. System.out.println(tmp +, + tmp2); 10. } 11. } A. 1, 3 B. 2, 3 C. 1, 4 D. 2, 4 E. Compiler Error Answer: B 變數 tmp 和方法 m 都被定義為 static, 所以能直接被其他類別所存取, 第 7 行將 tmp2 設為 3, 第 8 行呼叫 m 並將 tmp 由 1 增加到 2, 而函式的參數 i 並不影響 tmp2 的值, 所以結果為 2, 3 12. Which of the following are legal identifiers? A. _3variable B. 3_variable C. this D. super E. *variable Answer: A B. 變數字首不可為數字 C.this 為關鍵字 D. super 為關鍵字 E. 變數字首不可為 *

13. Which are not primitive types in Java? A. float B. Boolean C. short D. Double E. long Answer: B, D Boolean 和 Double 分別為 boolean 與 double 之 Wrapper Class, 並非 primitive 資料型態 14. Given the following Java code: 1. interface Count { 2. short counter = 0; 3. void countup( ); 4. } 5. public class TestCount implements Count { 6. 7. public static void main(string[] args) { 8. TestCount t = new TestCount( ); 9. t.countup( ); 10. } 11. public void countup( ) { 12. for (int x = 6; x > counter; x--, ++counter) { 13. System.out.println( + counter); 14. } 15. } 16. } A.0 1 2 B. 1 2 3 C. 0 1 2 3 D. 1 2 3 4 E. Compiler error Answer: E

由於 counter 變數是介面變數, 其預設為 final static, 因此程式碼無法編譯 : 當第 12 行的程式碼試圖增加 counter 時, 編譯器便會發生錯誤 15. Given the following Java code: 1. public class ConstOver { 2. public ConstOver(int x, int y, int z) { 3. } 4. } Which two overload the ConstOver constructor? A. ConstOver( ){} B. Protected int ConstOver( ){} C. Private ConstOver(int z, int y, int x){} D. public Object ConstOver(int x, byte y, byte z){} E. public void ConstOver(byte x, byte y, byte z){} Answer: A C B: 有回傳值 int, 是 method 不是建構子 D: 有回傳值 Object, 是 method 不是建構子 E: 回傳值是 void,void 是沒有回傳值的 method 16. Given the following Java code: 1. interface foo { 2. int k = 0; 3. } 4. public class ExamA015 implements foo{ 5. public static void main(string[] args) { 6. int i; 7. ExamA015 test = new ExamA015( ); 8. i = test.k; 9. i = ExamA015.k; 10. i = foo.k; 11. } 12. } A. Compilation succeeds. B. An error at line 2 causes compilation to fail.

C. An error at line 9 causes compilation to fail. D. An error at line 10 causes compilation to fail. E. An error at line 11 causes compilation to fail. Answer: A 在 interface 所定義的變數其實就是 final static 變數, 可直接取用 17. Given the following Java code: 1. public class foo { 2. public static void main (String[] args) { 3. String s; 4. System.out.println( s= + s); 5. } 6. } A. The code compiles and s= is printed. B. The code compiles and s=null is printed. C. The code does not compile because string s is not initialized. D. The code does not compile because string s cannot be referenced. E. There is a runtime error. Answer: C String 變數在使用前必須先給定初始值 Java 語言中只有 static 變數 陣列與 Primitive Type 會自動加入初始內容值, 題中 s 屬於非陣列的參考資料型別 (Reference Type)

18. Which two statements are true about has-a and is-a relationships? (choose two) A. Inheritance represents an is-a relationship. B. Inheritance represents a has-a relationship. C. Interfaces must be used when creating a has-a relationship. D. Instance variables can be used when creating a has-a relationship. Answer: A D A: 繼承關係表現出 is-a 的關係 : 機車 ( 繼承 ) 車, 所以機車 is-a 車子, 正確 B: 繼承關係表現出 has-a 的關係 : 機車 ( 繼承 ) 車, 機車 has-a 車子是錯的 C: 建立一個 has-a 的關係必須用到介面 : 不一定, 因為機車 has-a 引擎, 並不需要實做任何介面, 因為引擎是另一種物件 D: 建立一個 has-a 的關係可以用到實體變數 : 正確, 因為機車 has-a 引擎的實體變數 19. Which two statements are true? (choose two) A. A final method in class X can be abstract if and only if X is abstract. B. A protected method in class X can be overridden by any subclass of X. C. A private static method can be called only within other static methods in class X. D. A non-static public final method in class X can be overridden in any subclass of X. E. A public static method in class X can be called by a subclass of X without explicitly referencing the class X. Answer: B, E A: final 方法不可能也同時是抽象方法, 所以錯誤 B: 一個 protected 方法是可以被子類別進行 override, 正確 C: 一個 private 的 static 方法只可以被同類別中的 static 方法所呼叫使用? 這是錯誤的 因為其他同類別的 non-static 方法也可以呼叫使用 D: 一個 final non-static 的 public 方法, 可以被該類別的子類別所 overridden? 這是錯誤的, 因為該方法已經是 final, 不可以被任何方式進行 overridden E: 一個 public static 方法可以被該類別之子類別直接參考使用, 正確