Given:



public class Whizlabs{
    public static void main(String[] args){
        int sum = 0;
        
        for (int x = 0; x <= 10; x++)
            sum += x;
        System.out.print("Sum for 0 to" + x);
        System.out.print(" = " + sum);
    }
}

Which is true?

A.

Sum for 0 to 0 = 55

B.

Sum for 0 to 10 = 55

C. Compilation fails due to error on line 6.
D. Compilation fails due to error on line 7.
E. An Exception is thrown at the runtime.

題解

程式第7行使用到第5行for迴圈內宣告的x變數,由於x變數只在for迴圈內才可以被有效地存取,而程式第7行已經在for迴圈之外,因此會發生編譯錯誤。