错题整理--成员变量与局部变量

以下代码结果是什么?

public class foo {

public static void main(String sgf[]) {

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.AB”。
  • 代码可以编译运行,输出“A.A”。
  • 代码可以编译运行,输出“AB.B”。
  • 代码可以编译运行,输出“A.B”。

答案:C

解析:

下列代码的执行结果是()

public class Test {

    public static int a = 1;

    public static void main(String[] args) {

        int a = 10;

        a++; Test.a++;

        Test t=new Test();

        System.out.println("a=" + a + " t.a=" + t.a);

    }

}

  • a=10 t.a=3
  • a=11 t.a=2
  • a=12 t.a=1
  • a=11 t.a=1

答案:B

解析:值为1的a属于类变量也叫作成员变量,值为10的a是局部变量首先a++时就近原则,用局部变量10来加。Text.a直接用类名调用成员变量的情况,只能是static修饰的成员变量1来加,然后又实例化了对象,在输出中第一个a= 用就近原则输出11,第二个t.a 对象只能调用类变量输出2

检查程序,是否存在问题,如果存在指出问题所在,如果不存在,说明输出结果。

package algorithms.com.guan.javajicu; 

public class Example { 

  String str = new String("good"); 

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

  public static void main(String[] args) { 

     Example ex = new Example(); 

     ex.change(ex.str, ex.ch); 

     System.out.print(ex.str +"and"); 

     System.out.print(ex.ch);  

  } 

  public void change(String str, char ch[]){ 

     str= "test ok"; 

     ch[0]= 'g'; 

  } 

  • test okandabc
  • test okandgbc
  • goodandabc
  • goodandgbc

答案:D

解析:创建字符串有两种方式;两种内存区域(heap,pool)
String str = new String("good");在堆内存中创建
String str = "test ok" ;在方法区的常量池中创建

https://uploadfiles.nowcoder.net/images/20180119/6118706_1516326219645_701DD924C073AB9E931A69B78D077BD3

given the following code,what will be the output?

class Value{

    public int i=15;

}

public class Test{

    public static void main(String argv[]){

        Test t=new Test( );

        t.first( );

    }

 

public void first( ){

    int i=5;

    Value v=new Value( );

    v.i=25;

    second(v,i);

    System.out.println(v.i);

}

 

public void second(Value v,int i){

    i = 0;

    v.i = 20;

    Value val = new Value( );

    v = val;

    System.out.println(v.i+" "+i);

   }

}

  • 15 0 20
  • 15 0 15
  • 20 0 20
  • 0 15 20

解析:

https://uploadfiles.nowcoder.net/images/20160817/6316247_1471446548929_2F9F7E31EC51C5C37F5962B0F07D6A39

vateString a = “aa”; // a 为成员变量的引用,在堆区,“aa”为未经 new 的常量,在常量区

  publicboolean methodB() {

 String b = “bb”; // b 为局部变量的引用,在栈区,“bb”为未经 new 的常量,在常量区

 final String c = “cc”; // c 为局部变量的引用,在栈区,“cc”为未经 new 的常量,在常量区

    }

以下代码输出的是:

public class SendValue{

    public String str="6";

    public static void main(String[] args) {

        SendValue sv=new SendValue();

        sv.change(sv.str);

        System.out.println(sv.str);

    }

    public void change(String str) {

        str="10";

    }

}

  • 6
  • 10
  • 都不对
  • 16

检查程序,是否存在问题,如果存在指出问题所在,如果不存在,说明输出结果。

package algorithms.com.guan.javajicu; 

public class Inc { 

    public static void main(String[] args) { 

       Inc inc = new Inc(); 

       int i = 0; 

       inc.fermin(i); 

       i= i ++; 

       System.out.println(i);

    

    } 

    void fermin(int i){ 

       i++; 

    } 

}

  • 0
  • 1
  • 2
  • 3

解析:fermin方法没有影响到i的值

猜你喜欢

转载自blog.csdn.net/hd12370/article/details/81115606
今日推荐