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 迴圈之外,因此會發生編譯錯誤。
