1、下面语句将输出什么?
public class T2 {
long a[] = new long[10];
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(a[5]);
}
}
此段代码在编译的时候将会报错,因为静态方法中无法调用非静态属性值
可将long a[] = new long[10];改为 static long a[] = new long[10];
2、如下代码
public class T {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean flag = true;
if(flag = false){
System.out.println("false");
}
else{
System.out.println("true");
}
}
}
执行结果是什么?
可能有人觉得会报错,因为if里面的 flag = false是赋值语句 不是判断。而flag是boolean类型,即使赋值为false if 里面它还是boolean类型,所以是正确的。
如果是如下代码
public class T {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 10;
if(i=10){
System.out.println("a");
}else{
System.out.println("b");
}
}
}
将会报错,因为int类型无法转换为boolean型。
3、已知如下代码:
public class T {
public static void main(String[] args) {
// TODO Auto-generated method stub
switch(m){
case 0:{
System.out.println("is 0");
}
case 1:{
System.out.println("is 1");
}
case 2:{
System.out.println("is 2");
}
case 3:{
System.out.println("is 3");
break;
}
default :{
System.out.println("other");
}
}
}
}
当m的值为什么时输出is 2
A、0 B、1 C、2 D、3 E、4 F、None
答案为:ABC。 每个case语句后面的break不可少。
4、已知如下代码
class Base{
public Base(){
//...
}
public Base(int m){
//...
}
protected void fun(int m){
//...
}
}
class Child extends Base{
//...
}
下列哪些语句可以正确加入到子类中?
private void fun(int n){//...}
void fun(int n){//...}
protected void fun(int n){//...}
public void fun(int n){//...}
在java中四种作用域的范围大小依次是public protected friendly private
所以可以加入到子类中的是protected void fun(int n){//...} public void fun(int n){//...}