當前位置:才華齋>IT認證>SUN認證>

java認證考試試題及答案

SUN認證 閱讀(6.91K)

1. What gets printed when the following program

java認證考試試題及答案

is compiled and run?

class Test {

public static void main(String args[]) {

int i;

do {

i++;

} while (i < 0);

tln(i);

}

}

Select 1 correct answer:

A. The program does not compile as i is not initialized.

B. The program compiles but does not run.

C. The program compiles and runs but does not print anything.

D. The program prints 0.

E. The program prints 1.

答案:A:如果沒有初始化便使用基本變數型別,會導致編譯時異常,程式不能編譯。

2. What gets printed when the following program

is compiled and run?

public class XYZ {

public static void main(String args[]) {

int i,j,k;

for (i = 0; i < 3; i++)

{

for(j=1; j < 4; j++)

{

for(k=2; k<5; k++)

{

if((i == j) && (j==k))

tln(i);

} } } } }

Select 1 correct answer:

A. 0

B. 1

C. 2

D. 3

E. 4

答案:C

3. Given the following code :

class Base{}

public class MyCast extends Base{

static boolean b1=false;

static int i = -1;

static double d = 10.1;

public static void main(String argv[]){

MyCast m = new MyCast();

Base b = new Base();

//Here

}

}

Which of the following, if inserted at the comment //Here

will allow the code to compile and run without error?

Select 2 correct answers:

A. b = m;

B. m = b;

C. d = i;

D. b1 = i;

解析:A 從子型別到父型別的轉換是擴充套件引用轉換,不需要在執行時採取特殊的動作,不會在執行時丟擲異常。

B 從超型別到子型別的轉換是收縮引用轉換,需要在執行時執行測試,以查明實際的引用值是否是新型別的合法值.如果不是, 則會丟擲ClassCascException 。在這裡,b本身不是MyCast型別的值,因此會導致執行時異常。

C 從int到double的轉換是擴充套件基本轉換,基本型別之間的擴充套件轉換永遠不會導致執行時異常。但從int或long到float,或者是從long到double都可能導致精度丟失。

D 不允許進行int和boolean之間的型別轉換

答案:A、C

4. Given the following classes which of the following

will compile without error?

interface IFace{}

class CFace implements IFace{}

class Base{}

public class ObRef extends Base{

public static void main(String argv[])

{

ObRef ob = new ObRef();

Base b = new Base();

Object o1 = new Object();

IFace o2 = new CFace();

}

}

Select 3 correct answers:

A. o1 = o2;

B. b = ob;

C. ob = b;

D. o1 = b;

解析:A 任何物件都可以賦給Object型別的物件,正確

B 子類賦給超類是擴充套件引用轉換,可以進行

C 收縮引用轉換,錯誤

D 擴充套件引用轉換,可以進行

答案 A B D

5. What is the result of compiling and running the following code?

1 public class Test {

2 static int total = 10;

3 public static void main (String args []) {

4 new Test();

5 }

6 public Test () {

7 tln("In test");

8 tln(this);

9 int temp = l;

10 if (temp > 5) {

11 tln(temp);

12 }

13 }

14 }

Select 1 correct answer:

A. The class will not compile

B. The compiler reports an error at line 2

C. The compiler reports an error at line 9

D. The value 10 is one of the printed elements

E. The class compiles but generates a runtime error

答案:D 由於test類沒有override toString方法,故在呼叫println(this)時會直接顯示類的有關資訊。

6. Carefully examine the following code:

public class StaticTest {

static { tln("Hi there"); }

public void print() {

tln("Hello");

}

public static void main(String args []) {

StaticTest st1 = new StaticTest();

t();

StaticTest st2 = new StaticTest();

t();

}}

When will the string "Hi there" be printed?

Select 1 correct answer:

A. Never

B. Each time a new instance is created

C. Once, when the class is first loaded into the JVM

D. Only when the static method is called explicitly

答案 C

Java虛擬機器規範約定,類的初始化發生在首次主動使用時。靜態變數和靜態初始化塊的先後,實際上取決於它們在類中出現的先後順序。

7. Given the following code what is the effect of a being 5?

public class Test {

public void add(int a) {

loop: for (int i = 1; i < 3; i++){

for (int j = 1; j < 3; j++) {

if (a == 5) {

break loop;

}

tln(i * j);

}

}

}

}

Select 1 correct answer:

A. Generates a runtime error

B. Throws an ArrayIndexOutOfBoundsException

C. Prints the values: 1, 2, 2, 4

D. Produces no output

解析:考察標記和break的使用。單純使用break只跳出當前迴圈(一層)。如果在要跳出的迴圈前加標記,那麼在加過標記的迴圈中的任何地方都可以使用break 標記 來跳出該迴圈

答案:D

8. Given the following class definition:

public class Droitwich{

class one{

private class two{

public void main(){

tln("two");

}

}

}

}

Which of the following statements are true?

Select 1 correct answer:

A. The code will not compile because the classes are nested to

more than one level

B. The code will not compile because class two is marked as private

C. The code will compile and output the string two at runtime

D. The code will compile without error

答案:D

9. Given the following code:

class Base { static int oak=99; }

public class Doverdale extends Base{

public static void main(String argv[]){

Doverdale d = new Doverdale();

hod();

}

public void amethod(){

//Here

}

}

Which of the following if placed after the comment //Here,

will compile and modify the value of the variable oak?

Select all possible answers:

A. =1;

B. oak=33;

C. =22;

D. oak=50.1;

解析:當子類沒有重新定義靜態屬性則子類的靜態屬性與父類的靜態屬性為同一個變數

當子類重新定義了父類靜態屬性則子類的靜態屬性與父類的靜態屬性是兩個不同的變數

靜態方法呼叫的是定義這個靜態方法的類的靜態屬性。

答案:A B C

10. What will happen when you attempt to compile

and run the following code?

public class Inc {

public static void main(String argv[]) {

Inc inc = new Inc();

int i =0;

in(i);

i = i++;

tln(i);

}

void fermin(int i){

i++;

}

}

Select 1 correct answer:

A. Compile time error

B. Output of 2

C. Output of 1

D. Output of 0

答案:D

i++和++i是什麼意思?_百度知道

答 都是i=i+1的意思,區別在於i++是i先不自加,在語句完後自加,++i先自加

11. In the code fragment below, what is the value of k

after line 3 executes?

1 int i = -1;

2 int j = i >> 3;

3 int k = j & 129;

A. -1

B. 0

C. 129

D. A very large negative number

E. A very large positive number

解析:本題考察了整數在java虛擬機器中的二進位制表示。

計算機中的符號數有三種表示方法,即原碼、反碼和補碼。三種表示方法均有符號位和數值位兩部分,符號位都是用0表示“正”,用1表示“負”,而數值位,三種表示方法各不相同。

在計算機系統中,數值一律用補碼來表示和儲存。原因在於,使用補碼,可以將符號位和數值域統一處理;同時,加法和減法也可以統一處理。此外,補碼與原碼相互轉換,其運算過程是相同的,不需要額外的硬體電路。

正整數的補碼是其二進位制表示,與原碼相同,

求負整數的補碼,將其對應正數二進位制表示所有位取反(包括符號位,0變1,1變0)後加1。

簡單起見,我們不表示出所有32位,僅表示十二位。

i : -1 1::0000 0000 0001 -1: 1111 1111 11111

第二行,對i進行了移位操作,>>為帶符號右移,其移出的空位需要用符號位補齊。格式為 需要移位的數字>>要移的'位數

故j = 1111 1111 1111

第三行,&為按位與運算子,129=0000 1000 0001,k=0000 1000 0001,即129