close

SCJP 6.0 版的題庫好像分類的比較好,前幾題都是基本概念的題目

雖然都是基本概念,但是還是要仔細的看,錯在這裡真的會搥心肝喔

1~5題只要小心地在紙上跑流程,應該都可以輕鬆地得到正確答案

第一題:

Given:

35. String #name = "Jane Doe";
36. int $age = 24;
37. Double _height = 123.5;
38. double ~temp = 37.5;

Which two statements are true?
(Choose two.)

A. Line 35 will not compile.
B. Line 36 will not compile.
C. Line 37 will not compile.
D. Line 38 will not compile

答案:AD

執行結果:無

題目範圍:基本概念

解析:

簡單的變數命名規則
1.
只可以由文字(含中文)、數字、底線(_)、錢符號($)組成
2.
不可以數字開頭

 

第二題:

Given:

public class TestString1 {
    public static void main(String[] args) {
        String str = "420";
        str += 42;
        System.out.print(str);
    }
}

What is the output?

A. 42
B. 420
C. 462
D. 42042
E. Compilation fails.
F. An exception is thrown at runtime.

答案:

執行結果:答案中

題目範圍:字串的操作

解析:

字串可以用加(+) 來合併
數字、字元、物件等等與字串做運算時,都會自動轉為字串,物件會呼叫toString() 方法

 

第三題:

Given:

public class Test {
    public static void main(String[] args) {
        int x = 5;
        boolean b1 = true;
        boolean b2 = false;

        if ((x == 4) && !b2)
            System.out.print("1 ");
            System.out.print("2 ");
        if ((b2 = true) && b1)
            System.out.print("3 ");
    }
}

What is the result?

A. 2
B. 3
C. 1 2
D. 2 3
E. 1 2 3
F. Compilation fails.
G. An exception is thrown at runtime.

答案:

執行結果:答案中

題目範圍:基本概念

解析:

這題有兩個誤導人的地方

第一是印出"2" 的那一行縮排故意排錯,其實那已經是在if 判斷之外了,因此一定會執行到
另外是
(b2 = true)這邊,注意是單個等號(=)做指派數值,而不是判斷相等的雙等號(==)
指派數值這個動作,只要沒有出現錯誤,就會回傳true

 

第四題:

Given:

    public void go() {
        String o = "";
        z:
        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 2; y++) {
                if (x == 1)break;
                if (x == 2 && y == 1)break z;
                o = o + x + y;
            }
        }
        System.out.println(o);
    }

What is the result when the go() method is invoked?

A. 00
B. 0001
C. 000120
D. 00012021
E. Compilation fails.
F. An exception is thrown at runtime.

答案:

執行結果:答案中

題目範圍:基本概念

解析:

有個比較少見的用法 z:
這個用法很方便,可以在
break 時,跳出迴圈到指定的標記層
break 不再只能跳脫一層迴圈

 

第五題:

Given:

    int x = 0;
    int y = 10;
    while (x < 5) {
        y--;
        ++x;
    } ;
    System.out.print(x + "," + y);

What is the result?

A. 5,6
B. 5,5
C. 6,5
D. 6,6

答案:於2012/01/16更正,感謝網友提醒 

執行結果:答案中

題目範圍:基本概念

解析:

或許有些人會被唬到++x;

由於這個指令不是被寫在迴圈的判斷式中,或是指派數值指令上,這裡他是個獨立的指令
因此這題寫++x;x++;結果是完全一樣的

arrow
arrow

    yaya741228 發表在 痞客邦 留言(10) 人氣()