欢迎来到装配图网! | 帮助中心 装配图网zhuangpeitu.com!
装配图网
ImageVerifierCode 换一换
首页 装配图网 > 资源分类 > DOC文档下载
 

ocjp考试题库

  • 资源ID:75248044       资源大小:602KB        全文页数:66页
  • 资源格式: DOC        下载积分:35积分
快捷下载 游客一键下载
会员登录下载
微信登录下载
三方登录下载: 微信开放平台登录 支付宝登录   QQ登录   微博登录  
二维码
微信扫一扫登录
下载资源需要35积分
邮箱/手机:
温馨提示:
用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
支付方式: 支付宝    微信支付   
验证码:   换一换

 
账号:
密码:
验证码:   换一换
  忘记密码?
    
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

ocjp考试题库

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) class 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注意这道题主要考的是方法的 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 System.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,则 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) return;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),先执行语句 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: 编译错误。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 result?Answer: caught IOException如果 methodA() 抛出 IOExecption ,被语句 6 捕获,输出 caught IOException ,然后呢?然后就结束了呗。71)int i=1,j=10;2)do3) if(i+>-j) continue;4)while(i<5);After Execution, what are the value for i and j?A. i=6 j=5B. i=5 j=5C. i=6 j=4D. i=5 j=6E. i=6 j=6Answer: d程序一直循环,直到 i=4 ,j=6 时,执行完语句 3 后,i 会+,这时 i 就等于了 5,continue后就不能再循环了,所以选 D。81)public class X2) 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 4B. After line 5C.After line 6D.After line 7E.After line 9(that is,as the method returns)Answer: d当执行第 6 行后,仍然有对象指向 o,所以 o 不能满足条件,当第 7 条语句被执行后,就再也没有对象指向 o 了,所以选 D。91) interface Foo2) int k=0;3) 4) public class Test implements Foo5) public static void main(String args)6) int i;7) Test test =new Test();8) 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编译通过,通过测试的10what is reserved(保留) words in java?A. runB. defaultC. implementD. importAnswer: b ,D111)public class Test2) public static void main(String args)3) String foo=args1;4) Sring bar=args2;5) String baz=args3;6) 7) java Test Red Green Bluewhat is the value of baz?A. baz has value of ""B. baz has value of nullC. baz has value of "Red"D. baz has value of "Blue"E. baz has value of "Green"F. the code does not compileG. the program throw an exceptionAnswer: G当 执 行 java Test Red Green Blue 时 , 数 组 args 只 有 012 , 运 行 时ArrayIndexOutOfBoundsException 这个异常会被抛出,数组越界。12int index=1;int foo=new int3;int bar=fooindex;int baz=bar+index;what is the result?A. baz has a value of 0B. baz has value of 1C. baz has value of 2D. an exception is thrownE. the code will not compileAnswer: b数组初始化后默认值是 0,所以 baz=0+1=113which three are valid declaraction (行为) of a float?A. float foo= -1;B. float foo=1.0;C. float foo=42e1;D. float foo=2.02f;E. float foo=3.03d;F. float foo=0x0123;Answer: A ,D,F其它的系统都会认为是 double 类型,所以出错。说一下 A 和 C 的区别吧, -1 系统会认为是一个 int 类型,把 int 类型再赋给 float 类型的 foo,当然没错了,可 C 就不同啦, 42e1 是 int类型吗?141)public class Foo2) public static void main(String args)3) String s;4) System.out.println("s="+s);5) 6) what is the result?A. The code compiles and “s=” is printed.B. The code compiles and “s=null ” is printed.C. The code does not compile because string s is not initialized (初始化) .D. The code does not compile because string s cannot be referenced(引用) .E. The code compiles, but a NullPointerException is thrown when toString is called.Answer:C只有实例变量系统才给予自动赋默认值的这种待遇151) public class Test2) public static void main(String args)3) int i=oxFFFFFFF1;4) int j=i;5)6) 7) which is decimal value of j at line 5?A. 0B.1C.14D.-15E. compile error at line 3F. compile error at line 4Answer: C算一算就知道了。16float f=4.2F;Float g=new Float(4.2F);Double d=new Double(4.2);Which are true?A. f=gB. g=gC. d=fD. d.equals(f)E d.equals(g)F. g.equals(4.2);Answer: B=两边类型不同不相等。所以 A 和 C 不等。equals 只能用于引用类型,不能用于基本类型,所以 D 不对,而且两边类型不兼容的话 ,即使对象的内容一样 ,也不相等,所以 E 和 F 不对。171)public class Test2) public static void add3(Integer i)3) int val=i.intValue();4) val+=3;5) i=new Integer(val);6) 7) public static void main(String args)8) Integer i=new Integer(0);9) add3(i);10) System.out.println(i.intValue();11) 12) what is the result?A. compile failB.print out "0"C.print out "3"D.compile succeded but exception at line 3Answer: b在第五行里,程序又操作了 New,重新分配了内存空间。所以此 i 非彼 i 啦。181)public class Test2) public static void main(String args)3) System.out.println(63); /,? 为 XOR4) 5) what is output?Answer: 5 算呗。191) public class Test2) public static void stringReplace(String text)3) text=text.replace('j','l');4) 5) public static void bufferReplace(StringBuffer text)6) text=text.append("c");7) 8) public static void main(String args)9) String textString=new String("java");10) StringBuffer textBuffer=new StringBuffer("java");11) StringReplace(textString);12) bufferReplace(textBuffer);13) System.out.println(textString+textBuffer);14) 15) what is the output?Answer: javajavactextString 是 String 类型的,具有不变性,语句 3 其实是创建了一个新的字符串,而不是修改原来的 textString ,而对于 StringBuffer 类型的对象,则所有修改都是实在的。所以在语句6 中 textBuffer 变成了 javac,所以输出为 javajavac。201)public class ConstOver2) public ConstOver(int x, int y, int z)3) which two overload the ConstOver constructor?A.ConstOver()B.protected int ConstOver()C.private ConstOver(int z, int y, byte x)D.public void ConstOver(byte x, byte y, byte z)E.public Object ConstOver(int x, int y, int z)Answer: a,c主要的问题是 overload,参数列表必须不同, 方法名相同, 访问控制无限制。 也无异常限制。这道题因为是构造器,所以 B,D 和 E 不对,因为构造器不能有返回类型。211)public class MethodOver2) public void setVar(int a, int b, float c)3) which overload the setVar?A.private void setVar(int a, float c, int b)B.protected void setVar(int a, int b, float c)C.public int setVar(int a, float c, int b)return a;D.public int setVar(int a, float c)return a;Answer: a,c,doverload 无访问控制限制,所以 A 对,顺序也属于参数列表,顺序不同也一样是 overload,所以 C 正确, D 当然正确了,参数列表明显不同。221)class EnclosingOne2)public class InsideOne3) 4)public class InnerTest5) public static void main(String args)6) EnclosingOne eo=new EnclosingOne();7) /insert code here8) 9)A.InsideOne ei=eo.new InsideOne();B.eo.InsideOne ei=eo.new InsideOne();C.InsideOne ei=EnclosingOne.new InsideOne();D.InsideOne ei=eo.new InsideOne();E.EnclosingOne.InsideOne ei=eo.new InsideOne();Answer: e这里边的一些形式是固定的。(1)静态方法访问非静态内类:方法为:Outer myouter=new Outer(); 这里的 myouter 是创建的外部类的对象。Outer.Inner myinner=myouter.new Inner(); myinner 是内类的对象。然后再 myinner.showName(); showName()是外类中的非静态方法。(2)非静态方法访问非静态内类直接创建该内部类的对象: new Inner().showName();(3)静态方法访问静态内类:也是直接创建该内部类的对象,即 Inner myinner = new Inner() ,或者 Outer.Inner myinner = new Outer.Inner() 也行得通哦。23What is "is a" relation?A.public interface Colorpublic class Shapeprivate Color color;B.interface Componentclass Container implements Componentprivate Component children;C.public class Speciespublic class Animalprivate Species species;Answer: b"is a " 意思为是什么:定义了一个超类和一个子类之间的一种直接关系 :子类是超类的一种。也即是继承的关系241)package foo;2)3)public class Outer4)public static class Inner5)6)which is true to instantiated( 事例 ) Inner class inside Outer?A. new Outer.Inner()B. new Inner()Answer: a,b25class BaseClassprivate float x=1.0f;private float getVar()return x;class SubClass extends BaseClassprivate float x=2.0f;/insert codewhat are true to override getVar()?A.float getVar()B.public float getVar()C.public double getVar()D.protected float getVar()E.public float getVar(float f)Answer: a,b,d又是 override 的问题, 参数列表和返回值以及方法名 (好像是费话 )必须精确匹配 ,访问控制要更公有化 ,如果抛出异常 ,那么必须异常本身或其子集或什么都不抛 .26public class SychTestprivate int x;private int y;public void setX(int i) x=i;public void setY(int i)y=i;public Synchronized void setXY(int i)setX(i);setY(i);public Synchronized boolean check()return x!=y;Under which conditions will check() return true when called from a different class?A.check() can never return true.B.check() can return true when setXY is callled by multiple threads.C.check() can return true when multiple threads call setX and setY separately.D.check() can only return true if SychTest is changed allow x and y to be set separately.Answer: c27Given:3. public class SyncTest (4. private int x;5. private int y;6. private synchronized void setX (int i) (x=1;)7. private synchronized void setY (int i) (y=1;)8. public void setXY(int 1)(set X(i); setY(i);)9. public synchronized Boolean check() (return x !=y;)10. )Under which conditions will check () return true when called from a different class?A. Check() can never return trueB. Check() can return true when setXY is called by multiple threadsC. Check() can return true when multiple threads call setX and setY separately.D. Check() can only return true if SyncTest is changed to allow x and y to be set separately.Answer:B28Given:11. public class SyncTest 12. private int x;13. private int y;14. public synchronized void setX (int i) (x=1;)15. public synchronized void setY (int i) (y=1;)16. public synchronized void setXY(int 1)(set X(i); setY(i);)17. public synchronized Boolean check() (return x !=y;)18. )Under which conditions will check () return true when called from a different class?A. Check() can never return true.B. Check() can return true when setXY is called by multiple threads.C. Check() can return true when multiple threads call setX and setY separately.D. Check() can only return true if SyncTest is changed to allow x and y to be set separately.Answer: A哪一个不加锁,就从哪一个入手,但这道题全都加锁了,所以先 A。291)public class X implements Runnable2)private int x;3)private int y;4)public static void main(String args)5) X that =new X();6) (new Thread(that).start();7) (new Thread(that).start();9) public synchronized void run()10) for(;)11) x+;12) y+;13) System.out.println("x="+x+",y="+y);14) 15) 16) what is the result?A.An error at line 11 causes compilation to fail.B.Errors at lines 6 and 7cause compilation to fail.C.The program prints pairs of values for x and y that might not always be the same on the sameline (for example, “x=2, y=1 ”)D.The program prints pairs of values for x and y that are always the same on the same line (forexample, “x=1, y=1 ”. In addition, each value appears twice (for example, “x=1, y=1 ” foll“x=1, y=1 ”)E.The program prints pairs of values for x and y that are always the same on the same line (forexample, “x=1, y=1 ”. In addition, each value appears twice (for example, “x=1, y=1 ” foll“x=2, y=2 ”)Answer: E这道题有问题, 当两个线程同名时, 输出 x=1,y=1 ,但只一次, 而当两个线程不同名时,输出就是两次。理论上加锁时,线程 1 会执行直到结束,然后线程 2 才会开始执行。30Click the exhibit button:19. public class X implements Runnable(20. private int x;21. private int y;22.23. public static void main(Stringargs)24. X that = new X();25. (new Thread(that).start();26. (new Thread(that).start();27. )28.29. public void run() (30. for (;) (31. x+;32. y+;33. System.out.printIn( “x=” + x+y); “, y = ”34. )35. )36. )What is the result?A. Errors at lines 7 and 8 cause compilation to fail.B. The program prints pairs of values for x and y that might not always be the same on thesame line (for example, “x=2, y=1 ”).C. The program prints pairs of values for x and y that are always the same on the same line(for example, “x=1,y=1”.In addition, each value appears twice (for example, “x=1,y=1”followed by “x=1, y=1 ”).D. The program prints pairs of values for x and y that are always the same on the same line(for example, “x=1, y=1 ”.) In addition, each value appears only for once (for example, “followed by “x=2, y=2 ”).Answer D这道题也有问题,当两个线程同名的情况下,只输出一次,但当两个线程不同名的情况下,可以清楚的看到两个线程是交替执行的, x=1,y=1 后现可以是任何东西,也可能是另一个线程的 x=1,y=1 。31class A implements Runnableint i;public void run()tryThread.sleep(5000);i=10;catch(InterruptException e)public static void main(String args)tryA a=new A();Thread t=new Thread(a);t.start();17)int j=a.i;19)catch(Exception e)what be added at line line 17,ensure j=10 at line 19?A. a.wait();B. t.wait();C. t.join();D.t.yield();E.t.notify();F. a.notify();G.t.interrupt();Answer: c32 Given an ActionEvent, which method allows you to identify the affected component?A. public class getClass()B. public Object getSource()C. public Component getSource()D. public Component getTarget()E. public Component getComponent()F. public Component getTargetComponent()Answer: c33 import java.awt.*;public class X extends Framepublic static void main(String args)X x=new X();x.pack();x.setVisible(true);public X()setLayout(new GridLayout(2,2);Panel p1=new Panel();add(p1);Button b1=new Button("One");p1.add(b1);Panel p2=new Panel();add(p2);Button b2=new Button("Two");p2.add(b2);Button b3=new Button("Three");p2.add(b3);Button b4=new Button("Four");add(b4);when the frame is resized,A.all change heightB.all change widthC.Button "One" change heightD.Button "Two" change heightE.Button "Three" change widthF.Button "Four" change height and widthAnswer: f341)public class X2) public static void main(String args)3) String foo="ABCDE"4) foo.substring(3);5) foo.concat("XYZ");6) 7) what is the value of foo at line 6?Answer: ABCDE问题的关键在于 String 的不变性,虽然又是 substring 又是 concat 的,但都只是创建了一个新的字符串,原本的 foo 一直都没有被改变,也不可能被改变。35Which method is an appropriate way to determine the cosine of 42 degrees?A. double d = Math.cos(42);B. double d = Math.cosine(42);C. double d = Math.cos(Math.toRadians(42);D. double d = Math.cos(Math.toDegrees(42);E. double d = Math.cosine(Math.toRadians(42);Answer: ctoRadians 是把一个角度转换成一个弧度, cos 方法的参数必须是以弧度表示的。36public class Testpublic static void main(String args)StringBuffer a=new StringBuffer("A");StringBuffer b=new StringBuffer("B");operate(a,b);System.out.println(a+","+b);public static void operate(StringBuffer x, StringBuffer y)x.append(y);y=x;what is the output?Answer: AB,B 有些不理解。我只能认为 y=x 这名语句没有改变 b。37 Given:37. public class Foo 38. public static void main (String args) 39. StringBuffer a = new StringBuffer ( “A”);40. StringBuffer b = new StringBuffer ( “B”);41. operate (a,b);42. system.out.printIna + “, ” +b;43. )44. static void operate (StringBuffer x, StringBuffer y) 45. y.append (x) ;46. y = x;47. )48. What is the result?A.The code compiles and prints “A,B ”.B.The code compiles and prints “A, BA ” .C.The code compiles and prints “AB, B ” .D.The code compiles and prints “AB, AB ” .E.The code compiles and prints “BA, BA ” .F.The code does not compile because “+” cannot be overloaded for stringBuffer.Answer B49.1) public class Test2) public static void main(String args)3) class Foo4) public int i=3;5) 6) Object o=(Object)new Foo();7) Foo foo=(Foo)O;8) System.out.println(foo.i);9) 10) what is result?A.compile error at line 6B.compile error at line 7C.print out 3Answer: C简单的说,要访问变量的时候,看等号左边,访问方法的时候看等号右边,但对于 static 类型的方法除外。39public class FooBarpublic static void main(String args)int i=0,j=5;4) tp: for(;i+)for(;-j)if(i>j)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 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, 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 static void main(String args)trySystem.exit(0);finallySystem.out.println("Finally");what is the result?A.print out nothingB.print out "Finally"Answer: ASystem.exit(0) 可以强行终止 JVM ,可见优先级最高啦, 有了它,finally 就玩完了。Systtem.exit()中不只可以放 0,可以是任何整数和字符。42which four types of objects can be thrown use "throws"?A.ErrorB.EventC.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

注意事项

本文(ocjp考试题库)为本站会员(h***)主动上传,装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知装配图网(点击联系客服),我们立即给予删除!

温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

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

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


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