6~10題也都是基本概念,重要,仔細看

 

 

第六題:

Given:

public class Breaker {
    static String o = "";

    public static void main(String[] args) {
        z: o = o + 2;
        for (int x = 3; x < 8; x++) {
            if (x == 4)
                break;
            if (x == 6)
                break z;
            o = o + x;
        }
        System.out.println(o);
    }
}

What is the result?

A. 23
B. 234
C. 235
D. 2345
E. 2357
F. 23457
G. Compilation fails.

答案:

執行結果:答案中

題目範圍:基本概念

解析:

與第六題考的觀念一樣,考break 標記的用法
由於這裡
break z;z 標籤不在其範圍之內,找不到標籤,出現編譯錯誤
z: o = o + 2;
這一行可以解讀成 z: {o = o + 2;} 一行獨立一個區塊
只有在區塊內使用該標籤才有效

continue 也可以像break 這樣使用

 

第七題:

Given:

public class Breaker2 {
    static String o = "";

    public static void main(String[] args) {
        z: for (int x = 2; x < 7; x++) {
            if (x == 3)
                continue;
            if (x == 5)
                break z;
            o = o + x;
        }
        System.out.println(o);
    }
}

What is the result?

A. 2
B. 24
C. 234
D. 246
E. 2346
F. Compilation fails.

答案:

執行結果:答案中

題目範圍:基本概念

解析:

break :跳到區塊的外面
continue :跳到區塊的末端

 

第八題:

Given:

public class Spock {
    public static void main(String[] args) {
        Long tail = 2000L;
        Long distance = 1999L;
        Long story = 1000L;
        if ((tail > distance) ^ ((story * 2) == tail))
            System.out.print("1");
        if ((distance + 1 != tail) ^ ((story * 2) == distance))
            System.out.print("2");
    }
}

What is the result?

A. 1
B. 2
C. 12
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.

答案:

執行結果:答案中

題目範圍:基本概念

解析:

運算符號(^) 表示 XOR
true ^ true = false
true ^ false = true
false ^ true = true
false ^ false = false

雖然不影響這題的結果,不過要注意一下這題的Long 是使用WapperClass 來宣告的

 

第九題:

Given:

    String[] elements = {"for", "tea", "too"};
    String first = (elements.length>0) ? elements[0] : null;

What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. The variable first is set to null.
D. The variable first is set to elements[0].

答案:

執行結果:答案中

題目範圍:基本概念

解析:

( ? : ) 的條件判斷式
變數或物件 = <判斷條件> ? <true時的值> : <false時的值>

JAVA中,陣列可以使用屬性length 來取得陣列長度

 

第十題:

Given:

import java.util.*;

public class Quest {
    public static void main(String[] args) {
        String[] colors = { "blue", "red", "green", "yellow", "orange" };
        Arrays.sort(colors);
        int s2 = Arrays.binarySearch(colors, "orange");
        int s3 = Arrays.binarySearch(colors, "violet");
        System.out.print(s2 + "" + s3);
    }
}

What is the result?
A. 2-1
B. 2-4
C. 2-5
D. 3-1
E. 3-4
F. 3-5
G. Compilation fails.
H. An exception is thrown at runtime.

答案:

執行結果:答案中

題目範圍:基本概念

解析:

Arrays 類別提供了許多對陣列方便的操作
這題用到的sort(String[] s)方法會對陣列的內容以
快速排序法來排序
binarySearch(String[] s, <
搜尋目標>)必須要先排序好才可以使用,會回傳搜尋目標的索引值
當目標搜尋不到時,會回傳一個負值:(-(insertion point) - 1),意思就是搜尋值插入陣列時,應該位在的索引值的負值減一

例如:

{1,3,5,7,9},用Arrays.binarySearch()方法來找 4
但是陣列中沒有4 因此會回傳4 插入時該有的索引值的負值(-2)減一,也就是(-3)

題目中的字串排序後會是這樣blue green orange red yellow
搜尋的orange索引值是2,回傳2
搜尋的violet不在陣列中,回傳violet插入時該有的索引值的負值(-4)減一,也就是(-5)

arrow
arrow

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