《SCJP认证考试题库2.pdf》由会员分享,可在线阅读,更多相关《SCJP认证考试题库2.pdf(66页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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
26、 q p o l l 0 +:*-f p q.p e e k(),9.)W.)What is the result?A.apple:appleB.carrot:appleC.apple:bananaD.banana:appleE.carrot:carrotF.carrot:bananaAnswer:(C)PriorityQueue优先级队列:预设是自然排序,因此pq内的元素顺序将是apple-banana-carrotpoll。取第一个元素,取完之后删除,peek取第一个元素,并不删除元素QUESTION 71Given:11.public class key(12.private long
27、idl;13.private long id2,1415./class key methods16.)A programmer is developing a class Key,that willbe used as a key in a standardjava.util.HashMap.Which two methods should be overridden to assurethat key works correctly as akey?(choose two)A.public int hashCode()B.public Boolean equals(Key k)C.publi
28、c int compareTo(Object o)D.public Boolean equals(Object o)Answer:(A,D)HashMap中 的 k ey 不能重复,因此必须实现hashCode 和 equals 方法QUESTION 72 Given the exhibit:2.H insert code here3 private N min.max,4 public N getMin()return m in,)5 public N getMax()(return m ax.)6 public void add(N add ed)(if(min=null 11 addsd
29、 double Value()min doubleValue()8 mm=added,9if(max=null 11 added doubleValue()max.doubleValue()1 max=added,1 1)12)Which two,inserted at line 2 will allow the code tocompile?(Choose Two)A.public class MinMax B.public class MinMax C.public class MinMax D.public class MinMax E.public class MinMax F.pub
30、lic class MinMax Answer:(D,F)N 是泛型类别变量,它相当于一个类型。A 类声明是不能用?这个万用字符。B 同上C Object 没有 double Value。方法。D OK.E 同A。F OK.QUESTION 73Given the exhibit:enum Example ONE,TWO,THREE Which statement is true?A.The expressions(ONE=ONE)andONE.equals(ONE)are both guaranteed to be true.B.The expression(ONE TWO)is guara
31、nteed tobe true and ONE.compareTo(TWO)is guaranteed to be less than one.C.The Example values cannot be used in a rawjava.util.HashMap.;instead,theprogrammer must use a java.util.EnumMap.D.The Example values can be used in ajava.util.SortedSet,but the set will NOT besorted because enumerated Type do
32、NOTIMPLEMENT java.lang.Comparable.Answer:(A)A 正确B 不能保证C 错误 枚举类型的元素是可以用在HASHMAP里的HashMaph=newHashMap;h.put(l,Example.ONE);D 错误,枚举类型的元素值是可以放入SortedSet里的例如TreeSet.把枚举类型的元素转成字符串在放入其中,字符串就已经实现comparable接口。可以自然排序。SortedSet s=new TreeSet();s.add(Example.TW O);s.add(Example.ONE);System.out.println(s);打印出结果O
33、NE,TWOQUESTION 74Given the exhibit:11.public void genNuabers()12.ArrayList numbers-new ArrayList();13.for(int i,0;i10;i+)14 int value-i (int)Hath.xando()15 Integer intObj new Integer(value);16.nuMbers add(mtObj):17.)18 System.out print In(nuMbers)#19.)Which line of code marks the earliest point that
34、 anobject referenced by intObjbecomes a candidate for garbage collection?A.Line 16B.Line 17C.Line 18D.Line 19E.The object is NOT a candidate for garbagecollection.Answer:(D)intObj 一直会被numbers引用,直到19行之后QUESTION 75Given:12 p u b lic c la s s Yippee2 13.14 s ta tic p u b lic void inain(S tring yahoo)IS
35、 .o r(in t x-1;x And the command line invocation:javaYippee2 a b cWhat is the result?A.a bB.b cC.a b cD.Compilation fails.E.An exception is thrown at runtime.Answer:(B)QUESTION 76A class games.cards.Poker is correctly defined inthe jar file Poker.jar.A user wantsto execute the main method of Poker o
36、n a UNIXsystem using the command:Java games.cards.PokerWhat allows the user to do this?A.put Poker.jar in directory/stuff/java,and set theCLASSPATH to include/stuff/javaB.put Poker.jar in directory/stufjava,and set theCLASSPATH to include/stuff/java/*.jarC.put Poker.jar in directory/stuff/java,and s
37、et theCLASSPATH to include/stuff/java/Poker.jarputPoker.jarin/stufjava/games/cards,andto include/stuff/javaset thedirectoryCLASSPATHE.putPoker.jarin/stuff/java/games/cards,andto include/stuff/java/*.jarset thedirectoryCLASSPATHputPoker.jarin/stufjava/games/cards,andto includeset thedirectoryCLASSPAT
38、H/stuff/j ava/Poker.j arAnswer:(C)classpath中须明确写明poker.jar,不能用*.jarQUESTION 770123456789012345678901211111111112222222222333class Inner p riv ate in t x;public void setX(in t x)public int getX()return x1Xxzclass Outer p riv ate Inner y;public void setY(Inner y)th is.y V-public Inner getY()return y;p
39、ublic class Gamma public s ta tic void m ain(Strm g args)Outer o-new Outer()#Inner i new Inner();in t n 10;i.setX(n);o.setY(i);/in sert code hereSystem out p rin tln(o getY()getX();)Which three code fragments,added individually atline 29,produce the output 100?(Choose three.)A.n=100;B.LsetX(lOO);C.o
40、.getY().setX(100);D.i=new Inner();i.setX(100);E.O.setY(i);i=new Inner();i.setX(100);F.i=new Inner();i.setX(100);o.setY(i);Answer:(B,C,F)根据内存关系可知QUESTION 78Given a class Repetition:1.package u tils;2.3 p u b lic c la s s R ep etitio n !4 p u b lic s ta t ic S trin g tv ic e(S trin g s)re tu rn s+s,5
41、.)And given another class Demo:1./in s e rt code here3.p u b lic c la s s Deao 4.p u b lic s ta tic void n ain(S trm (args)5.Sy stew out p rm tln(tw ice(p izza);7:Which code should be inserted at line 1 ofDemo.java to compile and run Demo to printn pizzapizza nA.import utils.*;B.static import utils.
42、*;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)静 态 im port引 入 静 态 方 法.import staticutils.Repetition*;或 者 import staticutils.Repetition.twice;A 正常的导入类,Dem。的第5 行要
43、new实例才能带用 twice方法.B static import顺序颠倒,而且要加上class名C 正常的导入,类名后面不能跟*D 顺序颠倒E 正常的导入,只能导入包下的类,不能导入类里的方法.G 顺序颠倒QUESTION 79Given:11 public static void test(String str)12 if(str null|str.length()-0)13.System out println(String is empty);14.else 15 System out println(String xs not epty).16.)17.And the invocat
44、ion:31.te s t(n u ll);What is the result?A.An exception is thrown at runtime.B.HString is emptyn is printed to output.C.Compilation fails because of an error in line 12.D.n String is not emptyn is printed to output.Answer:(A)A 空指针异常符“I”是非短路逻辑运算QUESTION 80Exhibit:Given the fully-qualified class names
45、:com.foo.bar.Dogcom.foo.bar.blatz.Bookcom.bar.Carcom.bar.blatz.SunWhich graph represents the correct directorystructure for a JAR file from whichthose classes can be used by the compiler andJVM?A.Jar AB.Jar BC.JarCD.Jar DE.Jar EAnswer:(A)QUESTION 81Given:56789011111122p u b lic c la s s Yippee p u b
46、 lic s ta tic void M ain(Strm g args)f o r(in t x*1;x 3)System out print(pi is bigger than 3.*);else System out print(pi is not bigger than 3.finallv System out printlnf Have a nice day),What is the result?A.Compilation failsB.pi is bigger than 3.C.An exception occurs at runtime.D.pi is bigger than
47、3.Have a nice day.E.pi is not bigger than 3.Have a nice day.Answer:(A)18行finally是配合try使用的.QUESTION 90Given:0123456781A1X1X111 AA1xinterface Foo class Alpha implements Foo class Beta extends Alpha class Delta extends Beta(public static void main(String1 args)Beta x new Bets()r/insert code here)Which
48、code,inserted at line 16 will cause ajava.lang.ClassCastException?A.Alpha a=x;B.Foo f=(Delta)x;C.Foo f=(Alpha)x;D.Beta b=(Beta)(Alpha)x;Answer:(B)转到子类别抛异常。.QUESTION 91Given a method that must ensure that itsparameter is not null:11.p u b lic void soneMethcxd(Object value)12./check fo r n u ll value2
49、0.System out.println(value getClass():21What,inserted at line 12,is the appropriate way tohandle a null value?A.assert value=null;B.assert value!=null,nvalue is null*;C.if(value=null)throw new AssertionException(1*value is null*);)D.if(value=null)throw new IllegalArgumentExceptioii(*!value ismill);)
50、Answer:(D)A不要用断言对公共方法的参数进行判断B同上C 没 有 AssertionException 这个类;AssertionEiror 继承自 Error,handle error 是没有意义的D O K抛出参数错误的异常.QUESTION 92Place the correct Code in the Code Sample toachieve the expected results.Expected results:Output:1 2 4 8 16 32Code Samplei nt=1,2,4,8,16.32);System.out.prirCOvtput:);|Pla