java面试题分析

第一题:
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.


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值