JAVA认证历年真题:SCJP考试真题和解析例题1:Choosethethreevalididentifiersfromthoselistedbelow.A.IDoLikeTheLongNameClassB.$byteC.constD._okE.3_case解答:A,B,D点评:Java中的标示符必须是字母、美元符($)或下划线(_)开头。关键字与保留字不能作为标示符。选项C中的const是Java的保留字,所以不能作标示符。选项E中的3_case以数字开头,违反了Java的规则。例题2:Howcanyouforcegarbagecollectionofanobject?A.GarbagecollectioncannotbeforcedB.CallSystem.gc().C.CallSystem.gc(),passinginareferencetotheobjecttobegarbagecollected.D.CallRuntime.gc().E.Setallreferencestotheobjecttonewvalues(null,forexample).解答:A点评:在Java中垃圾收集是不能被强迫立即执行的。调用System.gc()或Runtime.gc()静态方法不能保证垃圾收集器的立即执行,因为,也许存在着更高优先级的线程。所以选项B、D不正确。选项C的错误在于,System.gc()方法是不接受参数的。选项E中的方法可以使对象在下次垃圾收集器运行时被收集。例题3:Considerthefollowingclass:1.classTest(inti){2.voidtest(inti){3.System.out.println(“Iamanint.”);4.}5.voidtest(Strings){6.System.out.println(“Iamastring.”);7.}8.9.publicstaticvoidmain(Stringargs){10.Testt=newTest();11.charch=“y”;12.t.test(ch);13.}14.}Whichofthestatementsbelowistrue?(Chooseone.)A.Line5willnotcompile,becausevoidmethodscannotbeoverridden.B.Line12willnotcompile,becausethereisnoversionoftest()thatrakesacharargument.C.Thecodewillcompilebutwillthrowanexceptionatline12.D.Thecodewillcompileandproducethefollowingoutput:Iamanint.E.Thecodewillcompileandproducethefollowingoutput:IamaString.解答:D点评:在第12行,16位长的char型变量ch在编译时会自动转化为一个32位长的int型,并在运行时传给voidtest(inti)方法。QuestionNo:1Given:1.publicclasstest(2.publicstaticvoidmain(Stringargs){3.inti=0xFFFFFFF1;4.intj=~i;5.6.}7.)Whatisthedecimalvalueofjatline5?A.0B.1C.14D.–15E.Anerroratline3causescompilationtofail.F.Anerroratline4causescompilationtofail.Answer:DQuestionNo:2Given:Integeri=newInteger(42);Long1=newLong(42);Doubled=newDouble(42.0);WhichtwoexpressionsevaluatetoTrue?(ChooseTwo)A.(i==1)B.(i==d)C.(d==1)D.(i.equals(d))E.(d.equals(i))F.(i.equals(42))Answer:D,EQuestionNo:3Exhibit:1.publicclasstest(2.privatestaticintj=0;3.4.privatestaticbooleanmethodB(intk)(5.j+=k;6.returntrue;6.)7.8.publicstaticvoidmethodA(inti){9.booleanb:10.b=i10|methodB(4);11.b=i10||methodB(8);12.)13.14.publicstaticvoidmain(Stringargs}(15.methodA(0);16.system.out.printIn(j);17.)18.)Whatistheresult?A.Theprogramprints“0”B.Theprogramprints“4”C.Theprogramprints“8”D.Theprogramprints“12”E.Thecodedoesnotcomplete.Answer:BQuestionNo:4Given1.Publicclasstest(2.Publicstaticvoidmain(Stringargs)(3.System.out.printIn(6^3);4.)5.)Whatistheoutput?Answer:5QuestionNo:5Given:1.publicclassFoo{2.publicstaticvoidmain(Stringargs){3.StringBuffera=newStringBuffer(“A”);4.StringBufferb=newStringBuffer(“B”);5.operate(a,b);6.system.out.printIn{a+“,”+b};7.)8.staticvoidoperate(StringBufferx,StringBuffery){9.x.append{y};10.y=x;11.)12.}Whatistheresult?A.Thecodecompilesandprints“A,B”.B.Thecodecompilesandprints“A,A”.C.Thecodecompilesandprints“B,B”.D.Thecodecompilesandprints“AB,B”.E.Thecodecompilesandprints“AB,AB”.F.Thecodedoesnotcompilebecause“+”cannotbeoverloadedforStringBuffer.Answer:D|||QuestionNo:6Exhibit:1.Publicclasstest(2.PublicstaticvoidstringReplace(Stringtext)(3.Text=text.replace(‘j‘,‘i‘);4.)5.6.publicstaticvoidbufferReplace(StringBuffertext)(7.text=text.append(“C”)8.)9.10.publicstaticvoidmain(Stringargs}(11.StringtextString=newString(“java”);12.StringBuffertextBufferString=newStringBuffer(“java”);13.14.stringReplace(textString);15.BufferReplace(textBuffer);16.17.System.out.printIn(textString+textBuffer);18.}19.)Whatistheoutput?Answer:javajavaCQuestionNo:7Exhibit:1.publicclasstest{2.publicstaticvoidadd3(Integeri)}3.intval=i.intValue();4.val+=3;5.i=newInteger(val);6.}7.8.publicstaticvoidmain(Stringargs[]){9.Integeri=newInteger(0);10.add3(i);11.system.out.printIn(i.intValue());12.}13.)Whatistheresult?A.Compilationwillfail.B.Theprogramprints“0”.C.Theprogramprints“3”.D.Compilationwillsucceedbutanexceptionwillbethrownatline3.Answer:BQuestionNo:8Given:1.publicclassConstOver{2.publicConstOver(intx,inty,intz){3.}4.}WhichtwooverloadtheConstOverconstructor?(ChooseTwo)A.ConstOver(){}B.ProtectedintConstOver(){}C.PrivateConstOver(intz,inty,bytex){}D.PublicObjectConstOver(intx,inty,intz){}E.PublicvoidConstOver(bytex,bytey,bytez){}Answer:A,CQuestionNo:9Given:1.publicclassMethodOver{2.publicvoidsetVar(inta,intb,floatc){3.}4.}WhichtwooverloadthesetVarmethod?(ChooseTwo)A.PrivatevoidsetVar(inta,floatc,intb){}B.ProtectedvoidsetVar(inta,intb,floatc){}C.PublicintsetVar(inta,floatc,intb)(returna;)D.PublicintsetVar(inta,intb,floatc)(returna;)E.ProtectedfloatsetVar(inta,intb,floatc)(returnc;)Answer:A,CQuestionNo:10Given:1.classBaseClass{2.Privatefloatx=1.0f;3.protectedfloatgetVar()(returnx;)4.}5.classSubclassextendsBaseClass(6.privatefloatx=2.0f;7.//insertcodehere8.)Whichtwoarevalidexamplesofmethodoverriding?(ChooseTwo)A.FloatgetVar(){returnx;}B.PublicfloatgetVar(){returnx;}C.FloatdoublegetVar(){returnx;}D.PublicfloatgetVar(){returnx;}E.PublicfloatgetVar(floatf){returnf;}Answer:B,DQuestionNo:11Whichtwodemonstratean“isa”relationship?(ChooseTwo)A.publicinterfacePerson{}publicclassEmployeeextendsPerson{}B.publicinterfaceShape{}publicclassEmployeeextendsShape{}C.publicinterfaceColor{}publicclassEmployeeextend