问题描述
QuestionNo:1Given:publicclassTestAmigo{publicstaticvoidmain(String[]args){StringBuffera=newStringBuffer("A");StringBufferb=newStringBuffer("B");operate(a,b);System.out.println(a+","+b);}staticvoidoperate(StringBufferx,StringBuffery){x.append(y);y=x;}}Whatistheresult?A.Thecodecompilesandprints“A,B”.B.Thecodecompilesandprints“A,A”.C.Thecodecompilesandprints“B,B”.D.Thecodecompilesandprints“AB,B”.E.Thecodecompilesandprints“AB,AB”.F.Thecodedoesnotcompilebecause“+”cannotbeoverloadedforStringBufferQuestionNo:2Given:publicclassTestAmigo{publicstaticvoidstringReplace(Stringtext){text=text.replace('j','i');}publicstaticvoidbufferReplace(StringBuffertext){text=text.append("C");}publicstaticvoidmain(Stringargs[]){StringtextString=newString("java");StringBuffertextBuffer=newStringBuffer("java");stringReplace(textString);bufferReplace(textBuffer);System.out.println(textString+textBuffer);}}Whatistheoutput?Ajavajava Biavajava CiavaJavaC DjavajavaCENoneoftheseQuestionNo:3Given:publicclassTestAmigo{publicstaticvoidadd3(Integeri){intval=i.intValue();val+=3;i=newInteger(val);}publicstaticvoidmain(Stringargs[]){Integeri=newInteger(0);add3(i);System.out.println(i.intValue());}}Whatistheresult?A.Compilationwillfail.B.Theprogramprints“3”.C.Compilationwillsucceedbutanexceptionwillbethrownatline3.D.Theprogramprints“0”.QuestionNo:4publicclassTestAmigo{static{intx=5;}staticintx,y;publicstaticvoidmain(Stringargs[]){x--;myMethod();System.out.println(x+y+++x);}publicstaticvoidmyMethod(){y=x+++++x;}}A.compiletimeerrorB.prints:1C.prints:2D.prints:3E.prints:7F.prints:8QuestionNo:5interfaceMyInterface{}publicclassMyInstanceTestimplementsMyInterface{staticStrings;publicstaticvoidmain(Stringargs[]){MyInstanceTestt=newMyInstanceTest();if(tinstanceofMyInterface){System.out.println("Iamtrueinterface");}else{System.out.println("Iamfalseinterface");}if(sinstanceofString){System.out.println("IamtrueString");}else{System.out.println("IamfalseString");}}}A.compiletimeerrorB.runtimeerrorC.prints:“Iamtrueinterface”followedby“IamtrueString”D.prints:“Iamtrueinterface”followedby“IamfalseString”E.prints:“Iamfalseinterface”followedby“IamfalseString”F.prints:“Iamfalseinterface”followedby“IamtrueString”QuestionNo:6Whatresultsfromattemptingtocompileandrunthefollowingcode?publicclassTernary{publicstaticvoidmain(Stringargs[]){inta=5;System.out.println("Valueis-"+((a<5)?9.0:9));}}A.print:Valueis-9B.print:Valueis-5C.CompilationerrorD.NoneoftheseQuestionNo:7Whatisdisplayedwhenthefollowingisexecuted?classParent{privatevoidmethod1(){System.out.println("Parent'smethod1()");}publicvoidmethod2(){System.out.println("Parent'smethod2()");method1();}}classChildextendsParent{publicvoidmethod1(){System.out.println("Child'smethod1()");}publicstaticvoidmain(Stringargs[]){Parentp=newChild();p.method2();}}A.compiletimeerrorB.runtimeerrorC.prints:parent’smethod2()child’smethod1()D.prints:parent’smethod2()parent’smethod1()QuestionNo:8Whichofthefollowinglineswillprintfalse?1.publicclassMyClass2.{3.staticStrings1="Iamunique!";4.publicstaticvoidmain(Stringargs[])5.{6.Strings2="Iamunique!";7.Strings3=newString(s1);8.System.out.println(s1==s2);9.System.out.println(s1.equals(s2));10.System.out.println(s3==s1);11.System.out.println(s3.equals(s1));12.System.out.println(TestClass.s4==s1);13.}14.}15.16.classTestClass17.{18.staticStrings4="Iamunique!";19.}A.line10and12B.line12onlyC.line8and10D.noneoftheseQuestionNo:9Whatwillbetheoutputoncompiling/runningthefollowingcode?publicclassMyThreadimplementsRunnable{StringmyString="Yes";publicvoidrun(){this.myString="No";}publicstaticvoidmain(String[]args){MyThreadt=newMyThread();newThread(t).start();for(inti=0;i<10;i++)System.out.print(t.myString);}}A.compileerrorB.prints:yesyesyesyesyesyesandsoonC.prints:nonononononononoandsoonD.theoutputcannotbedeterminatedE.prints:yesnoyesnoyenoyenoandsoonQuestionNo:10Whatwillhappenwhenyouattempttocompileandrunthefollowingcode?classBase{inti=99;publicvoidamethod(){System.out.println("Base.amethod()");}Base(){amethod();}}publicclassDerivedextendsBase{inti=-1;publicstaticvoidmain(Stringargv[]){Baseb=newDerived();System.out.println(b.i);b.amethod();}publicvoidamethod(){System.out.println("Derived.amethod()");}}A.Derived.amethod()-1Derived.amethod()B.99C.99Derived.amethod()D.Derived.amethod()99Derived.amethod()E.compiletimeerror.