JAVA经典题目,对理解概念很有意思(自己总结)

1)

class ValHold{

        public int i = 10;

}

public class ObParm{

        public void amethod(){

                ValHold v = new ValHold();

                another(v);

                System.out.println(v.i);

        }

        public void another(ValHold v){

                v.i = 20;

                ValHold vh = new ValHold();

v =vh;

                System.out.println(v.i);

        }

        public static void main(String[] argv){

                ObParm o = new ObParm();

                o.amethod();

        }

}

程序运行结果为什么,如果编译错误,指出错误原因

答案:

10 

20.

 

 

 

2)

class A {

       public static void a() {

       System.out.println("super!");

       }

       public String toString() {

       return “a!”;

}

}

class TestStatic extends A {

       public static void a() {

              System.out.println("child!");

       }

       public static void main( String[] args) {

              A obj = new TestStatic();

        System.out.println(obj);

              obj.a();

       }

}

输出结果,如果编译错误指出错误原因.

答案:

static,输出

a!

super!

 

 

 

3)

public class Inc{

       public static void main(String argv[]){

                Inc inc = new Inc();

                int i =0;

                inc.fermin(i);

                i = i++;

                System.out.println(i);

        }

        void fermin(int i){

                i++;

        }

}

 

 

 

输出结果,如果编译错误指出错误原因

答案:

0;

 

 

 

4)

 

 

 

public class Agg{

  static public int i=10; 

  public static void main(String argv[]){

        switch(i){

                case 1:

                    System.out.println("one"); 

                default:

                    System.out.println("default");

                case 10:

                    System.out.println("ten");

        }

  }

}

输出结果:

 

答案:

default, ten.

 

 

 

5)

boolean flag = false;

if (flag = true) {

   System.out.println("true");

}else{

   System.out.println("false");

}

输出结果,如果编译错误,指出错误所在..

答案:

True

 

 

 

6)

public class Test {

  public static void test() {

    this.print();

  }

public static void print() {

    System.out.println("Test");

  }

public static void main(String args []) {

    test();

  }

}

输出结果,如果编译错误,指出错误所在.

答案

编译错误,静态方法不可使用this

 

 

 

7)

1.StringBuffer sb = new StringBuffer("abc");

2.String s = new String("abc");

3.sb.append("def");

4.s.append("def");

5.sb.insert(1, "zzz");

6.s.concat(sb);

7.s.trim();

 

 

 

指出哪些非法,说明理由

答案

46非法

理由:String属于包裹类,也称为不可变类

 

 

 

8)数组的初始化下面哪些是错误的

    1     int a[] = {1,2,3};

    2   int []b[] = {{1,2,3},{1,2,3}};

    3   int d[2] = {1,2,3};  

    4   int d[] = {1,2,3};

    5   int []e = {0,0,0};

6  char c[] = {'a', 'b'}; 

错误为,并说明理由

答案:

3

 

 

 

9)

public class Test8 {

     public static void main(String [] args){

         Base b = new Subclass();

         System.out.println(b.x);

         System.out.println(b.method());

     }

}

class Base{

     int x = 2;

     int method(){

         return x;

     }

}

class Subclass extends Base{

     int x = 3;

     int method(){

         return x;

     }

}

指出运行结果:

2

3

 

 

 

10)

abstract class MineBase {

  abstract void amethod();

  static int i;

}

public class Mine extends MineBase{

  public static void main(String argv[]){

    int[] ar = new int[5];

    for(i = 0;i < ar.length;i++)

       System.out.println(ar[i]);

    }

}

指出运行结果,如果编译错误指出错误原因

答案:

编译错误,抽象类必须重写

 

 

 

11)

class Base {

       //void speak() {

       //     System.out.println("Base");

       //     }

       }

class Sub extends Base {

       void speak() {

              System.out.println("Sub");

              }

       }

public class Test9{

  public static void main(String argv[]){

    Base b = new Sub(); 

b.speak(); 

  }

}

指出运行结果,如果编译错误指出错误原因

编译错误,父类没有类似方法.

 

 

 

11)

public class Arg{

    String[] MyArg;

    public static void main(String argv[]){

             MyArg=argv; 

    }

    public void amethod(){

        System.out.println(argv[1]);  

    }                             

}

指出运行结果,如果编译错误指出错误原因

编译错误,静态方法不可以调用动态属性

 

 

 

 

 

 

12)

public class Test{

       public static void main(String[] args) {

              amethod();

       }

       static void amethod(){

              try{

                     System.out.println("abcd");

                     return;

              }finally {

                     System.out.println("123456");

              }

       }

}

 

 

 

指出运行结果,如果编译错误指出错误原因

答案:

abcd

123456

 

 

 

13)

class XXX {

   public static void main(String[] args) {

     String s1 = "abcde";

     String s2 = "abcde";

     s1.toUpperCase();

     if (s1 == s2)

       System.out.println("YES");

     else

       System.out.println("NO");

   }

}

 

 

 

指出输出结果,如果编译错误指出错误原因.

答案:

YES

 

 

 

14)

 

 

 

1. public class Papa {

 2.   int i;

 3.   Papa(int j) { i = j; }

 4. }

 5.

 6. class Child extends Papa {

 7.   Child() { i = 5; }  

 8.   public static void main(String[] args) {

 9.     new Child();  

10.   }

11. }

 

 

 

指出输出结果,如果编译错误指出错误原因和所在行

答案:

编译错误,父类构造函数没有初始化

 

 

 

15)

public class T{

       public static void main(String[] arg){

              StringBuffer a = new StringBuffer ("A");

              StringBuffer b = new StringBuffer ("B");

              operate (a,b);

              System.out.println(a + "," +b);

       }

static void operate (StringBuffer x, StringBuffer y)  {

              x.append (y);

              y = x;

       }

}

 

 

 

指出输出结果,如果编译错误指出错误原因

输出

AB,B

 

 

 

16)

1. String s1 = "abc";

2. String s2 = s1;

3. String s3 = "abc";

在三个声明中将有几个对象产生

答案:1

 

 

 

 

 

 

16)

public class StrEq{

public static void main(String argv[]){

        StrEq s = new StrEq();

    }

    private StrEq(){

         String s = "Marcus";

         String s2 = new String("Marcus");

         if(s == s2){

               System.out.println("we have a match");

          }else{

               System.out.println("Not equal");

          }

    }

}

 

 

 

指出输出结果,如果编译错误指出错误原因.

答案:

Not equal

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值