1.题目:下列程序test 类中的变量c 的最后结果为
public class Test {
public static void main(String args[]) {
int a = 10;
int b;
int c;
if (a > 50) {
b = 9;
}
c = b + a;
}
}
A 10 B 0 C 编译出错 D 19
答案:C
方法中的变量需要为它初始化才能使用,否则出现编译错误,当类中的变量,会默认给它A初始值。通过下图来理解一下。
①方法中的变量
会报这样的错误,The local variable a may not have been initialized,提示你变量没有初始化。
②当类中的变量,会默认给它初始值。
类似题目:class point {
int x;
boolean z;
void output() {
System.out.println(x);
System.out.println(z);
}
public static void main(String[] args) {
point pt=new point();
pt.output();
}
}
A 运行错误 B 0 true C 0 false D 00
答案:C
int类型默认值为0,boolean默认值为false。
宝剑锋从磨砺出,梅花香自苦寒来。--冯梦龙
加油!小芳同学