昨日翻译

“The good news is that the moment you decide that what you know is more important than what you have been taught to believe, you will have shifted gears in your quest for abundance. Success comes from within, not from without.”

——Elie Wiesel


“好消息是,当你意识到你所知道的事物比你被教导要相信的事物更重要时,你将在追求富足的过程中加速。成功来自内在,而非外在。”

——埃利·威塞尔

今日名言

“I never knew how to worship until I knew how to love.”

——Henry Ward Beecher


2019.04.30问题及解析


题目
public class Test {    public int add(int num1,int num2){        try{            return num1 + num2;        }catch (Exception e){            System.out.print("catch语句 ");        }finally {            System.out.print("finally语句块 ");        }        return 0;    }    public static void main(String[] args) {        Test test = new Test();        System.out.print("和是:" + test.add(1,2) + " ");    }}

下面代码的结果为:

A.catch语句块  和是:3

B.编译异常

C.finally语句块  和是:3

D.和是:3  finally语句块

答案与解析

1.相关知识

不管有没有出现异常,都会执行finally语句块

finally语句块都会在return返回前执行

catch语句没有捕获到相应异常时,不会执行语句块中的内容

2.源码分析

定义了一个Test类

定义了add方法传递整型参数num1、num2,返回整型返回值

返回 num1+num2

try catch捕获

catch语句块中输出catch语句

finally语句块中输出finally语句块

返回0

main方法

定义了一个Test实例

输出“和是: 调用test实例的add方法,传递参数1,2 ”

3.答案解析

System.out.print()是一个整体,因此内部内容未执行完时不会输出

首先调用add方法,先计算num1 + num2 = 3,但不返回

没有抛出异常不执行catch,执行finally,输出“finally语句块  ”,返回3

输出“和是:3  ”

最终输出:

finally语句块  和是:3

答案选:C


2019.05.05问题


int i;int sum=0;for(int i = 0;i<100; ++i, sum += i)

请问i的最终结果是?

A.100

B.101

C.99

D.以上答案都不正确


END