java 测试题(一)

1.下列java代码中的变量a,b,c分别在内存当中的___区域 ,并用图示法表示


public class Demo {
    private String a = "aa";
    public boolean method(){
        String b = "bb";
        final String c = "cc";
    }
}

a在堆中,bc在栈

2.下面程序的输出结果为:


class A {
 double f(double x, double y) {
  return x * y;
 }
}

class B extends A {
 double f(double x, double y) {
  return x + y;
 }
}

public class Test {
 public static void main(String args[]) {
  A obj = new B();
  System.out.println(obj.f(4, 6));
 }
}

看main方法,数据类型是A,obj是名,创建了一个B类;由于B是A的子类且重写了f方法,所以输出应该是4+6,答案是10.

 

3.下列代码的输出是什么?并解释其原因


public class A {
    public String show(D obj) {
        return ("A and D");
    }
    public String show(A obj) {
        return ("A and A");
    } 
}

public class B extends A{
    public String show(B obj){
        return ("B and B");
    }
    
    public String show(A obj){
        return ("B and A");
    } 
}

public class C extends B{
}

public class D extends B{
}

public class Test {
    public static void main(String[] args) {
        A a1 = new A();
        A a2 = new B();
        B b = new B();
        C c = new C();
        D d = new D();
        
        System.out.println("1--" + a1.show(b));
        System.out.println("2--" + a1.show(c));
        System.out.println("3--" + a1.show(d));
        
        System.out.println("4--" + a2.show(b));
        System.out.println("5--" + a2.show(c));
        System.out.println("6--" + a2.show(d));
        
        System.out.println("7--" + b.show(b));
        System.out.println("8--" + b.show(c));
        System.out.println("9--" + b.show(d));      
    }
}

1.A and A   a1数据类型是A b数据类型是B,A中没有B,且B是A的子类,向上找,所以答案是A and A
2.A and A   a1数据类型是A c数据类型是C,A中没有C,向上找B,B也没有,然后再找A,所以答案是A and A
3.A and D  a1数据类型是A d数据类型是D,A中有D,所以答案是A and D
4.B and A   a2数据类型是A, b数据类型是B,A中没有B,向上找A,B重写了show(A)所以答案是B and A
5.B and A   a2数据类型是A, c数据类型是C,A中没有C,向上找B,再找A,B重写了show(A)所以答案是B and A
6.A and D   a2数据类型是A, d数据类型是D A中有D所以答案是A and D
7.B and A b数据类型是B b数据类型是B,B是A的子类,B中有A,所以答案是B and A
8.B and A b数据类型是B c数据类型是C,B中没C,向上找B,B是A的子类,向上找A,B中有A,所以答案是B and A
9.A and D b数据类型是B d数据类型是D B是A的子类,之中有D,所以答案是A and D

4.分析下面这段Java代码,它的运行结果是( )


 public class Test {
    public static void main(String[] args) {
        int i = 12;
        System.out.println(i+=i-=i*=i);  
    }
}

 看输出的那一句,我们可以把后边看作一个整体来算,首先i=i+(i-=i*=i),再分i=i-(i*=i)。然后我们可以开始计算,i*=i得144,然后12-144为-132,-132+12=-120.答案正是-120

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值