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)
題目:下面的哪些賦值語(yǔ)句是對的。
浮點(diǎn)數的賦值是帶有小數點(diǎn)的數字缺省是double型的,如果在浮點(diǎn)數后面加f或者F則是float,后面加d或者D則是double,科學(xué)計數法形式的浮點(diǎn)數也是double型的,而double的精度比f(wàn)loat高,將一個(gè)高精度的double賦值給一個(gè)低精度的float時(shí)需要進(jìn)行強制類(lèi)型轉換,反之則不需要。
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)
題目:給出下面的不完整的類(lèi)代碼:
…
下面的哪些表達式可以加到構造方法中的"doing the same as..."處?
在同一個(gè)類(lèi)的不同構造方法中調用該類(lèi)的其它構造方法需要使用this(…)的形式,而且必須是在構造方法的第一行調用,這個(gè)和普通的方法重載調用的方式不同,普通的方法可以直接使用方法名加參數來(lái)調用,而且調用位置沒(méi)有限制,因此答案A是不行的,B的語(yǔ)法就是錯誤的,D的錯誤在于在父類(lèi)型的構造函數被調用前不能引用類(lèi)的成員。構造方法是一個(gè)類(lèi)對象實(shí)例化的起點(diǎn)(雖然嚴格來(lái)說(shuō)首先執行的并不是構造方法的第一個(gè)語(yǔ)句,而是內存的分配),因此在構造方法中不能將成員作為參數引用。
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)
題目:下面關(guān)于變量及其范圍的陳述哪些是對的。
A. 實(shí)例變量是類(lèi)的成員變量。
B. 實(shí)例變量用關(guān)鍵字static聲明。
C. 在方法中定義的局部變量在該方法被執行時(shí)創(chuàng )建
D. 局部變量在使用前必須被初始化。
類(lèi)中有幾種變量,分別是:局部變量(英文可以為:local\automatic\temporary\stack variable)是定義在方法里的變量;實(shí)例變量(英文為:instance variable)是在方法外而在類(lèi)聲明內定義的變量,有時(shí)也叫成員變量;類(lèi)變量(英文為:class variable)是用關(guān)鍵字static聲明的實(shí)例變量,他們的生存期分別是:局部變量在定義該變量的方法被調用時(shí)被創(chuàng )建,而在該方法退出后被撤銷(xiāo);實(shí)例變量在使用new Xxxx()創(chuàng )建該類(lèi)的實(shí)例時(shí)被創(chuàng )建,而其生存期和該類(lèi)的實(shí)例對象的生存期相同;類(lèi)變量在該類(lèi)被加載時(shí)被創(chuàng )建,不一定要用new Xxxx()創(chuàng )建,所有該類(lèi)的實(shí)例對象共享該類(lèi)變量,其生存期是類(lèi)的生存期。任何變量在使用前都必須初始化,但是需要指出的是局部變量必須顯式初始化,而實(shí)例變量不必,原始類(lèi)型的實(shí)例變量在該類(lèi)的構造方法被調用時(shí)為它分配的缺省的值,整型是0,布爾型是false,而浮點(diǎn)型是0.0f,引用類(lèi)型(類(lèi)類(lèi)型)的實(shí)例變量的缺省值是null(沒(méi)有進(jìn)行實(shí)際的初始化,對它的使用將引起NullPointException),類(lèi)變量的規則和實(shí)例變量一樣,不同的是類(lèi)變量的初始化是在類(lèi)被加載時(shí)。
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塊中的語(yǔ)句在執行時(shí)發(fā)生異常,則執行從該處中斷而進(jìn)入catch塊,根據異常的類(lèi)型進(jìn)行匹配,最前面的優(yōu)先進(jìn)行匹配比較,只要該異常是catch中指定的異常的子類(lèi)就匹配成功進(jìn)而執行相應的catch中的內容,而finally塊中的內容無(wú)論是否發(fā)生異常都將被執行。
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循環(huán)和 -- 操作符的知識,do…while最少被執行一次,在執行完do中的內容后判斷while中的條件是否為true,如果為true的話(huà)就再執行do中的內容,然后再進(jìn)行判斷,以此類(lèi)推直到while的判斷為false時(shí)退出循環(huán)執行循環(huán)后面的內容,而—操作符的規則是在變量右邊的-- 將先進(jìn)行運算,然后才是使變量的值減一,而在變量左邊的是先將變量的值減一再運算。
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)
題目:下面的有關(guān)聲明的哪些敘述是對的。
A. 對原始數據類(lèi)型例如boolean,byte的變量的聲明不會(huì )為該變量分配內存空間。
B. 對原始數據類(lèi)型例如boolean,byte的變量的聲明將為之分配內存空間。
C. 非原始數據類(lèi)型例如String,Vector的變量的聲明不會(huì )為該對象分配內存。
D. 非原始數據類(lèi)型例如String,Vector的變量的聲明會(huì )為該對象分配內存。
對原始數據類(lèi)型的變量的聲明將為之分配內存并賦予一個(gè)缺省值,參見(jiàn)23題的敘述,而非原始數據類(lèi)型的變量必須用new Xxxx()分配內存及初始化。但是嚴格來(lái)講此題的答案有待確定,因為只有原始類(lèi)型的實(shí)例變量和類(lèi)變量的聲明在類(lèi)對象被創(chuàng )建/類(lèi)被加載時(shí)完成內存的自動(dòng)分配,而原始類(lèi)型的局部變量必須顯式初始化,從這點(diǎn)來(lái)看原始類(lèi)型的局部變量沒(méi)有被自動(dòng)分配內存,SL275中只提出了非原始數據類(lèi)型的變量必須使用new Xxxx()完成內存的分配而沒(méi)有指出原始數據類(lèi)型的變量是否在聲明時(shí)即自動(dòng)進(jìn)行內存分配,而從局部變量不能在顯式初始化前使用這點(diǎn)來(lái)看在聲明時(shí)沒(méi)有進(jìn)行內存分配。因此答案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. 類(lèi)及用途的描述
B. 父類(lèi)的方法的列表
C. 成員變量的列表
D. 類(lèi)層次
類(lèi)文檔的內容主要是:類(lèi)層次、類(lèi)及用途描述、成員變量列表、構造方法列表、成員方法列表、從類(lèi)層次上繼承的方法列表、成員變量的詳細說(shuō)明、構造方法詳細說(shuō)明、成員方法詳細說(shuō)明。
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)
題目:給出下面的代碼:
…
哪些行在編譯時(shí)可能產(chǎn)生錯誤。
這個(gè)問(wèn)題在前面有關(guān)變量的類(lèi)型及其作用域的問(wèn)題中討論過(guò),局部變量在使用前必須顯式初始化,而代碼中的變量k在使用前沒(méi)有。
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)
題目:下面有關(guān)變量及其作用域的陳述哪些是對的。
A. 在方法里面定義的局部變量在方法退出的時(shí)候被撤銷(xiāo)。
B. 局部變量也叫自動(dòng)變量。
C. 在方法外面定義的變量(譯注:即實(shí)例變量)在對象被構造時(shí)創(chuàng )建。
D. 在方法中定義的方法的參變量只要該對象被需要就一直存在。
本題還是討論變量的類(lèi)型及作用域,參看前面的敘述。
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)
題目:類(lèi)的設計要求它的某個(gè)成員變量不能被外部類(lèi)直接訪(fǎng)問(wèn)。應該使用下面的哪些修飾符獲得需要的訪(fǎng)問(wèn)控制。
這個(gè)在前面也有敘述,java有四種訪(fǎng)問(wèn)類(lèi)型,分別為:public,protected,default,private,其中public變量可以被所有的外部類(lèi)訪(fǎng)問(wèn),而pretected的可以被同一個(gè)包及該類(lèi)的子類(lèi)訪(fǎng)問(wèn),default即沒(méi)有任何修飾符的變量可以被同一個(gè)包中的類(lèi)訪(fǎng)問(wèn),而private變量只能在被該類(lèi)內部被訪(fǎng)問(wèn)。題目中的外部類(lèi)應該理解為除該類(lèi)自身的所有其它類(lèi),因此只有使用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)
題目:給出下面的代碼片斷:
…
哪些行將導致錯誤。
此題需要將代碼仔細看清楚,查詢(xún)沒(méi)有邏輯錯誤,if …else的使用沒(méi)有問(wèn)題,也沒(méi)有拼寫(xiě)錯誤,錯誤在于第5行的“與”操作符的使用,邏輯操作符(logical operator)的“與”應該是&&,而&是位邏輯操作符(bitwise logical operator)的“與”,使用的對象不一樣,邏輯操作符的“與”的左右操作數都應該是布爾型(logical boolan)的值,而位邏輯操作符的左右操作數都是整型(integral)值。
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)
題目:下面有關(guān)java代碼安全性的敘述哪些是對的。
A. 字節碼校驗器加載查詢(xún)執行需要的所有類(lèi)。
B. 運行時(shí)解釋器執行代碼。
C. 在運行時(shí),字節碼被加載,驗證然后在解釋器里面運行。
D. 類(lèi)加載器通過(guò)分離本機文件系統的類(lèi)和從網(wǎng)絡(luò )導入的類(lèi)增加安全性。
SL275中描述的Java程序運行的過(guò)程是這樣的:類(lèi)加載器(class loader)加載程序運行所需要的所有類(lèi),它通過(guò)區分本機文件系統的類(lèi)和網(wǎng)絡(luò )系統導入的類(lèi)增加安全性,這可以限制任何的特洛伊木馬程序,因為本機類(lèi)總是先被加載,一旦所有的類(lèi)被加載完,執行文件的內存劃分就固定了,在這個(gè)時(shí)候特定的內存地址被分配給對應的符號引用,查找表(lookuo table)也被建立,由于內存劃分發(fā)生在運行時(shí),解釋器在受限制的代碼區增加保護防止未授權的訪(fǎng)問(wèn);然后字節碼校驗器(byte code verifier)進(jìn)行校驗,主要執行下面的檢查:類(lèi)符合JVM規范的類(lèi)文件格式,沒(méi)有違反訪(fǎng)問(wèn)限制,代碼沒(méi)有造成堆棧的上溢或者下溢,所有操作代碼的參數類(lèi)型都是正確的,沒(méi)有非法的數據類(lèi)型轉換(例如將整型數轉換成對象類(lèi)型)發(fā)生;校驗通過(guò)的字節碼被解釋器(interpreter)執行,解釋器在必要時(shí)通過(guò)運行時(shí)系統執行對底層硬件的合適調用。后三個(gè)答案是SL275中的原話(huà)。
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)
題目:給出下面的代碼:
…
那個(gè)敘述是對的。
A. 編譯時(shí)將發(fā)生錯誤。
B. 編譯時(shí)正確但是運行時(shí)出錯。
C. 輸出為0。
D. 輸出為null
int型數組是類(lèi)對象,它在類(lèi)被加載時(shí)完成初始化,在前面題目中已經(jīng)有敘述,由于是原始數據類(lèi)型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. 編譯時(shí)出錯。
B. 編譯時(shí)正確而運行時(shí)出錯。
C. 輸出0。
D. 輸出null。
實(shí)例變量在類(lèi)的一個(gè)實(shí)例構造時(shí)完成初始化,而且在類(lèi)的靜態(tài)方法中不能直接訪(fǎng)問(wèn)類(lèi)的非靜態(tài)成員而只能訪(fǎng)問(wèn)類(lèi)成員(像上題中一樣),類(lèi)的普通方法可以訪(fǎng)問(wèn)類(lèi)的所有成員和方法,而靜態(tài)方法只能訪(fǎng)問(wèn)類(lèi)的靜態(tài)成員和方法,因為靜態(tài)方法屬于類(lèi),而普通方法及成員變量屬于類(lèi)的實(shí)例,類(lèi)方法(靜態(tài)方法)不能使用屬于某個(gè)不確定的類(lèi)的實(shí)例的方法和變量,在靜態(tài)方法里面沒(méi)有隱含的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)
題目:哪些方法可以加入類(lèi)Child中。
此題涉及方法重載(overload),方法重寫(xiě)(override)以及類(lèi)派生時(shí)方法重寫(xiě)的規則。方法重載的規則是:一、參數列表必須不同,個(gè)數的不同完全可以,如果個(gè)數相同則參數類(lèi)型的不同不能引起歧意,例如int 和long,float和double就不能作為唯一的類(lèi)型不同;二、返回值可以不同,但是不能是重載時(shí)唯一的不同點(diǎn)(這點(diǎn)和c++中不同,c++中返回類(lèi)型必須一致)。方法重寫(xiě)發(fā)生在類(lèi)繼承時(shí),子類(lèi)可以重寫(xiě)一個(gè)父類(lèi)中已有的方法,必須在返回類(lèi)型和參數列表一樣時(shí)才能說(shuō)是重寫(xiě),否則就是重載,java中方法重寫(xiě)的一個(gè)重要而且容易被忽略的規則是重寫(xiě)的方法的訪(fǎng)問(wèn)權限不能比被重寫(xiě)的方法的訪(fǎng)問(wèn)權限低!重寫(xiě)的另一個(gè)規則是重寫(xiě)的方法不能比被重寫(xiě)的方法拋棄(throws)更多種類(lèi)的異常,其拋棄的異常只能少,或者是其子類(lèi),不能以?huà)仐壆惓5膫€(gè)數來(lái)判斷種類(lèi),而應該是異常類(lèi)層次結果上的種類(lèi)。此題中答案a的錯誤就是重寫(xiě)的訪(fǎng)問(wèn)權限比被重寫(xiě)的方法的低,而b,c都屬于重載,d的錯誤在于比被重寫(xiě)的方法拋棄了更多種類(lèi)的異常。
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)
題目:一個(gè)類(lèi)中定義的成員變量只能被同一包中的類(lèi)訪(fǎng)問(wèn)。下面的哪些修飾符可以獲得需要的訪(fǎng)問(wèn)控制。
參看前面的題目中的敘述。
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是一個(gè)int型值,變量的值保持常數值100。使用一個(gè)短聲明定義這個(gè)變量。
Java中共有變量使用public定義,常量變量使用final,另外注意的是修飾符的順序,一個(gè)最完整的修飾是public static final int varial_a=100;這個(gè)順序不能錯,這和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)
題目:哪些表達式是聲明一個(gè)含有10個(gè)String對象的數組。
嚴格來(lái)說(shuō)這個(gè)題目沒(méi)有給出一個(gè)正確的答案,唯一比較正確的是c,而完全滿(mǎn)足題目要求的應該是: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)
題目:下面的那個(gè)java源文件代碼片斷是對的。
Java中的package語(yǔ)句必須是源文件中除去說(shuō)明以外的第一條語(yǔ)句,導入包語(yǔ)句可以有幾個(gè),但是必須位于package語(yǔ)句之后,其它類(lèi)定義之前,一個(gè)源文件中可以有幾個(gè)類(lèi),但最多只能有一個(gè)是public的,如果有,則源文件的文件名必須和該類(lèi)的類(lèi)名相同。
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。
這個(gè)在前面第10題的equals()方法和==操作符的討論中論述過(guò)。==操作符比較的是操作符兩端的操作數是否是同一個(gè)對象,而String的equals()方法比較的是兩個(gè)String對象的內容是否一樣,其參數是一個(gè)String對象時(shí)才有可能返回true,其它對象都返回假。需要指出的是由于s和t并非使用new創(chuàng )建的,他們指向內存池中的同一個(gè)字符串常量,因此其地址實(shí)際上是相同的(這個(gè)可以從反編譯一個(gè)簡(jiǎn)單的測試程序的結果得到,限于篇幅不列出測試代碼和反編譯的分析),因此答案c也是正確的。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。