SCJP模拟考试一及答案.docx

上传人:b****5 文档编号:6649520 上传时间:2023-01-08 格式:DOCX 页数:26 大小:25.84KB
下载 相关 举报
SCJP模拟考试一及答案.docx_第1页
第1页 / 共26页
SCJP模拟考试一及答案.docx_第2页
第2页 / 共26页
SCJP模拟考试一及答案.docx_第3页
第3页 / 共26页
SCJP模拟考试一及答案.docx_第4页
第4页 / 共26页
SCJP模拟考试一及答案.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

SCJP模拟考试一及答案.docx

《SCJP模拟考试一及答案.docx》由会员分享,可在线阅读,更多相关《SCJP模拟考试一及答案.docx(26页珍藏版)》请在冰豆网上搜索。

SCJP模拟考试一及答案.docx

SCJP模拟考试一及答案

SCJP模拟考试一及答案

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)

A.RuntimeException

B.Error

C.VirtualMachineError

D.IllegalAccessException

E.Throwable

BandEarecorrect.TheclassAssertionErrorisanError,whichdenotesan“incorrectcondition”asopposedtoan“unusualcondition”(Exception).Since,theclassErrordescendsfromThrowable,AssertionErroralsohas“is-a”relationshipwithThrowable.Hereisthehierarchy–java.lang.Object

|

+-java.lang.Throwable

|

+-java.lang.ErrorException

||

+-java.lang.AssertionError+RuntimeExceptionIOException

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.Pleasenoteherethatanassignmentstatementalsohasavalueequaltothevaluebeingassigned,hence(b=c)evaluatestotrueandsubsequentlya&&(b=c)evaluatestotrueand"It'stooconfusingtotellwhatistrueandwhatisfalse"willbeprinted.HencethecorrectanswerischoiceD.

7.Whatwillhappenwhenyouattempttocompileandrunthefollowingco

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1