JAVA认证模拟试题程序类.docx
- 文档编号:6710170
- 上传时间:2023-01-09
- 格式:DOCX
- 页数:19
- 大小:26.41KB
JAVA认证模拟试题程序类.docx
《JAVA认证模拟试题程序类.docx》由会员分享,可在线阅读,更多相关《JAVA认证模拟试题程序类.docx(19页珍藏版)》请在冰豆网上搜索。
JAVA认证模拟试题程序类
30道1.4模拟经典题(附详解)
1.Whatwillhappenwhenyouattempttocompileandrunthefollowingcode?
(Assumethatthecodeiscompiledandrunwithassertionsenabled.)
以下程序编译时或则运行时出现的结果是:
publicclassAssertTest{
publicvoidmethodA(inti){
asserti>=0:
methodB();
System.out.println(i);
}
publicvoidmethodB(){//无返回值
System.out.println("Thevaluemustnotbenegative");
}
publicstaticvoidmain(Stringargs[]){
AssertTesttest=newAssertTest();
test.methodA(-10);
}
}
A.itwillprint-10
B.itwillresultinAssertionErrorshowingthemessage-“thevaluemustnotbenegative”.
C.thecodewillnotcompile.
D.Noneofthese.
Ciscorrect.Anassertstatementcantakeanyoneofthesetwoforms-
assertExpression1;
assertExpression1:
Expression2;
Notethat,inthesecondform;thesecondpartofthestatementmustbeanexpression-Expression2.Inthiscode,themethodB()returnsvoid,whichisnotanexpressionandhenceitresultsinacompiletimeerror.ThecodewillcompileifmethodB()returnsanyvaluesuchasint,Stringetc.
Also,inbothformsoftheassertstatement,Expression1musthavetypebooleanoracompile-timeerroroccurs.
2.Whatwillhappenwhenyouattempttocompileandrunthefollowingcode?
以下程序编译时或则运行时出现的结果是:
publicclassStatic{
static{
intx=5;//在static内有效
}
staticintx,y;//初始化为0
publicstaticvoidmain(Stringargs[]){
x--;//-1
myMethod();
System.out.println(x+y+++x);
}
publicstaticvoidmyMethod(){
y=x+++++x;//y=-1+1x=1
}
}
A.compiletimeerror
B.prints:
1
C.prints:
2
D.prints:
3
E.prints:
7
F.prints:
8
Disthecorrectchoice.Theabovecodewillnotgiveanycompilationerror.Notethat"Static"isavalidclassname.ThuschoiceAisincorrect.
Intheabovecode,onexecution,firstthestaticvariables(xandy)willbeinitializedto0.Thenstaticblockwillbecalledandfinallymain()methodwillbecalled.Theexecutionofstaticblockwillhavenoeffectontheoutputasitdeclaresanewvariable(intx).
Thefirststatementinsidemain(x--)willresultinxtobe-1.AfterthatmyMethod()willbeexecuted.Thestatement"y=x+++++x;"willbeevaluatedtoy=-1+1andxwillbecome1.Incasethestatementbe"y=++x+++x",itwouldbeevaluatedtoy=0+1andxwouldbecome1.FinallywhenSystem.outisexecuted"x+y+++x"willbeevaluatedto"1+0+2"whichresultin3astheoutput.ThuschoiceDiscorrect.
3.Giventhefollowingcode,whatwillbetheoutput?
下列代码输出的结果是:
classValue{
publicinti=15;
}
publicclassTest{
publicstaticvoidmain(Stringargv[]){
Testt=newTest();
t.first();
}
publicvoidfirst(){
inti=5;
Valuev=newValue();
v.i=25;
second(v,i);
System.out.println(v.i);
}
publicvoidsecond(Valuev,inti){
i=0;
v.i=20;
Valueval=newValue();
v=val;
System.out.println(v.i+""+i);
}
}
A.15020
B.15015
C.20020
D.01520
Aiscorrect.WhenwepassreferencesinJavawhatactuallygetspassedisthevalueofthatreference(i.e.memoryaddressoftheobjectbeingreferencedandnottheactualobjectreferencedbythatreference)anditgetspassedasvalue(i.eacopyofthereferenceismade).Nowwhenwemakechangestotheobjectreferencedbythatreferenceitreflectsonthatobjectevenoutsideofthemethodbeingcalledbutanychangesmadetothereferenceitselfisnotreflectedonthatreferenceoutsideofthemethodwhichiscalled.Intheexampleabovewhenthereferencevispassedfrommethodfirst()tosecond()thevalueofvispassed.Whenweassignthevaluevaltovitisvalidonlyinsidethemethodsecond()andthusinsidethemethodsecond()whatgetsprintedis15(initialvalueofiintheobjectreferencedbyval),thenablankspaceandthen0(valueoflocalvariablei).Afterthiswhenwereturntothemethodfirst()vactuallyreferstothesameobjecttowhichitwasreferringbeforethemethodsecond()wascalled,butonethingshouldbenotedherethatthevalueofiinthatobject(referredbyvinsidethemethodfirst())waschangedto20inthemethodsecond()andthischangedoesreflectevenoutsidethemethodsecond(),hence20getsprintedinthemethodfirst().Thusoveralloutputofthecodeinconsiderationis
150
20
4.Whatwillhappenwhenyouattempttocompileandrunthefollowingcode?
以下程序编译时或则运行时出现的结果是:
classMyParent{
intx,y;
MyParent(intx,inty){
this.x=x;
this.y=y;
}
publicintaddMe(intx,inty){
returnthis.x+x+y+this.y;
}
publicintaddMe(MyParentmyPar){
returnaddMe(myPar.x,myPar.y);
}
}
classMyChildextendsMyParent{
intz;
MyChild(intx,inty,intz){
super(x,y);
this.z=z;
}
publicintaddMe(intx,inty,intz){
returnthis.x+x+this.y+y+this.z+z;
}
publicintaddMe(MyChildmyChi){
returnaddMe(myChi.x,myChi.y,myChi.z);
}
publicintaddMe(intx,inty){
returnthis.x+x+this.y+y;
}
}
publicclassMySomeOne{
publicstaticvoidmain(Stringargs[]){
MyChildmyChi=newMyChild(10,20,30);
MyParentmyPar=newMyParent(10,20);
intx=myChi.addMe(10,20,30);
inty=myChi.addMe(myChi);
intz=myPar.addMe(myPar);
System.out.println(x+y+z);
}
}
A.300
B.240
C.120
D.180
E.compileerror
F.noneoftheabove
Aisthecorrectchoice.Intheabovecode,MyChildclassoverridestheaddMe(intx,inty)methodoftheMyParentclass.AndinboththeMyChildandMyParentclass,addMe()methodisoverloaded.Thereisnocompilationerroranywhereintheabovecode.
Onexecution,first,theobjectofMyChildclasswillbeconstructed.Pleasenotethatthereisasuper()callfromtheconstructorofMyChildclass,whichwillcalltheconstructorofMyParentclass.ThiswillcausethevalueofzvariableofMyChildclasstobe30andx,yvariablesofMyParentclasswillbecome10and20respectively.ThenextstatementwillagaincalltheconstructorofMyParentclasswithsamexandyvalues.ThisisfollowedbyexecutionofaddMe()methodofMyChildclasswithxas10,yas20andzas30.AlsoxandyareinheritedbyMyChildclassfromtheMyParentclass.ThusintheaddMe()methodoftheMyChildclass,thevalueofthis.xwillbe10,this.ywillbe20andthis.zwillbe30.Thereturnvalueofthismethodwillbe"10+10+20+20+30+30",whichisequalto120.Thusxwillbecome120.
ThisisfollowedbytheinvocationoftheotheraddMe()methodwhichtakesobjectreferenceoftheMyChildclass.Fromthismethod,themethodwhichwascalledearlierisinvoked.Thiscallisexactlythesameastheearlierone.Thusthevalueofywillalsobe120likex.
NowtheaddMe()methodofMyParentclassisinvoked.ThismethodinvokesanotheraddMe()methodofthesameclass.ItsequivalenttotheinvocationofaddMe(intx,inty)methodwithxas10andyas20.AlsothevalueofinstancevariablesxandyofMyParentclassis10and20respectively.Thevalueofzwillbeevaluatedto"10+10+20+20",whichisequalto60.Thusthevalueofx,yandzafteralltheinvocationswillbe120,120and60respectively.Asaresultofthisfinally,"120+120+60"whichisequalto300willbeprinted.ThusAisthecorrectchoice.
5.TheclassAssertionErrorhas"is-a"relationshipwiththeseclasses(choosetwo)
类AssertionError跟下列的哪种类是一种“is–a(是一个)”的关系:
A.RuntimeException
B.Error
C.VirtualMachineError
D.IllegalAccessException
E.Throwable
BandEarecorrect.TheclassAssertionErrorisanError,whichdenotesan“incorrectcondition”asopposedtoan“unusualcondition”(Exception).Since,theclassErrordescendsfromThrowable,AssertionErroralsohas“is-a”relationshipwithThrowable.Hereisthehierarchy?
Cjava.lang.Object
|
+-java.lang.Throwable
|
+-java.lang.Error
|
+-java.lang.AssertionError
Wanttoknowmore?
Youcanfindmoreinformationaboutthisasananswertothequestion-“WhyisAssertionErrorasubclassofErrorratherthanRuntimeException?
”at-#design-faq-error
6.Whatwillbetheresultofexecutingthefollowingcode?
运行下列代码的结果是:
1.booleana=true;
2.booleanb=false;
3.booleanc=true;
4.if(a==true)
5.if(b==true)
6.if(c==true)System.out.println("Somethingsaretrueinthisworld");
7.elseSystem.out.println("Nothingistrueinthisworld!
");
8.elseif(a&&(b=c))//这里是赋值,不是比较
System.out.println("It´stooconfusingtotellwhatistrueandwhatisfalse");
9.elseSystem.out.println("Heythiswon´tcompile");
A.Thecodewon’tcompile.
B.“somethingsaretrueinthisworld”willbeprinted
C.“heythiswon’tcompile”willbeprinted
D.Noneofthese
Discorrect.Thisisaverygoodquestiontotesttheconceptsofexecutionflowincaseofifconditions.Theruleforattachingelsestatementswithifconditionsisthesameasattachingclosebracketswithopenbrackets.Aclosebracketattacheswiththeclosestopenbracket,whichisnotalreadyclosed.Similarlyanelsestatementattacheswiththeclosestifstatement,whichdoesn´thaveanelsestatementalready,attachedtoit.Sotheelsestatementatline7attachestotheifstatementatline6.Theelsestatementatline8attachestotheifstatementatline5.Theelsestatementatline9attachestotheifstatementatline8.
Nowlet´slookattheexecution.Atline4sinceaisequaltotruetheexecutionfallstoline5.Atline5sincebisnottruetheexecutiongoestothecorrespondingelsestatementatline8.Nowitevaluatestheconditioninsidetheifstatement.Pleasenoteherethatanassignmentstatementalsohasavalueequaltothevaluebein
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 认证 模拟 试题 程序