ocjp考试题库

上传人:h*** 文档编号:75248044 上传时间:2022-04-15 格式:DOC 页数:66 大小:602KB
收藏 版权申诉 举报 下载
ocjp考试题库_第1页
第1页 / 共66页
ocjp考试题库_第2页
第2页 / 共66页
ocjp考试题库_第3页
第3页 / 共66页
资源描述:

《ocjp考试题库》由会员分享,可在线阅读,更多相关《ocjp考试题库(66页珍藏版)》请在装配图网上搜索。

1、WORD格式1.Given:1. public class returnIt 2. returnType methodA(byte x, double y)3. return (short) x/y * 2;4. 5. What is the valid returnType for methodA in line 2?A. intB. byteC. longD. shortE. floatF. doubleAnswer F注释:short 类型的 x,除以 double 类型的 y,再乘 int 的 2,所以结果是 double 类型的。注意第三行的强制转换,只是转换了 x。2.1) cla

2、ss Super2) public float getNum()return 3.0f;3) 4)5) public class Sub extends Super6)7) which method, placed at line 6, will cause a compiler error?A. public float getNum()return 4.0f;B. public void getNum()C. public void getNum(double d)D. public double getNum(float d)return 4.0d;Answer :B注意这道题主要考的是

3、方法的 overload 和 override。对于 overload,只有参数列表不同,才做为标准,而返回值和访问控制关键字不能做为标准,所以 B 错在方法名相同,但只有返回值不同,这是错的。 C 和 D 是正确的 overload。对于 override ,则访问控制关键字只能更加公有化, 异常只能是超类方法抛出的异常的子类,也可以不抛出。返回类型,参数列表必须精确匹配。所以 A 是正确的 override。3.1)public class Foo2) public static void main(String args)专业资料整理3) tryreturn;4) finally Sys

4、tem.out.println(Finally);5) 6) what is the result?A. The program runs and prints nothing.B. The program runs and prints “Finally ”.C. The code compiles, but an exception is thrown at runtime.D. The code will not compile because the catch block is missing.Answer:btry.catch.finally 的问题。程序中如果遇到 return,

5、则 finally 块先被执行,然后再执行retrun,而 finally 块后面的语句将不被执行。如果遇到 System.exit(1) ,则 finally 块及其后的语句都不执行,整个程序退出,还执行什么呀。2.1) public class Test2) public static String output=;3) public static void foo(int i)4) try 5) if(i=1)6) throw new Exception();7) 8) output +=1;9) 10) catch(Exception e)11) output+=2;12) retur

6、n;13) 14) finally15) output+=3;16) 17) output+=4;18) 19) public static void main(String args)20) foo(0);21) foo(1);22)23) 24) what is the value of output at line 22?Asnwer:13423执行第一个 foo(0) 时,执行第 8 条语句, output=1 ,然后执行语句 15,output=13,然后是 17条,output=134,因为是 static 类型的变量, 所以任何对其值的修改都有效。 执行第二条 foo(1),先执

7、行语句 5,结果抛出异常,转到 catch 块,output=1342 ,finally 任何情况下都执行,所以 output=13423 ,然后 return 跳出方法体,所以 output=1342351)public class IfElse2)public static void main(String args)3)if(odd(5)4)System.out.println(odd);5)else6)System.out.println(even);7)8)public static int odd(int x)return x%2;9)what is output?Answer:

8、编译错误。if 中的判断条件的结果必须是 boolean 类型的。注意这里说的是结果 .61)class ExceptionTest2)public static void main(String args)3)try4)methodA();5)catch(IOException e)6)System.out.println(caught IOException);7)catch(Exception e)8)System.out.println(caught Exception);9) 10) 11) If methodA() throws a IOException, what is the

9、 result?Answer: caught IOException如果 methodA() 抛出 IOExecption ,被语句 6 捕获,输出 caught IOException ,然后呢?然后就结束了呗。71)int i=1,j=10;2)do3) if(i+-j) continue;4)while(ij)break tp;System.out.println(i=+i+,j=+j);what is the result?A.i=1,j=-1 B. i=0,j=-1 C.i=1,j=4 D.i=0,j=4E.compile error at line 4Answer: b break

10、 tp; 退出了最外层的 for 循环,程序接着从 System 开始执行。40Given:6. public class ForBar 7. public static void main(String args) 8. int i = 0, j = 5;9. tp: for (;) 10. i +;11. for(;)50. if(i -j) break tp;51. 52. system.out.printIn( “i = ” + i + “, j = “+ j);53. 54. What is the result?A.The program runs and prints “i=1,

11、 j=0 ”B.The program runs and prints “i=1, j=4 ”C.The program runs and prints “i=3, j=4 ”D.The program runs and prints “i=3, j=0 ”E.An error at line 4 causes compilation to failF.An error at line 7 causes compilation to failAnswer A在第五行 i 就变成了 1,第七行里 j 就一直往下减吧,然后退出最外层的 for41public class Foopublic sta

12、tic void main(String args)trySystem.exit(0);finallySystem.out.println(Finally);what is the result?A.print out nothingB.print out FinallyAnswer: ASystem.exit(0) 可以强行终止 JVM ,可见优先级最高啦, 有了它,finally 就玩完了。Systtem.exit()中不只可以放 0,可以是任何整数和字符。42which four types of objects can be thrown use throws?A.ErrorB.Eve

13、ntC.ObjectD.ExcptionE.ThrowableF.RuntimeExceptionAnswer: A,D,E ,Fthrows 可以抛出各种异常,但 Event 和 Object 算哪门子的异常呀?431)public class Test2) public static void main(String args)3) unsigned byte b=0;4) b-;5)6) 7) what is the value of b at line 5?A.-1 B.255 C.127 D.compile fail E.compile succeeded but run errorAnswer: d unsigened byte ? java 里怎么可以这样子定义变量哩?44public class ExceptionTestclass TestException exte

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!