SCJP认证考试题库2.pdf
《SCJP认证考试题库2.pdf》由会员分享,可在线阅读,更多相关《SCJP认证考试题库2.pdf(66页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、S CJP认证考试题库2QUESTION 61GIVEN THE EXHIBIT:23 int z=5;2425 pubhc voidd Certkiller 1 (int x)26 assert(x 0),27 switch(x)(2:8 case 2:x=3,29 default;accert f a k e,)3 0.)31 private void Certkiller2(int y)assert(y o)3233 private void CertkillerS()(assert i Certkiller 4(),)3435 private Boolean Certkiller 4(
2、)z=6,return fa ls e,)Which statement is true?A.All of the assert statements are usedappropriately.B.Only the assert statement on line 31 is usedappropriatelyC.The assert statements on lines 29 and 31 areused appropriatelyD.The assert statements on lines 26 and 29 areused appropriatelyE.The assert st
3、atements on lines 29 and 33 are usedappropriatelyF.The assert statements on lines 29,31 and 33 areused appropriatelyG.The assert statements on lines 26,29 and 31 areused appropriately断言应该用在“你认为 你的程序不可能产生错误的地方,而且有没有启用断言,都不会影响程序的正常运行.断言使用限制:1.不要在public方法中,用断言来检查参数的正确性;2.不要让断言语句去处理一些程序必须的流程.原因:l.public
4、方法会被别人调用,你不能保证他一定启用断言;如果没有启用,那么用断言来做参数的检查也就没有意义了.所以不要用断言来检查参数,公共方法的参数一定要用代码执行检查;2.如果用断言来控制程序执行流程,如果没有启用断言,那么程序就不能正确执行下去。另外,断言语句不可以有任何边界效应,不要使用断言语句去修改变量和改变方法的返回值,如果这样当启动断言和不启动断言执行的结果会截然不同。断言的使用时机:1.检查流程的不变性:在if-else switch-case的预期结果之外可以加上断言做额外的检查。2.内部执行的不变性:if(true)return;assert false;3.检查私有方法的参数,结果等
5、4.程序运行中的一致性断言语句不是永远会执行,可以屏蔽也可以启用javac-source 1.4*.java需要java-e a启用assert;当判断条件为FALSE时就抛出错误。Answer:(C)26行不合适:不要对public方法的参数断言29合适:程序员在程序中最不大可能到达的地方断言31合适:断言private方法的参数33行不合适:启用和不启用断言会产生不同的程序执行序参考大纲:异常处理一断言和AssertionErrorQUESTION 62GIVEN THE EXHIBIT:11 public static void main(String args)(12.String s
6、tr=nuir;13.if(str=n u l l)14,system,outpnntln CnulT),15.)else(str length()=0)(16,system.outpnntln(zero)17.)eke(18,system out.pnntln(somu。19)20)What is the result?A.nullB.zeroC.someD.Compilation failsE.An exception is thrown at runtimeAnswer:(D)13行会报错,应在15行使用else参考大纲:流程控制QUESTION 63Given the exhibit:
7、11 p u b l i c s t a t i c v o i d m a i n (S t r i n g a r g s)(12.try(13 args=null,14 a e g s 0 =t e s t ;15.S y s t e m,o u t p r i n t In (a r g s 0,16)c a t c h (Ex c e p t i o n e x)(17 S y s t e m o u t p n n t l n (Ex c e p t i o n ),18.)c a t c h (Nu l l Po i n t e r Ex c e p t i o n q)e)(1
8、9 S y s t e m o u t p r i n t l n (g Nu l l Po i n t t e r Ex c e p i o nw),20.)21.)What is the result?A.testB.ExceptionC.Compilation failsD.NullPointerExceptionAnswer:(C)1 8 行 出 错,应 该 先 catch子 异 常,再 catchException;13行把args赋 null,14行会报 NuIlPointerException如 果 没 有 第 1 3 行 运 行 时 1 4 行 会 报Array工ndexOut
9、OEBoundsException 异常。参考大纲:异常处理QUESTION 64Given the exhibit:1.import java util*,23 public class Let terASort(4 public static void mam(String args)5 AxrayList strings=new ArrayList (),6 strings add(aAaA);7.strings,add(AaA);8.strings add(、A a)9 strings add(A A aa)10.Collections.$ort(strings);11 for(Str
10、ing s strings)(System ourpnnt(s+“)12)13.)What is the result?A.Compilation failsB.aAaA aAa AAaa AaAC.AAaa AaA aAa aAaAD.AaA AAaa aAaA aAaE.aAa AaA aAaA AAaaF.An exception is thrown at runtimeAnswer:(C)第 10行将对strings这个集合做自然排序(ASCII小到大,一个一个比较)Collections.sort(List list)对 list 进行排序,对 set不能排序!List里可以放对象,
11、所以当list里面存放的是对象的时候就不能用CoHections.sort(List list)去排序了。因为JVM不知道用什么规则去排序!只有把对 象 类 实 现 Comparable接 口,然 后 改 写compareTo()参考大纲:集合QUESTION 65Given the exhibit:1.i m p o r t j a v a u t i l *;2 p u b l i c c l a s s W r a p p e d S t r i n g (3 p r i v a t e S t r i n g s;4 p u b l i c W r a p p e d S t r i
12、n g (S t r i n g s)(t h i s.s =s,)5.p u b l i c s t a t i c v o i d mam(S t r i n g a r g s)(6 Ha s h S e t h s =n e w Ha s h S e t ();7 W r a p p e d S t r i n g w s l =n e w W r a p p e d S t r i n g (Ma a r d v a r k*,8.WrappedString ws2=new WrappedString(aardvark)9 S t r i n g s i =n e w S t r i
13、 n g (a a r d v a r k*);10.S t r i n g s 2=n e w S t r i n g (a a i d v a u k)11 h s a d d (w s l),h s a d d (w s 2).;h s a d d(s l),h s a d d(s 2),12.S y s t e m.o u t.p n n t(h s s i z e ().)What is the result?A.OB.1C.2D.3E.4F.Compilation fails.G.An exception is thrown at runtimeAnswer:(D)S et中存放的
14、元素是无序不重复的。如果你想Set里 Add 一个元素,首先他会去调用equals方法,判断set中是否有该元素,如果有则不更改set的值并返回false,如果没有,则把元素添加进去,并返回 true。Wsl ws2是自定义的类,w s l和 ws2 equals不相等;String的equals方法已经改写,s i 和 s2相等;比较两个对象是否相同,先比较hashcode,如果相同,在 用 equals方法比较.如果都相同则两个对象就认为是相同的.Hashcode是把对象的内存地址经过运算得来的.基本数据类型和基本数据类型的包装类还有String类都已经覆盖了 hashcode(),equ
15、als。,所以这些对象的值只要一样就认为对象一样.参考大纲:集合QUESTION 66Given a pre-generics implementation of a method:11 public static int sum(List list)12.in t sum=0;13.for(Iterator iter=list iterator();iter haNext();)(14 int i=(Integer)iter next()intValue(),15.sum-H=i,16.)17.return sum1 8.)Which three changes must be made t
16、o the methodsum to use generics?(choose three)A.remove line 14B.replace line 14 with int i=iter.next();”C.replace line 13 with for(int i:intList)*D.replace line 13 with n for(Iterator iter:intList)f!E.replace the method declaration with sum(List intList)nF.replace the method declaration with”sum(Lis
17、t intList)nAnswer:(A,C,F)public static int sum(List intList)int sum=0;for(int i:intList)sum+=i;)return sum;)参考大纲:集合和泛型QUESTION 67Given the exhibit:23.Object myObjects=24 new Integer(12)25.new String(foo*),26.new Integer(5),27.new Boolean(true)28.29 Arrays,sort(myObjects);30.(int i-0,imyObjccts.lengt
18、h,i+)(31.System.out.print(myObjects i.toString();32.Sysem.out.print(*);33.What is the result?A.Compilation fails due to an error in line 23.B.Compilation fails due to an error in line 29.C.A ClassCastExceptation occurs in line 29.D.A ClassCastExceptation occurs in line 31.E.The value of all four obj
19、ect prints in naturalorder.Answer:(C)Arrays.sort(Object a)方法中,a 的每一个元素都必须是相互可以比较的(调用pareTo(Objecto2)o 否则会报 ClassCastException 的异常。参考大纲:泛型QUESTION 68Place the code into position to create a class thatmaps from Strings to Integer values.The result of execution must be one.Some optionsmay be used more
20、than once.G ive n:N uaberNam es nn n e v N uberN aes().nn p u t(o n e,1);S y s t s o u t.p r in t ln(n n g e tN a m e s().p u b lic c la s s N ujuberN axesp r iv a te HashMapPlace heren e v HashMap Hap Place herePlace herep u b lic v o id p u t(S t r in g name i n t v a lu e)Place here);p u b licPla
21、ce hereqetNaMes()r e t u r n ap k e y S e t(),)Code|S e t|S o tI n t e g g r|B a sh S o t S e t In te g e r,S t r in g|S e P n t S t r in g|S e t S tr in g.I n t e g e rS e t S tr*n g|S e tS tri.n g|NujaberNames Answer:()public class NumberNames private HashMap map=new HashMap();public void put(Stri
22、ng name,Integer value)map.put(name,value);)public Set getNames()Return map.keySet();)QUESTION 69Place a result onto each method call to indicatewhat would happen if the method callwere inserted at line 9.Note:Results can be usedmore than once.12345678901231111Givenxaport java u til:cla ss A (cla ss
23、B extends A(public cla ss Test(public sta tic void mam(Strin9 args)List HstA nev LinkedLisKA().List listB new LinkedList().ListcOb3ect)listO nev IinkedLxsKObect();/Insert code herepublic sta tic void MA(L ist lis t)public sta tic void 2(List l i s t)Method Calls ResultM l(llStA);M2(listA).Does notco
24、mpte*1(listB)Ccmpiet and funs Mtout wor*1(IxstC)An exception tt thrown d runhrneAnswer:()MethodResultml(list A)Compiles and runswithout error泛型规范没问题m2(listA)Compiles and runswithout error泛型规范没问题ml(listB)Compiles and runswithout errorB 是 A 的子类,泛型规范没问题m2(listB)Does not compileml(listO)Does not compile
25、m2(listO)Does not compileQUESTION 70Given the exhibit:1 i m p o r t j a v a.u t i l.*,2 p u b l i c c l a s s P Q (3 public static void main(String args)(4 PnontyQueue pq=new PnontyQueue(),5 p q a d d Cc a r r o O,6 p q.a d da p p l u ),7 p q a d d Cb a n a n a)8 S y s t t e m.o u t.p r i n t l n (p
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SCJP 认证 考试 题库
限制150内