1 如下代码结果为
public class TestMain {
public static float aMethod(float i) throws Exception {
try {
return i/0;
} catch (Exception ex) {
throw new Exception("exception-in-method ");
} finally{
System.out.print("finally ");
}
}
public static void main(String[] args) {
try{
aMethod(10.5);
} catch (Exception ex) {
System.out.print("exception-in-main ");
}
System.out.print("finished ");
}
}
A.exception-in-main finished
B.finally finished
C.exception-in-method finally exception-in-main
D.finally exception-in-main
E.Compilation fails
Correct Answer: D
解析:
1)aMethod(10.5)中的参数10.5为double类型的
而方法要求时float类型的 所以会出现编译时异常
2)若改为aMethod(10.5f) 则程序正常执行
因为小数/0 不会出现异常
整数/0 会出现算数异常
那么结果就为B
2 Java创建对象共有几种方式
要使用clone方法 我们需要实现Cloneable接口
需要已经有一个分配了内存的对象 使用这个已经创建的对象调用clone()方法创建新对象
用clone方法创建对象并不会调用任何构造函数