OCP题库解析仅供参考.docx_第1页
OCP题库解析仅供参考.docx_第2页
OCP题库解析仅供参考.docx_第3页
OCP题库解析仅供参考.docx_第4页
OCP题库解析仅供参考.docx_第5页
已阅读5页,还剩142页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

QUESTION 11.Given a pre-generics implementation of a method:11. public static int sum(List list) 12. int sum = 0;13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) 14. int i = (Integer)iter.next().intValue();15. sum += i;16. 17. return sum;18. What three changes allow the class to be used with generics and avoid an unchecked warning? (Choosethree.)A. Remove line 14.B. Replace line 14 with int i = iter.next();.C. Replace line 13 with for (int i : intList) .D. Replace line 13 with for (Iterator iter : intList) .E. Replace the method declaration with sum(List intList).F. Replace the method declaration with sum(List intList).Answer: A,C,F考察把原始类型转换成泛型。则把List转换成List,由于类型参数不能为基本类型,所以E不正确,而第13行中Iterator转换成Iterator,Integer可以自动装箱和拆箱,所以第14行不需要。2.A programmer has an algorithm that requires a java.util.List that provides an efficientimplementation of add(0, object), but does NOT need to support quick random access. What supportsthese requirements.?A. java.util.QueueB. java.util.ArrayListC. java.util.LinearListD. java.util.LinkedListAnswer: D实现了Java.util.List接口,并且需要高效率在首部进行插入操作,选项中仅有LinkedList符合3.Given:11. / insert code here12. private N min, max;13. public N getMin() return min; 14. public N getMax() return max; 15. public void add(N added) 16. if (min = null | added.doubleValue() max.doubleValue()19. max = added;20. 21.Which two, inserted at line 11, will allow the code to compile? (Choose two.)A. public class MinMaxThe safer , easier way to help you pass any IT exams.B. public class MinMaxC. public class MinMaxD. public class MinMaxE. public class MinMaxF. public class MinMaxAnswer: D,F必须有N这个泛型,所以从CDF中选择,而N需要实现了doubleValue方法,Object肯定不行,而Number和Integer都实现了这个方法。4.Given:12. import java.util.*;13. public class Explorer2 14. public static void main(String args) 15. TreeSet s = new TreeSet();16. TreeSet subs = new TreeSet();17. for(int i = 606; i 613; i+)18. if(i%2 = 0) s.add(i);19. subs = (TreeSet)s.subSet(608, true, 611, true);20. s.add(629);21. System.out.println(s + + subs);22. 23.What is the result?A. Compilation fails.B. An exception is thrown at runtime.C. 608, 610, 612, 629 608, 610D. 608, 610, 612, 629 608, 610, 629E. 606, 608, 610, 612, 629 608, 610F. 606, 608, 610, 612, 629 608, 610, 629Answer: E考察TreeSet以及它的subSet方法,两个true表示包括608和611。TreeSet是一个有序集,所以其中的元素必须是有序的。publicSortedSetsubSet(EfromElement, EtoElement)Returns a view of the portion of this set whose elements range fromfromElement, inclusive, totoElement, exclusive. (IffromElementandtoElementare equal, the returned set is empty.) The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.subSet返回的是一个view,而不是copy,所以对subSet的改变会反映到原set里面,反之,在原set中的变化也会反映到subSet里面。看下面的例子,首先移除一个15,发现原始的treeSet和subset里面的15都没了,而treeSet加上15之后,原始的treeSet和subset里面的15又都出现了。javaview plaincopy1. packagecn.xujin;2. importjava.util.TreeSet;3. 4. publicclassExplorer15. publicstaticvoidmain(Stringargs)6. TreeSettreeSet=newTreeSet();7. TreeSetsubSet=newTreeSet();8. for(inti=10;i=20;i+)9. treeSet.add(i);10. subSet=(TreeSet)treeSet.subSet(13,true,16,true);11. System.out.println(subSet);/13,14,15,1612. subSet.remove(15);13. System.out.println(subSet);/13,14,1614. System.out.println(treeSet);/10,11,12,13,14,16,17,18,19,2015. treeSet.add(15);16. System.out.println(subSet);/13,14,15,1617. System.out.println(treeSet);/10,11,12,13,14,15,16,17,18,19,2018. 19. 5.Given:1. public class Score implements Comparable 2. private int wins, losses;3. public Score(int w, int l) wins = w; losses = l; 4. public int getWins() return wins; 5. public int getLosses() return losses; 6. public String toString() 7. return ;8. 9. / insert code here10.Which method will complete this class?A. public int compareTo(Object o)/*more code here*/B. public int compareTo(Score other)/*more code here*/C. public int compare(Score s1,Score s2)/*more code here*/D. public int compare(Object o1,Object o2)/*more code here*/Answer: B实现Comparable接口必须实现comparTo方法!注意与Compartor接口区分开来,Compartor接口要求实现Compare方法。6.Given11. public class Person 12. private name;13. public Person(String name) 14. = name;15. 16. public int hashCode() 17. return 420;18. 19.Which statement is true?A. The time to find the value from HashMap with a Person key depends on thesize of the map.B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.C. Inserting a second Person object into a HashSet will cause the first Person object to beremoved as a duplicate.D. The time to determine whether a Person object is contained in a HashSet is constant and does NOTdepend on the size of the map.Answer: AB选项:删除HashMap中一个Person对象对应的键将会删除这个散列映射表中Person类的全部条目。错误,HashMap中Person对象的键值不是由Person对象决定的,而是程序员给定的键,例如staff.add(123-345, bob),就是把键为123-456的bob对象添加到名为staff的HashMap中,因而HashMap允许添加相同的对象。所以说,删除一个键对应的Person对象并不会删除所有的条目,他们的key都不同嘛。C选项:向HashSet中插入另外一个Person对象将会引起第二个对象覆盖第一个对象。错误,虽然Person对象的hashCode方法返回的值都是420,这仅仅表明两个Person对象在一个entry链表中,接下来要调用equals方法,由于Person类没有equals方法,所以调用Object的equals方法返回对象的存储地址,很明显两个Person对象的存储地址是不同的。综上,HashSet中可以添加不同的Person对象,只要equals方法返回值为false就好。D选项:判断一个HashSet中是否存在一个Person对象的次数是常数次,和map的大小无关。错误,由于Person对象的hashCode返回的值都是420,所以HashSet中的Person对象都在一个bucket中,组成了一条entry链表,查询速度与entry链表的大小息息相关。A:选项:由key来查找value的次数与map的大小有关。正确,map越大,即bucket的个数越多,entry链的长度相应的来说就越小(hashcode和桶个数取余后的数一样的几率就越小)。7.Given:5. import java.util.*;6. public class SortOf 7. public static void main(String args) 8. ArrayList a = new ArrayList();9. a.add(1); a.add(5); a.add(3);11. Collections.sort(a);12. a.add(2);13. Collections.reverse(a);14. System.out.println(a);15. 16.What is the result?A. 1, 2, 3, 5B. 2, 1, 3, 5C. 2, 5, 3, 1D. 5, 3, 2, 1E. 1, 3, 5, 2F. Compilation fails.G. An exception is thrown at runtime.Answer: CCollections.sort(a);实现了排序,而reverse方法是反转集合中的所有元素。8.Given11. public interface Status 12. /* insert code here */ int MY_VALUE = 10;13. Which three are valid on line12?(Choose three.)A. finalB. staticC. nativeD. publicE. privateF. abstractG. protectedAnswer: A,B,D接口中,变量默认是public static final,而方法默认是public,不能改变。9.Given:5. class Atom 6. Atom() System.out.print(atom ); 7. 8. class Rock extends Atom 9. Rock(String type) System.out.print(type); 10. 11. public class Mountain extends Rock 12. Mountain() 13. super(granite );14. new Rock(granite );15. 16. public static void main(String a) new Mountain(); 17. What is the result?A. Compilation fails.B. atom graniteC. granite graniteD. atom granite graniteE. An exception is thrown at runtime.F. atom granite atom graniteAnswer: F当调用子类的构造函数的时候,如果没有显式调用父类的构造函数,那么父类的默认构造函数将被调用,如果父类没有默认的构造函数,编译器就会警告出错。10.Click the Exhibit button. Which three statements are true? (Choose three.)A. Compilation fails.B. The code compiles and the output is 2.C. If lines 16, 17 and 18 were removed, compilation would fail.D. If lines 24, 25 and 26 were removed, compilation would fail.E. If lines 16, 17 and 18 were removed, the code would compile and the output would be 2.F. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.Answer: B,E,F注意这里有两个内部类,一个是名为A的内部类A,另一个是名为A的局部内部类A,在testFoo方法内部,局部内部类将屏蔽内部类A。 所以当调用Beta类型对象的testFoo方法时,将输出第28行的内容,new A()调用的是局部内部类A的构造函数,返回一个局部内部类A给fubar()函数,最终输出2。如果删除16,17,18行,由于局部内部类A在方法testFoo()内部屏蔽了外部的内部类A可以看出不会对局部内部产生任何影响。如果删除24,25,26行,那么本来被这个局部内部类屏蔽的不可见的局部类A 变得可见,输出自然也是1了。11.Given:10. class Line 11. public class Point public int x,y;12. public Point getPoint() return new Point(); 13. 14. class Triangle 15. public Triangle() 16. / insert code here17. 18.Which code, inserted at line 16, correctly retrieves a local instance of a Point object?A. Point p =Line.getPoint();B. Line.Point p = Line.getPoint();C. Point p = (new Line().getPoint();D. Line.Point p = (new Line().getPoint();Answer: D在内部类的外围类外使用内部类的时候,就像上面那个Line.Point p = (new Line().getPoint()即OuterClassName.InnerClassName p = OuterClassObject.method();/其中method方法返回OuterClassName.InnerClassName类型的对象12.Given:11. class Alpha 12. public void foo() System.out.print(Afoo ); 13. 14. public class Beta extends Alpha 15. public void foo() System.out.print(Bfoo ); 16. public static void main(String args) 17. Alpha a = new Beta();18. Beta b = (Beta)a;19. a.foo();20. b.foo();21. 22.What is the result?A. Afoo AfooB. Afoo BfooC. Bfoo AfooD. Bfoo BfooE. Compilation fails.F. An exception is thrown at runtime.Answer: D考察多态性,编译器认为a是Alpha类型,b是Beta类型,但是虚拟机知道a和b的真实类型是Beta,所以调用Beta类的函数。13.Click the Exhibit button.Which statement is true about the classes and interfaces in the exhibit?A. Compilation will succeed for all classes and interfaces.B. Compilation of class C will fail because of an error in line 2.C. Compilation of class C will fail because of an error in line 6.D. Compilation of class AImpl will fail because of an error in line 2.Answer: C考察多态性的动态绑定,如果方法的签名相同,则返回类型应该与超类的相同或是超类中返回类型的子类型。上题中C类中有方法Object execute() 以及 String execute(),Object不是Strng类型的子类型,所以错误。14.Which two code fragments correctly create and initialize a static array of int elements? (Choose two.)A. static final int a = 100,200 ;B. static final int a;static a=new int2; a0=100; a1=200;C. static final int a = new int2 100,200 ;D. static final int a;static void init() a = new int3; a0=100; a1=200;Answer: A,B考察static修饰符。A选项是经典定义。B是一个静态初始化块。类的初始化是按照这样的顺序进行的:首先定义域field,然后就会执行静态初始化块,然后再执行构造函数。C错在既然你已经用 100,200 初始化了数组,就不能再定义维数了。D中init()方法在初始化的时候根本就不会执行,除非你在某个函数中显式调用。15.Given:10. interface Foo int bar(); 11. public class Sprite 12. public int fubar( Foo foo ) return foo.bar(); 13. public void testFoo() 14. fubar(15. / insert code here16. );17. 18.Which code, inserted at line 15, allows the class Sprite to compile?A. Foo public int bar() return 1;B. new Foo public int bar() return 1;C. new Foo() public int bar() return 1;D. new class Foo public int bar() return 1; Answer: C匿名内部类。16.Given:1. class Alligator 2. public static void main(String args) 3. int x = 1,2, 3,4,5, 6,7,8,9;4. int y = x;5. System.out.println(y21);6. 7.What is the result?A. 2B. 3C. 4D. 6E. 7F. Compilation fails.Answer: E很简单的数组题。int x 与int x以及int x都是一样的。17.Given:22. StringBuilder sb1 = new StringBuilder(123);23. String s1 = 123;24. / insert code here25. System.out.println(sb1 + + s1);Which code fragment, inserted at line 24, outputs 123abc 123abc?A. sb1.append(abc); s1.append(abc);B. sb1.append(abc); s1.concat(abc);C. sb1.concat(abc); s1.append(abc);D. sb1.concat(abc); s1.concat(abc);E. sb1.append(abc); s1 = s1.concat(abc);F. sb1.concat(abc); s1 = s1.concat(abc);G. sb1.append(abc); s1 = s1 + s1.concat(abc);H. sb1.concat(abc); s1 = s1 + s1.concat(abc);Answer: E考察String类和StringBuilder类。String类有一个concat函数,String str = s.concat(one);用来把字符串添加到s的尾部,并把结果返回给str,注意String类型的变量是不变的,s并没有变化。而StringBuilder的append方法,sb.append(one);直接把字符串添加到sb引用的StringBuilder对象上。18.Given that the current directory is empty, and that the user has read and write permissions, and thefollowing:11. import java.io.*;12. public class DOS 13. public static void main(String args) 14. File dir = new File(dir);15. dir.mkdir();16. File f1 = new File(dir, f1.txt);17. try 18. f1.createNewFile();19. catch (IOException e) ; 20. File newDir = new File(newDir);21. dir.renameTo(newDir);22. 23.Which statement is true?A. Compilation fails.B. The file system has a new empty directory named dir.C. The file system has a new empty directory named newDir.D. The file system has a directory named dir, containing a file f1.txt.E. The file system has a directory named newDir, containing a file f1.txt.Answer: E考察java.io包。19.Given:11. class Converter 12. public static void main(String args) 13. Integer i = args0;14. int j = 12;15. System.out.println(It is + (j=i) + that j=i.);16. 17. What is the result when the programmer attempts to compile the code and run it with thecommand java Converter 12?A. It is true that j=i.B. It is false that j=i.C. An exception is thrown at runtime.D. Compilation fails because of an error in line 13.Answer: Dargs是一个String类型的数组,第13行应该是int i = Integer.ParseInt(args0);20.Given:11. String test = Test A. Test B. Test C.;12. / insert code here13. String result = test.split(regex);Which regular expression, inserted at line 12, correctly splits test into Test A, Test B, and TestC?A. String regex = ;B. String regex = ;C. String regex = .*;D. String regex = s;E. String regex = .s*;F. String regex = w . +;Answer: E正则表达式,String regex = .s*;表示根据点(dot)和空格(space)来分割字符串。21.Given:5. import java.util.Date;6. import java.text.DateFormat;21. DateFormat df;22. Date date = new Date();23. / insert code here24. String s = df.format(date);Which code fragment, inserted at line 23, allows the code to compile?A. df = new DateFormat();B. df = Date.getFormat();C. df = date.getFormat();D. df = DateFormat.getFormat();E. df = DateFormat.getInstance();Answer: EDateFormat.类的静态方法getInstance()返回一个DateFormat类型的对象,public static finalDateFormatgetInstance()Get a default date/time formatter that uses the SHORT style for both the date and the time.22.Given a class Repetition:1. package utils;2.3. public class Repetition 4. public static String twice(String s) return s + s; 5. and given another class Demo: 1. / insert code here2.3. public class Demo 4. public static void main(String args) 5. System.out.println(twice(pizza);6. 7. Which code should be inserted at line 1 of Demo.java to compile and run Demo to printpizzapizza?A. import utils.*;B. static import utils.*;C. import utils.Repetition.*;D. static import utils.Repetition.*;E. import utils.Repetition.twice();F. import static utils.Repetition.twice;G. static import utils.Repetition.twice;Answer: F静态导入,可以导入类,类中的静态方法,静态域。格式import static .import static utils.Repetition.twice;/导入Repetition类中的twice静态方法。import static utils.Repetition.*;/导入Repetition类中的所有静态方法和静态域。23.A UNIX user named Bob wants to replace his chess program with a new one, but he is not sure wherethe old one is installed. Bob is currently able to run a Java chess program starting from his home directory/home/bob using the command: java -classpath /test:/home/bob/downloads/*.jargames.Chess BobsCLASSPATH is set (at login time) to:/usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/*.jarWhat is a possible location for theChess.class file?A. /test/Chess.classB. /home/bob/Chess.classC. /test/games/Chess.classD. /usr/lib/games/Chess.classE. /home/bob/games/Chess.classF. inside jarfile /opt/java/lib/Games.jar (with a correct manifest)G. inside jarfile /home/bob/downloads/Games.jar (with a correct manifest)Answer: C-classpath 命令会把路径动态设置成/test和/home/bob/downloads/*.jar,只能在这两个路径里搜索Chess.class,由于在运行的时候classpath后面是games.Chess,所以肯定是games包里面的Chess文件。所以C可能正确。而/home/bob/downloads/*.jar这种写法无效,因为如果有两个jar包都存在games.Chess,系统不知道是哪一个。24.Given:3. interface Animal void makeNoise(); 4. class Horse implements Animal 5. Long weight = 1200L;6. public void makeNoise() System.out.println(whinny); 7. 8. public class Icelandic extends Horse 9. public void makeNoise() System.out.println(vinny); 10. public static void main(String args) 11. Icelandic i1 = new Icelandic();12. Icelandic i2 = new Icelandic();13. Icelandic i3 = new Icelandic();14. i3 = i1; i1 = i2; i2 = null; i3 = i1;15. 16.When line 15 is reached, how many objects are eligible for the garbage collector?A. 0B. 1C. 2D. 3E. 4F. 6Answer: E垃圾回收GC。我们假设13行结束的时候,i1引用的对象叫A,i2引用的对象叫B,i3引用的对象叫C。现在考虑在15行的时候,i1和i3都引用B,而i2引用为null,所以目前有A和C两个对象没有变量引用,所以这两个对象是可以被回收的。注意,A,B对象中含有Long类型的对象,所以,一共有4个变量可以被回收。25.Click the Exhibit button.Given the fully-qualified class names: com.foo.bar.Dogcom.foo.bar.blatz.Book com.bar.Car com.bar.blatz.Sun Which graph represents the correct directorystructure for a JAR file from which those classes can be used by the compiler and JVM?A. Jar AB. Jar BC. Jar CD. Jar DE. Jar EAnswer: A26.Given classes defined in two different files:1. package util;2. public class BitUtils 3. private static void process(byte b) 4. 1. package app;2. public class

温馨提示

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

最新文档

评论

0/150

提交评论