Interface Count{

short counter = 0;

void countUp();

}

public class TestCount implements Count{

public static void main(String[] args){

TestCount t = new TestCount();

t.countUp();

}

public void countUp(){

for (int x = 6; x>counter; x--, ++counter){

System.out.print(“ ” + counter);

}

}

}


1. 运行下列程序,会产生什么结果?
class StaticA

{

StaticA()

{

System.out.println("StaticA instructor!");

}

}



public class StaticExsample

{

static int a;

static String str = "abc";


StaticExsample()

{

System.out.println(str);

}


{

System.out.println("block initialized");

}


static StaticA sa1 = new StaticA();


static

{

System.out.println("static block initialized");

System.out.println(str);

str += "def";

}


static StaticA sa = new StaticA();



public static void main(String[] args)

{

new StaticExsample();

new StaticExsample();

}

}


A.
StaticA instructor!
static block initialized
abc
StaticA instructor!
block initialized
abcdef
block initialized
abcdef




2.看看下一列将要输出的结果:
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’;

}

}

good and gbc






下列代码哪一行会出错?
public void modify() {
int I,j,k;
I=100;
while (I>0) {
j=I*2;
System.out.println(" The value of j is "+j);
k=k+1;
I--;
}
}


C.line 7
编辑器会报错,k没有被赋初始值。



阅读以下代码,选择正确的答案
​ Interface Count{

short counter = 0;

void countUp();

}

public class TestCount implements Count{

public static void main(String[] args){

TestCount t = new TestCount();

t.countUp();

}

public void countUp(){

for (int x = 6; x>counter; x--, ++counter){

System.out.print(“ ” + counter);

}

}

}



E. Compilation fails







看看下面2个程序

public class Test {

String str = new String("good");
char [] ch = {'a','b','c'};

public static void main(String[] args) {
Test t = new Test();
t.change(t.str, t.ch);
System.out.print(t.str+"and");
System.out.print(t.ch);
}

public void change(String str,char ch[]){
str = "test ok";
ch[0] = 'g';
}
}

输出结果:

goodandabc


public class Test {

String str = new String("good");
char [] ch = {'a','b','c'};

public static void main(String[] args) {
Test t = new Test();
t.change(t.str, t.ch);
System.out.print(t.str+"and");
System.out.print(t.ch);
}

public void change(String str,char ch[]){
this.str = "test ok";
ch[0] = 'g';
}
}

输出的结果:

test okandgbc


这两个程序,关键的是在change()中一个有是str ,一个是this.str。
当使用this.str = "test ok"时,改变了Test类实例str变量的引用,所以结果打印出来的结果为"test okandabc";
t.ch没变,是因为数据类型是引用数据类型。
这些属于个人理解,要是不对还请你指出。