昨日翻译
“Where there is love there is life. ”
——Mahatma Gandhi
“有爱就有生命。”
——圣雄甘地
“ Love is our true destiny. We do not find the meaning Of life by ourselves alone- We find it with another. ”
—— Thomas Merton
public class Test {
public static void main(String[] args) {
System.out.println(num());
}
public static int num(){
int num = 3;
try{
num = num / 0;
}catch (Exception e){
num = 4;
}finally {
num = 5;
}
return num;
}
}
请问上述程序的输出结果是什么?、
A.编译错误
B.3
C.4
D.5
异常的执行顺序为try捕获到异常则执行catch语句,未捕获到则不执行catch语句,但无论如何都会执行fianlly语句。
num/0,分母不能为0,因此抛出异常
被捕获后,num赋值为4
执行finally,num赋值为5
返回5,输出5
答案选D。
2019.07.11问题
List——ArrayList、LinkedList
public class Test {
public static void main(String[] args) {
int num = 1;
List arrayList = new ArrayList();
List linkedList = new LinkedList();
long t1 = System.currentTimeMillis();
for(int i = 0; i < 5000; i++){
arrayList.add(0,num);
}
long t2 = System.currentTimeMillis() - t1;
t1 = System.currentTimeMillis();
for(int i = 0; i < 5000; i++){
linkedList.add(0,num);
}
long t3 = System.currentTimeMillis() - t1;
System.out.print(t2 > t3);
}
}
请问上述代码的输出结果为:
A.true
B.false
C.编译错误
D.运行异常
END