SCJP-1 TEST

来源:百度知道 编辑:UC知道 时间:2024/06/05 09:48:58
Given:

1. class Ping extends Utils {
2. public static void main(String [] args) {
3. Utils u = new Ping();
4. System.out.print(u.getInt(args[0]));
5. }
6. int getInt(String arg) {
7. return Integer.parseInt(arg);
8. }
9. }

10. class Utils {
11. int getInt(String x) throws Exception { return 7; }
12. }

And the following three possible changes:

C1. Declare that main() throws an Exception.

C2. Declare that Ping.getInt() throws an Exception.

C3. Wrap the invocation of getInt() in a try / catch block.

Which change(s) allow the code to compile? (Choose all that apply.)

A. Just C1 is sufficient.

B. Just C2 is sufficient.

C. Just C3 is sufficient.

D. Both C1 and C2 are required.

E. Both C1 and C3 are required.

F. Both C2 and C3 are required.

因为第四行调用的是Utils中的getInt方法,而Utils中的getInt声明了一个Exception,所以在main方法中调用它之后必须也声明一个Exception或者用try catch来处理它。
对于Ping中的getInt方法,由于Integer.parseInt可能发生的异常是RuntimeException,所以可以不显示声明。
不知道你能否理解,如果还不理解,建议先看看异常相关章节,搞懂checked Exception和unchecked Exception再说。