当前位置: 技术问答>java相关
请教一道SCJP的模拟题?
来源: 互联网 发布时间:2015-05-29
本文导语: 6. Given the following incomplete method. 1. public void method(){ 2. 3. if (someTestFails()){ 4. 5. } 6. 7.} You want to make this method throw an IOExce...
6. Given the following incomplete method.
1. public void method(){
2.
3. if (someTestFails()){
4.
5. }
6.
7.}
You want to make this method throw an IOException if, and only if, the
method someTestFails() returns a value of true.
Which changes achieve this?
A. Add at line 2: IOException e;
B. Add at line 4: throw e;
C. Add at line 4: throw new IOException();
D. Add at line 6: throw new IOException();
E. Modify the method declaration to indicate that an object of [type]
Exception might be thrown.
答案:DE. (E suppose they mean the method declaration for someTestFails.)
-----
为什么是D而不是C?
1. public void method(){
2.
3. if (someTestFails()){
4.
5. }
6.
7.}
You want to make this method throw an IOException if, and only if, the
method someTestFails() returns a value of true.
Which changes achieve this?
A. Add at line 2: IOException e;
B. Add at line 4: throw e;
C. Add at line 4: throw new IOException();
D. Add at line 6: throw new IOException();
E. Modify the method declaration to indicate that an object of [type]
Exception might be thrown.
答案:DE. (E suppose they mean the method declaration for someTestFails.)
-----
为什么是D而不是C?
|
我觉得是CE
|
如果someTestFails()返回的不是boolean 而是int,long,或void ,那么发生compile time error.
所以if and only if the method someTestFails() returns a value of true
then this code can compile.
then at line 6 you can throw the exception
所以if and only if the method someTestFails() returns a value of true
then this code can compile.
then at line 6 you can throw the exception