SCJP(OCPJP) 考古題解析 第4題

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.

答案:


解析:

Label迴圈z

因為 break z; 

便跳出整個Label z迴圈

就不會再進行 o = o + x + y

所以最後一次印出的是20



0 意見:

張貼留言