以下代码结果是什么?
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
检查程序,是否存在问题,如果存在指出问题所在,如果不存在,说明输出结果。
|
- test okandabc
- test okandgbc
- goodandabc
- goodandgbc
答案:D
解析:创建字符串有两种方式;两种内存区域(heap,pool)
String str = new String("good");在堆内存中创建
String str = "test ok" ;在方法区的常量池中创建
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
解析:
vateString a = “aa”; // a 为成员变量的引用,在堆区,“aa”为未经 new 的常量,在常量区 publicboolean methodB() { String b = “bb”; // b 为局部变量的引用,在栈区,“bb”为未经 new 的常量,在常量区 final String c = “cc”; // c 为局部变量的引用,在栈区,“cc”为未经 new 的常量,在常量区 } |
以下代码输出的是:
|
- 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的值