21、Which of the following assignment is not correct? A. float f = 11.1; B. double d = 5.3E12; C. double d = 3.14159; D. double d = 3.14D. (a) 題目:下面的哪些賦值語句是對的。 浮點數的賦值是帶有小數點的數字缺省是double型的,假如在浮點數后面加f或者F則是float,后面加d或者D則是double,科學計數法形式的浮點數也是double型的,而double的精度比float高,將一個高精度的double賦值給一個低精度的float時需要進行強制類型轉換,反之則不需要。
22、Given the uncompleted code of a class: class Person { String name, department; int age; public Person(String n){ name = n; } public Person(String n, int a){ name = n; age = a; } public Person(String n, String d, int a) { // doing the same as two arguments version of constrUCtor // including assignment name=n,age=a department = d; } } Which eXPRession can be added at the "doing the same as..." part of the constructor? A. Person(n,a); B. this(Person(n,a)); C. this(n,a); D. this(name,age). (c) 題目:給出下面的不完整的類代碼: … 下面的哪些表達式可以加到構造方法中的"doing the same as..."處? 在同一個類的不同構造方法中調用該類的其它構造方法需要使用this(…)的形式,而且必須是在構造方法的第一行調用,這個和普通的方法重載調用的方式不同,普通的方法可以直接使用方法名加參數來調用,而且調用位置沒有限制,因此答案A是不行的,B的語法就是錯誤的,D的錯誤在于在父類型的構造函數被調用前不能引用類的成員。構造方法是一個類對象實例化的起點(雖然嚴格來說首先執行的并不是構造方法的第一個語句,而是內存的分配),因此在構造方法中不能將成員作為參數引用。
23、Which of the following statements about variables and their scopes are true?
A. Instance variables are member variables of a class. B. Instance variables are declared with the static keyWord. C. Local variables defined inside a method are created when the method is executed. D. Local variables must be initialized before they are used. (acd) 題目:下面關于變量及其范圍的陳述哪些是對的。 A. 實例變量是類的成員變量。 B. 實例變量用要害字static聲明。 C. 在方法中定義的局部變量在該方法被執行時創建 D. 局部變量在使用前必須被初始化。 類中有幾種變量,分別是:局部變量(英文可以為:local/automatic/temporary/stack variable)是定義在方法里的變量;實例變量(英文為:instance variable)是在方法外而在類聲明內定義的變量,有時也叫成員變量;類變量(英文為:class variable)是用要害字static聲明的實例變量,他們的生存期分別是:局部變量在定義該變量的方法被調用時被創建,而在該方法退出后被撤銷;實例變量在使用new Xxxx()創建該類的實例時被創建,而其生存期和該類的實例對象的生存期相同;類變量在該類被加載時被創建,不一定要用new Xxxx()創建,所有該類的實例對象共享該類變量,其生存期是類的生存期。任何變量在使用前都必須初始化,但是需要指出的是局部變量必須顯式初始化,而實例變量不必,原始類型的實例變量在該類的構造方法被調用時為它分配的缺省的值,整型是0,布爾型是false,而浮點型是0.0f,引用類型(類類型)的實例變量的缺省值是null(沒有進行實際的初始化,對它的使用將引起NullPointException),類變量的規則和實例變量一樣,不同的是類變量的初始化是在類被加載時。 24、public void test() { try { oneMethod(); System.out.println("condition 1"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("condition 2"); } catch(Exception e) { System.out.println("condition 3"); } finally { System.out.println("finally"); } } Which will display if oneMethod run normally? A. condition 1 B. condition 2 C. condition 3 D. finally (ad)
25、Given the following code: public class Test { void printValue(int m){ do { System.out.println("The value is"+m); } while( --m > 10 ) } public static void main(String arg[]) { int i=10; Test t= new Test(); t.printValue(i); } } Which will be output? A. The value is 8 B. The value is 9 C. The value is 10 D. The value is 11 (c) 題目:給出下面的代碼: … 輸出將是什么? 此題考察的是do… while循環和 -- 操作符的知識,do…while最少被執行一次,在執行完do中的內容后判定while中的條件是否為true,假如為true的話就再執行do中的內容,然后再進行判定,以此類推直到while的判定為false時退出循環執行循環后面的內容,而—操作符的規則是在變量右邊的-- 將先進行運算,然后才是使變量的值減一,而在變量左邊的是先將變量的值減一再運算。
26、Which of the following statements about declaration are true? A. Declaration of primitive types such as boolean, byte and so on does not allocate memory space for the variable. B. Declaration of primitive types such as boolean, byte and so on allocates memory space for the variable. C. Declaration of nonprimitive types such as String, Vector and so on does not allocate memory space for the object. D. Declaration of nonprimitive types such as String, Vector ans so on allocates memory space for the object. (ad) 題目:下面的有關聲明的哪些敘述是對的。 A. 對原始數據類型例如boolean,byte的變量的聲明不會為該變量分配內存空間。 B. 對原始數據類型例如boolean,byte的變量的聲明將為之分配內存空間。 C. 非原始數據類型例如String,Vector的變量的聲明不會為該對象分配內存。 D. 非原始數據類型例如String,Vector的變量的聲明會為該對象分配內存。 對原始數據類型的變量的聲明將為之分配內存并賦予一個缺省值,參見23題的敘述,而非原始數據類型的變量必須用new Xxxx()分配內存及初始化。但是嚴格來講此題的答案有待確定,因為只有原始類型的實例變量和類變量的聲明在類對象被創建/類被加載時完成內存的自動分配,而原始類型的局部變量必須顯式初始化,從這點來看原始類型的局部變量沒有被自動分配內存,SL275中只提出了非原始數據類型的變量必須使用new Xxxx()完成內存的分配而沒有指出原始數據類型的變量是否在聲明時即自動進行內存分配,而從局部變量不能在顯式初始化前使用這點來看在聲明時沒有進行內存分配。因此答案a的正確性還有待官方的確定。 27、In the java API documentation which sections are included in a class document? A. The description of the class and its purpose B. A list of methods in its super class C. A list of member variable D. The class hierarchy (acd) 題目:在Java API文檔中下面的哪些部分被包括在內 A. 類及用途的描述 B. 父類的方法的列表 C. 成員變量的列表 D. 類層次 類文檔的內容主要是:類層次、類及用途描述、成員變量列表、構造方法列表、成員方法列表、從類層次上繼續的方法列表、成員變量的具體說明、構造方法具體說明、成員方法具體說明。
28、Given the following code: 1) public void modify() { 2) int i, j, k; 3) i = 100; 4) while ( i > 0 ) { 5) j = i * 2; 6) System.out.println (" The value of j is " + j ); 7) k = k + 1; 8) i--; 9) } 10) } Which line might cause an error during compilation? A. line 4 B. line 6 C. line 7 D. line 8 (c) 題目:給出下面的代碼: … 哪些行在編譯時可能產生錯誤。 這個問題在前面有關變量的類型及其作用域的問題中討論過,局部變量在使用前必須顯式初始化,而代碼中的變量k在使用前沒有。
29、Which of the following statements about variables and scope are true? A. Local variables defined inside a method are destroyed when the method is exited. B. Local variables are also called automatic variables. C. Variables defined outside a method are created when the object is constructed. D. A method parameter variabl