public class MainTest {
public int aMethod(){
static int i = 0;
i++;
return i;
}
public static void main(String args[]){
MainTest test = new MainTest();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
what is the result?
A.0
B.1
C.2
D.Compilation fails
Explain:
在方法里面定义的变量是局部变量,就是说他有一定的作用范围和生命周期,就只能在方法里面使用而不能将其扩展到别的地方,这个变量在方法结束后就会被回收器回收,就不再存在了,而要强制将其加上static就是要把它的作用范围扩展到整个类,这就与开始定义这个变量时违背了,这是语法上的错误。
第二题:
public class MainTest extends Thread implements Runnable{
public void run(){
System.out.println("this is run");
}
public static void main(String[] args) {
Thread t = new Thread(new MainTest());
t.start();
}
}
A.第一行会产生编译错误
B.第六行会产生编译错误
C.第六行会产生运行错误
D.程序会运行和启动
Explain:
Thread类已经实现了Runnable接口
第三题:
public class MainTest{
public static void main(String[] args) {
new MainTest().makeThings();
}
void makeThings(){
TestA test = new TestA();
// ...code continues on
}
}
class TestA{
TestB b;
TestA(){
b = new TestB(this);
}
}
class TestB{
TestA a;
TestB(TestA a){
this.a = a;
}
}
which two statements are true after line 15, before main completes?(Choose two)
A.Line 15 causes a stack overflow.
B.An exception is thrown at runtime.
C.The object referenced by a is eligible for garbage collection.
D.The object referenced by b is eligible for garbage collection.
第四题:
哪种说法阐明Java内存回收机制?请选出正确的答案。
A.程序员必须手动释放内存对象
B.内存回收程序负责释放无用内存
C.内存回收程序允许程序员直接释放内存
D.内存回收程序可以在指定时间释放内存对象
Explain:
垃圾回收机制就是由JVM自动执行的,由JVM决定何时执行的 过程。程序员只能决定一个类在回收时所要做的动作(通过重载Object类的finalize()方法),以及提交JVM触发垃圾回收(通过System.gc(),但不保证会执行垃圾回收)。
第五题:
which statement is true?
A.catch(X x) can catch subclasses of X.
B.The Error class is a RuntimeException.
C.Any statement that can throw an Error must be eclosed in a try block.
D.Any statement that can throw an Exception must be enclosed in a try block.