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 ()創建該類的實例時被創建,而其生存期和該類的實例對象的生存期相同;類變量在該類被加載時被創建,不一定要用new ()創建,所有該類的實例對象共享該類變量,其生存期是類的生存期。任何變量在使用前都必須初始化,但是需要指出的是局部變量必須顯式初始化,而實例變量不必,原始類型的實例變量在該類的構造方法被調用時為它分配的缺省的值,整型是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)
題目:在onemethod()方法運行正常的情況下將顯示什么?
如果try塊中的語句在執行時發生異常,則執行從該處中斷而進入catch塊,根據異常的類型進行匹配,最前面的優先進行匹配比較,只要該異常是catch中指定的異常的子類就匹配成功進而執行相應的catch中的內容,而finally塊中的內容無論是否發生異常都將被執行。
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 ()分配內存及初始化。但是嚴格來講此題的答案有待確定,因為只有原始類型的實例變量和類變量的聲明在類對象被創建/類被加載時完成內存的自動分配,而原始類型的局部變量必須顯式初始化,從這點來看原始類型的局部變量沒有被自動分配內存,sl275中只提出了非原始數據類型的變量必須使用new ()完成內存的分配而沒有指出原始數據類型的變量是否在聲明時即自動進行內存分配,而從局部變量不能在顯式初始化前使用這點來看在聲明時沒有進行內存分配。因此答案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 variable continues to exist for as long as the object is needed in which the method is defined.
(abc)
題目:下面有關變量及其作用域的陳述哪些是對的。
a. 在方法里面定義的局部變量在方法退出的時候被撤銷。
b. 局部變量也叫自動變量。
c. 在方法外面定義的變量(譯注:即實例變量)在對象被構造時創建。
d. 在方法中定義的方法的參變量只要該對象被需要就一直存在。
本題還是討論變量的類型及作用域,參看前面的敘述。
30、a class design requires that a member variable cannot be accessible directly outside the class. which modifier should be used to obtain the access control?
a. public
b. no modifier
c. protected
d. private
(d)
題目:類的設計要求它的某個成員變量不能被外部類直接訪問。應該使用下面的哪些修飾符獲得需要的訪問控制。
這個在前面也有敘述,java有四種訪問類型,分別為:public,protected,default,private,其中public變量可以被所有的外部類訪問,而pretected的可以被同一個包及該類的子類訪問,default即沒有任何修飾符的變量可以被同一個包中的類訪問,而private變量只能在被該類內部被訪問。題目中的外部類應該理解為除該類自身的所有其它類,因此只有使用private可以達到要求。
31given the following code fragment:
1) string str = null;
2) if ((str != null) && (str.length() > 10)) {
3) system.out.println("more than 10");
4) }
5) else if ((str != null) & (str.length() < 5)) {
6) system.out.println("less than 5");
7) }
8) else { system.out.println("end"); }
which line will cause error?
a. line 1
b. line 2
c. line 5
d. line 8
(c)
題目:給出下面的代碼片斷:
…
哪些行將導致錯誤。
此題需要將代碼仔細看清楚,查詢沒有邏輯錯誤,if …else的使用沒有問題,也沒有拼寫錯誤,錯誤在于第5行的“與”操作符的使用,邏輯操作符(logical operator)的“與”應該是&&,而在執行“與”操作的時候,如果第一個條件為false,那么第二個條件判斷運算是不做的,但是這里是位邏輯操作符(bitwise logical operator)的“與”,在進行這個運算時,無論第一個條件的結果是什么都會執行第二個的運算,因此,假設str=null,那么第5句的str.length()就會導致nullpointerexception,因此本題的錯誤在于此。
32、which statements about java code security are true?
a. the bytecode verifier loads all classes needed for the execution of a program.
b. executing code is performed by the runtime interpreter.
c. at runtime the bytecodes are loaded, checked and run in an interpreter.
d. the class loader adds security by separating the namespaces for the classes of the local file system from those imported from network sources.
(bcd)
題目:下面有關java代碼安全性的敘述哪些是對的。
a. 字節碼校驗器加載查詢執行需要的所有類。
b. 運行時解釋器執行代碼。
c. 在運行時,字節碼被加載,驗證然后在解釋器里面運行。
d. 類加載器通過分離本機文件系統的類和從網絡導入的類增加安全性。
sl275中描述的java程序運行的過程是這樣的:類加載器(class loader)加載程序運行所需要的所有類,它通過區分本機文件系統的類和網絡系統導入的類增加安全性,這可以限制任何的特洛伊木馬程序,因為本機類總是先被加載,一旦所有的類被加載完,執行文件的內存劃分就固定了,在這個時候特定的內存地址被分配給對應的符號引用,查找表(lookuo table)也被建立,由于內存劃分發生在運行時,解釋器在受限制的代碼區增加保護防止未授權的訪問;然后字節碼校驗器(byte code verifier)進行校驗,主要執行下面的檢查:類符合jvm規范的類文件格式,沒有違反訪問限制,代碼沒有造成堆棧的上溢或者下溢,所有操作代碼的參數類型都是正確的,沒有非法的數據類型轉換(例如將整型數轉換成對象類型)發生;校驗通過的字節碼被解釋器(interpreter)執行,解釋器在必要時通過運行時系統執行對底層硬件的合適調用。后三個答案是sl275中的原話。
33、given the following code:
public class person{
static int arr[] = new int[10];
public static void main(string a[]) {
system.out.println(arr[1];)
}
}
which statement is correct?
a. when compilation some error will occur.
b. it is correct when compilation but will cause error when running.
c. the output is zero.
d. the output is null.
(c)
題目:給出下面的代碼:
…
那個敘述是對的。
a. 編譯時將發生錯誤。
b. 編譯時正確但是運行時出錯。
c. 輸出為0。
d. 輸出為null
int型數組是類對象,它在類被加載時完成初始化,在前面題目中已經有敘述,由于是原始數據類型int,其初始值為0。
34、given the following code:
public class person{
int arr[] = new int[10];
public static void main(string a[]) {
system.out.println(arr[1]);
}
}
which statement is correct?
a. when compilation some error will occur.
b. it is correct when compilation but will cause error when running.
c. the output is zero.
d. the output is null.
(a)
給出下面的代碼:
…
哪些敘述是對的。
a. 編譯時出錯。
b. 編譯時正確而運行時出錯。
c. 輸出0。
d. 輸出null。
實例變量在類的一個實例構造時完成初始化,而且在類的靜態方法中不能直接訪問類的非靜態成員而只能訪問類成員(像上題中一樣),類的普通方法可以訪問類的所有成員和方法,而靜態方法只能訪問類的靜態成員和方法,因為靜態方法屬于類,而普通方法及成員變量屬于類的實例,類方法(靜態方法)不能使用屬于某個不確定的類的實例的方法和變量,在靜態方法里面沒有隱含的this,而普通方法有。
35、public class parent {
public int addvalue( int a, int b) {
int s;
s = a+b;
return s;
}
}
class child extends parent {
}
which methods can be added into class child?
a. int addvalue( int a, int b ){// do something...}
b. public void addvalue (){// do something...}
c. public int addvalue( int a ){// do something...}
d. public int addvalue( int a, int b )throws myexception {//do something...}
(bc)
題目:哪些方法可以加入類child中。
此題涉及方法重載(overload),方法重寫(override)以及類派生時方法重寫的規則。方法重載的規則是:一、參數列表必須不同,個數的不同完全可以,如果個數相同則參數類型的不同不能引起歧意,例如int 和long,float和double就不能作為唯一的類型不同;二、返回值可以不同,但是不能是重載時唯一的不同點(這點和c++中不同,c++中返回類型必須一致)。方法重寫發生在類繼承時,子類可以重寫一個父類中已有的方法,必須在返回類型和參數列表一樣時才能說是重寫,否則就是重載,java中方法重寫的一個重要而且容易被忽略的規則是重寫的方法的訪問權限不能比被重寫的方法的訪問權限低!重寫的另一個規則是重寫的方法不能比被重寫的方法拋棄(throws)更多種類的異常,其拋棄的異常只能少,或者是其子類,不能以拋棄異常的個數來判斷種類,而應該是異常類層次結果上的種類。此題中答案a的錯誤就是重寫的訪問權限比被重寫的方法的低,而b,c都屬于重載,d的錯誤在于比被重寫的方法拋棄了更多種類的異常。
36、a member variable defined in a class can be accessed only by the classes in the same package. which modifier should be used to obtain the access control?
a. private
b. no modifier
c. public
d. protected
(b)
題目:一個類中定義的成員變量只能被同一包中的類訪問。下面的哪些修飾符可以獲得需要的訪問控制。
參看前面的題目中的敘述。
37、a public member vairable called max_length which is int type, the value of the variable remains constant value 100. use a short statement to define the variable.
a. public int max_length=100;
b. final int max_length=100;
c. final public int max_length=100;
d. public final int max_length=100.
(d)
題目:共有成員變量max_length是一個int型值,變量的值保持常數值100。使用一個短聲明定義這個變量。
java中共有變量使用public定義,常量變量使用final,另外注意的是修飾符的順序,一個最完整的修飾是public static final int varial_a=100;這個順序不能錯,這和c++中也是不同的。而答案c恰恰錯在修飾符的順序上。
38、which expressions are correct to declare an array of 10 string objects?
a. char str[];
b. char str[][];
c. string str[];
d. string str[10];
(c)
題目:哪些表達式是聲明一個含有10個string對象的數組。
嚴格來說這個題目沒有給出一個正確的答案,唯一比較正確的是c,而完全滿足題目要求的應該是:string str[]=new string[10];
注意答案d的形式是不對的,這和c++也是不同的。
39、which fragments are correct in java source file?
a. package testpackage;
public class test{//do something...}
b. import java.io.*;
package testpackage;
public class test{// do something...}
c. import java.io.*;
class person{// do something...}
public class test{// do something...}
d. import java.io.*;
import java.awt.*;
public class test{// do something...}
(acd)
題目:下面的那個java源文件代碼片斷是對的。
java中的package語句必須是源文件中除去說明以外的第一條語句,導入包語句可以有幾個,但是必須位于package語句之后,其它類定義之前,一個源文件中可以有幾個類,但最多只能有一個是public的,如果有,則源文件的文件名必須和該類的類名相同。
40:
string s= "hello";
string t = "hello";
char c[] = {´h´,´e´,´l´,´l´,´o´} ;
which return true?
a. s.equals(t);
b. t.equals(c);
c. s==t;
d. t.equals(new string("hello"));
e. t==c.
(acd)
題目:哪些返回true。
這個在前面第10題的equals()方法和==操作符的討論中論述過。==操作符比較的是操作符兩端的操作數是否是同一個對象,而string的equals()方法比較的是兩個string對象的內容是否一樣,其參數是一個string對象時才有可能返回true,其它對象都返回假。需要指出的是由于s和t并非使用new創建的,他們指向內存池中的同一個字符串常量,因此其地址實際上是相同的(這個可以從反編譯一個簡單的測試程序的結果得到,限于篇幅不列出測試代碼和反編譯的分析),因此答案c也是正確的。