2023年ocjp考试题库.doc

上传人:ni****g 文档编号:559640314 上传时间:2023-08-21 格式:DOC 页数:112 大小:207.54KB
返回 下载 相关 举报
2023年ocjp考试题库.doc_第1页
第1页 / 共112页
2023年ocjp考试题库.doc_第2页
第2页 / 共112页
2023年ocjp考试题库.doc_第3页
第3页 / 共112页
2023年ocjp考试题库.doc_第4页
第4页 / 共112页
2023年ocjp考试题库.doc_第5页
第5页 / 共112页
点击查看更多>>
资源描述

《2023年ocjp考试题库.doc》由会员分享,可在线阅读,更多相关《2023年ocjp考试题库.doc(112页珍藏版)》请在金锄头文库上搜索。

1、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. double Answer F注释:short类型的x,除以double类型的y,再乘int的2,所以结果是double类型的。注意第三行的强制转换,只是转换了x。2. 1) class Super 2) pu

2、blic float getNum()return 3.0f; 3) 4) 5) public class Sub extends Super 6) 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注意这道题重要考的是方法的o

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

4、y); 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,则finally块先被执行,然后再执行ret

5、run,而finally块后面的语句将不被执行。假如碰到System.exit(1),则finally块及其后的语句都不执行,整个程序退出,还执行什么呀。 4. 1) public class Test 2) 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) return; 13) 14) finally 1

6、5) 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),先执行语句5,结果抛出异常,转到catch块,out

7、put=1342,finally任何情况下都执行,所以output=13423,然后return跳出方法体,所以output=134235 1)public class IfElse 2)public static void main(String args) 3)if(odd(5) 4)System.out.println(odd); 5)else 6)System.out.println(even); 7) 8)public static int odd(int x)return x%2; 9) what is output? Answer: 编译错误。if中的判断条件的结果必须是bool

8、ean类型的。注意这里说的是结果. 6 1)class ExceptionTest 2)public static void main(String args) 3)try 4)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 result? Answer:

9、caught IOException假如methodA()抛出IOExecption,被语句6捕获,输出caught IOException,然后呢?然后就结束了呗。71)int i=1,j=10; 2)do 3) if(i+-j) continue; 4)while(i5); After Execution, what are the value for i and j? A. i=6 j=5 B. i=5 j=5 C. i=6 j=4 D. i=5 j=6 E. i=6 j=6 Answer: d程序一直循环,直到i=4,j=6时,执行完语句3后,i会+,这时i就等于了5,continue

10、后就不能再循环了,所以选D。81)public class X 2) public Object m() 3) Object o=new Float(3.14F); 4) Object oa=new Object1; 5) oa0=o; 6) o=null; 7) oa0=null; 8) System.out.println(oa0); 9) 10) which line is the earliest point the object a refered is definitely elibile to be garbage collectioned? A.After line 4 B.

11、After line 5 C.After line 6 D.After line 7 E.After line 9(that is,as the method returns) Answer: d当执行第6行后,仍然有对象指向o,所以o不能满足条件,当第7条语句被执行后,就再也没有对象指向o了,所以选D。9 1) interface Foo 2) int k=0; 3) 4) public class Test implements Foo 5) public static void main(String args) 6) int i; 7) Test test =new Test(); 8

12、) i=test.k; 9) i=Test.k; 10) i=Foo.k; 11) 12) What is the result?A. Compilation succeeds.B. An error at line 2 causes compilation to fail.C. An error at line 9 causes compilation to fail.D. An error at line 10 causes compilation to fail.E. An error at line 11 causes compilation to fail. Answer: A编译通过,通过测试的10 what is reserved(保存) words in java? A. run B. default C. implement D. import Answer: b,D111)public class Test 2) public static void ma

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 项目/工程管理

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号