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 循环之外,因此会发生编译错误。
