SCJP认证考试题库2.docx
《SCJP认证考试题库2.docx》由会员分享,可在线阅读,更多相关《SCJP认证考试题库2.docx(40页珍藏版)》请在冰豆网上搜索。
SCJP认证考试题库2
QUESTION61
GIVENTHEEXHIBIT:
Whichstatementistrue?
A.Alloftheassertstatementsareusedappropriately.
B.Onlytheassertstatementonline31isusedappropriately
C.Theassertstatementsonlines29and31areusedappropriately
D.Theassertstatementsonlines26and29areusedappropriately
E.Theassertstatementsonlines29and33areusedappropriately
F.Theassertstatementsonlines29,31and33areusedappropriately
G.Theassertstatementsonlines26,29and31areusedappropriately
断言应该用在“你认为”你的程序不可能产生错误的地方,而且有没有启用断言,都不会影响程序的正常运行。
断言使用限制:
1.不要在public方法中,用断言来检查参数的正确性;
2.不要让断言语句去处理一些程序必须的流程。
原因:
方法会被别人调用,你不能保证他一定启用断言;如果没有启用,那么用断言来做参数的检查也就没有意义了。
所以不要用断言来检查参数,公共方法的参数一定要用代码执行检查;2.如果用断言来控制程序执行流程,如果没有启用断言,那么程序就不能正确执行下去。
另外,断言语句不可以有任何边界效应,不要使用断言语句去修改变量和改变方法的返回值,如果这样当启动断言和不启动断言执行的结果会截然不同。
断言的使用时机:
1.检查流程的不变性:
在if-elseswitch-case的预期结果之外可以加上断言做额外的检查。
2.内部执行的不变性:
if(true){return;}assertfalse;
3.检查私有方法的参数,结果等
4.程序运行中的一致性
断言语句不是永远会执行,可以屏蔽也可以启用
javac–source*.java
需要java–ea启用assert;当判断条件为FALSE时就抛出错误。
Answer:
(C)
26行不合适:
不要对public方法的参数断言
29合适:
程序员在程序中最不大可能到达的地方断言
31合适:
断言private方法的参数
33行不合适:
启用和不启用断言会产生不同的程序执行序
参考大纲:
异常处理—断言和AssertionError
QUESTION62
GIVENTHEEXHIBIT:
Whatistheresult?
A.null
B.zero
C.some
D.Compilationfails
E.Anexceptionisthrownatruntime
Answer:
(D)
13行会报错,应在15行使用elseif
参考大纲:
流程控制
QUESTION63
Giventheexhibit:
Whatistheresult?
A.test
B.Exception
C.Compilationfails
D.NullPointerException
Answer:
(C)
18行出错,应该先catch子异常,再catchException;
13行把args赋null,14行会报NullPointerException
如果没有第13行运行时14行会报ArrayIndexOutOfBoundsException异常。
参考大纲:
异常处理
QUESTION64
Giventheexhibit:
Whatistheresult?
A.Compilationfails
B.aAaAaAaAAaaAaA
C.AAaaAaAaAaaAaA
D.AaAAAaaaAaAaAa
E.aAaAaAaAaAAAaa
F.Anexceptionisthrownatruntime
Answer:
(C)
第10行将对strings这个集合做自然排序(ASCII小到大,一个一个比较)
(Listlist)对list进行排序,对set不能排序!
List里可以放对象,所以当list里面存放的是对象的时候就不能用(Listlist)去排序了。
因为JVM不知道用什么规则去排序!
!
只有把对象类实现Comparable接口,然后改写compareTo()
参考大纲:
集合
QUESTION65
Giventheexhibit:
Whatistheresult?
A.0
B.1
C.2
D.3
E.4
F.Compilationfails.
G.Anexceptionisthrownatruntime
Answer:
(D)
Set中存放的元素是无序不重复的。
如果你想Set里Add一个元素,首先他会去调用equals方法,判断set中是否有该元素,如果有则不更改set的值并返回false,如果没有,则把元素添加进去,并返回true。
Ws1ws2是自定义的类,ws1和ws2equals不相等;String的equals方法已经改写,s1和s2相等;
比较两个对象是否相同,先比较hashcode,如果相同,在用equals方法比较.如果都相同则两个对象就认为是相同的.
Hashcode是把对象的内存地址经过运算得来的.
基本数据类型和基本数据类型的包装类还有String类都已经覆盖了hashcode(),equals(),所以这些对象的值只要一样就认为对象一样.
参考大纲:
集合
QUESTION66
Givenapre-genericsimplementationofamethod:
Whichthreechangesmustbemadetothemethodsumtousegenerics?
(choosethree)
A.removeline14
B.replaceline14with"inti=();"
C.replaceline13with"for(inti:
intList){"
D.replaceline13with"for(Iteratoriter:
intList)"
E.replacethemethoddeclarationwith"sum(ListintList)"
F.replacethemethoddeclarationwith"sum(ListintList)"
Answer:
(A,C,F)
publicstaticintsum(ListintList){
intsum=0;
for(inti:
intList){
sum+=i;
}
returnsum;
}
参考大纲:
集合和泛型
Whatistheresult?
A.Compilationfailsduetoanerrorinline23.
B.Compilationfailsduetoanerrorinline29.
C.AClassCastExceptationoccursinline29.
D.AClassCastExceptationoccursinline31.
E.Thevalueofallfourobjectprintsinnaturalorder.
Answer:
(C)
(Object[]a)方法中,a的每一个元素都必须是相互可以比较的(调用(Objecto2))。
否则会报ClassCastException的异常。
参考大纲:
泛型
QUESTION68
PlacethecodeintopositiontocreateaclassthatmapsfromStringstoIntegervalues.
Theresultofexecutionmustbe[one].Someoptionsmaybeusedmorethanonce.
Answer:
()
publicclassNumberNames{
privateHashMapmap=
newHashMap();
publicvoidput(Stringname,Integervalue){
(name,value);
}
publicSetgetNames(){
Return();
}
}
QUESTION69
Placearesultontoeachmethodcalltoindicatewhatwouldhappenifthemethodcall
wereinsertedatline9.Note:
Resultscanbeusedmorethanonce.
Answer:
()
Method
Result
m1(listA)
Compilesandrunswithouterror
泛型规范没问题
m2(listA)
Compilesandrunswithouterror
泛型规范没问题
m1(listB)
Compilesandrunswithouterror
B是A的子类,泛型规范没问题
m2(listB)
Doesnotcompile
m1(listO)
Doesnotcompile
m2(listO)
Doesnotcompile
QUESTION70
Giventheexhibit:
Whatistheresult?
A.apple:
apple
B.carrot:
apple
C.apple:
banana
D.banana:
apple
E.carrot:
carrot
F.carrot:
banana
Answer:
(C)
PriorityQueue优先级队列:
预设是自然排序,因此pq内的元素顺序将是apple->banana->carrot
poll()取第一个元素,取完之后删除,peek取第一个元素,并不删除元素
QUESTION71
Given:
AprogrammerisdevelopingaclassKey,thatwillbeusedasakeyinastandard
Whichtwomethodsshouldbeoverriddentoassurethatkeyworkscorrectlyasa
key?
(choosetwo)
A.publicinthashCode()
B.publicBooleanequals(Keyk)
C.publicintcompareTo(Objecto)
D.publicBooleanequals(Objecto)
Answer:
(A,D)
HashMap中的key不能重复,因此必须实现hashCode和equals方法
QUESTION72---
Giventheexhibit:
Whichtwo,insertedatline2willallowthecodetocompile?
(ChooseTwo)
A.publicclassMinMax
>{
B.publicclassMinMax
extendsNumber>{
C.publicclassMinMax{
D.publicclassMinMax{
E.publicclassMinMax
extendsObject>{
F.publicclassMinMax{
Answer:
(D,F)
N是泛型类别变量,它相当于一个类型。
A类声明是不能用?
这个万用字符。
B同上
CObject没有doubleValue()方法。
DOK.
E同A。
FOK.
QUESTION73
Giventheexhibit:
enumExample{ONE,TWO,THREE}
Whichstatementistrue?
A.Theexpressions(ONE==ONE)and(ONE)arebothguaranteedtobetrue.
B.Theexpression(ONEisguaranteedtobelessthanone.
C.TheExamplevaluescannotbeusedinarawinstead,the
programmermustusea
D.TheExamplevaluescanbeusedinabutthesetwillNOTbe
sortedbecauseenumeratedTypedoNOTIMPLEMENT
Answer:
(A)
A正确
B不能保证
C错误枚举类型的元素是可以用在HASHMAP里的
HashMaph=newHashMap;
(1,;
D错误,枚举类型的元素值是可以放入SortedSet里的例如TreeSet.把枚举类型的元素转成字符串在放入其中,字符串就已经实现comparable接口。
可以自然排序。
SortedSets=newTreeSet();
;
;
打印出结果[ONE,TWO]
QUESTION74
Giventheexhibit:
WhichlineofcodemarkstheearliestpointthatanobjectreferencedbyintObj
becomesacandidateforgarbagecollection?
A.Line16
B.Line17
C.Line18
D.Line19
E.TheobjectisNOTacandidateforgarbagecollection.
Answer:
(D)
intObj一直会被numbers引用,直到19行之后
QUESTION75
Given:
Andthecommandlineinvocation:
javaYippee2abc
Whatistheresult?
A.ab
B.bc
C.abc
D.Compilationfails.
E.Anexceptionisthrownatruntime.
Answer:
(B)
QUESTION76
Aclassiscorrectlydefinedinthejarfile.Auserwants
toexecutethemainmethodofPokeronaUNIXsystemusingthecommand:
Java
Whatallowstheusertodothis?
A.putindirectory/stuff/java,andsettheCLASSPATHtoinclude/stuff/java
B.putindirectory/stuff/java,andsettheCLASSPATHtoinclude
/stuff/java/*.jar
C.putindirectory/stuff/java,andsettheCLASSPATHtoinclude
/stuff/java/
D.putindirectory/stuff/java/games/cards,andsettheCLASSPATHtoinclude
/stuff/java
E.putindirectory/stuff/java/games/cards,andsettheCLASSPATHtoinclude
/stuff/java/*.jar
F.putindirectory/stuff/java/games/cards,andsettheCLASSPATHtoinclude
/stuff/java/
Answer:
(C)
classpath中须明确写明,不能用*.jar
QUESTION77
Whichthreecodefragments,addedindividuallyatline29,producetheoutput100?
(Choosethree.)
A.n=100;
B.(100);
C.().setX(100);
D.i=newInner();(100);
E.(i);i=newInner();(100);
F.i=newInner();(100);(i);
Answer:
(B,C,F)
根据内存关系可知
QUESTION78
GivenaclassRepetition:
AndgivenanotherclassDemo:
Whichcodeshouldbeinsertedatline1oftocompileandrunDemotoprint"pizzapizza"
A.importutils.*;
B.staticimportutils.*;
C.import.*;
D.staticimport.*;
E.import
F.importstatic
G.staticimport(F)
静态import引入静态方法.importstaticutils..Repetition*;或者importstatic正常的导入类,Demo的第5行要new实例才能带用twice方法.
Bstaticimport顺序颠倒,而且要加上class名
C正常的导入,类名后面不能跟*
D顺序颠倒
E正常的导入,只能导入包下的类,不能导入类里的方法.
G顺序颠倒
QUESTION79
Given:
Andtheinvocation:
Whatistheresult?
A.Anexceptionisthrownatruntime.
B."Stringisempty"isprintedtooutput.
C.Compilationfailsbecauseofanerrorinline12.
D."Stringisnotempty"isprintedtooutput.
Answer:
(A)
A空指针异常------------------“|”是非短路逻辑运算符
QUESTION80
Exhibit:
Giventhefully-qualifiedclassnames:
WhichgraphrepresentsthecorrectdirectorystructureforaJARfilefromwhich
thoseclassescanbeusedbythecompilerandJVM?
A.JarA
B.JarB
C.JarC
D.JarD
E.JarE
Answer:
(A)
QUESTION81
Given:
andtwoseparatecommandlineinvocations:
javaYippee
javaYippee1234
Whatistheresult?
A.Nooutputisproduced.
123
B.Nooutputisproduced.
234
C.Nooutputisproduced.
1234
D.Anexceptionisthrownatruntime.
123
E.Anexceptionisthrownatruntime.
234
F.Anexceptionisthrownatruntime.
1234
Answer:
(B)
QUESTION82
Given:
Whatistheresult?
A.Compilationfails
B.Anexceptionisthrownatruntime
C.doStuffx=6mainx=6
D.doStuffx=5mainx=5
E.doStuffx=5mainx=6
F.doStuffx=6mainx=5
Answer:
(D)
第10行参数x是值传递,不会改变main中x的值。
QUESTION83
Given:
WhenthedoSomethingmethodiscalled,afterwhichlinedoestheObject