1.Given:1.publicclassreturnIt{2.returnTypemethodA(bytex,doubley){3.return(short)x/y*2;4.}5.}WhatisthevalidreturnTypeformethodAinline2?A.intB.byteC.longD.shortE.floatF.doubleAnswerF注释:short类型的x,除以double类型的y,再乘int的2,所以结果是double类型的。注意第三行的强制转换,只是转换了x。2.1)classSuper{2)publicfloatgetNum(){return3.0f;}3)}4)5)publicclassSubextendsSuper{6)7)}whichmethod,placedatline6,willcauseacompilererror?A.publicfloatgetNum(){return4.0f;}B.publicvoidgetNum(){}C.publicvoidgetNum(doubled){}D.publicdoublegetNum(floatd){return4.0d;}Answer:B注意这道题主要考的是方法的overload和override。对于overload,只有参数列表不同,才做为标准,而返回值和访问控制关键字不能做为标准,所以B错在方法名相同,但只有返回值不同,这是错的。C和D是正确的overload。对于override,则访问控制关键字只能更加公有化,异常只能是超类方法抛出的异常的子类,也可以不抛出。返回类型,参数列表必须精确匹配。所以A是正确的override。3.1)publicclassFoo{2)publicstaticvoidmain(Stringargs[]){3)try{return;}4)finally{System.out.println(Finally);}5)}6)}whatistheresult?A.Theprogramrunsandprintsnothing.B.Theprogramrunsandprints“Finally”.C.Thecodecompiles,butanexceptionisthrownatruntime.D.Thecodewillnotcompilebecausethecatchblockismissing.Answer:btry......catch......finally的问题。程序中如果遇到return,则finally块先被执行,然后再执行retrun,而finally块后面的语句将不被执行。如果遇到System.exit(1),则finally块及其后的语句都不执行,整个程序退出,还执行什么呀。4.1)publicclassTest{2)publicstaticStringoutput=;3)publicstaticvoidfoo(inti){4)try{5)if(i==1){6)thrownewException();7)}8)output+=1;9)}10)catch(Exceptione){11)output+=2;12)return;13)}14)finally{15)output+=3;16)}17)output+=4;18)}19)publicstaticvoidmain(Stringargs[]){20)foo(0);21)foo(1);22)23)}24)}whatisthevalueofoutputatline22?Asnwer:13423执行第一个foo(0)时,执行第8条语句,output=1,然后执行语句15,output=13,然后是17条,output=134,因为是static类型的变量,所以任何对其值的修改都有效。执行第二条foo(1),先执行语句5,结果抛出异常,转到catch块,output=1342,finally任何情况下都执行,所以output=13423,然后return跳出方法体,所以output=134235.1)publicclassIfElse{2)publicstaticvoidmain(Stringargs[]){3)if(odd(5))4)System.out.println(odd);5)else6)System.out.println(even);7)}8)publicstaticintodd(intx){returnx%2;}9)}whatisoutput?Answer:编译错误。if中的判断条件的结果必须是boolean类型的。注意这里说的是结果.6.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@1)classExceptionTest{2)publicstaticvoidmain(Stringargs[]){3)try{4)methodA();5)}catch(IOExceptione){6)System.out.println(caughtIOException);7)}catch(Exceptione){8)System.out.println(caughtException);9)}10)}11)}IfmethodA()throwsaIOException,whatistheresult?Answer:caughtIOException如果methodA()抛出IOExecption,被语句6捕获,输出caughtIOException,然后呢??然后就结束了呗。7.1)inti=1,j=10;2)do{3)if(i++--j)continue;4)}while(i5);AfterExecution,whatarethevalueforiandj?A.i=6j=5B.i=5j=5C.i=6j=4D.i=5j=6E.i=6j=6Answer:d程序一直循环,直到i=4,j=6时,执行完语句3后,i会++,这时i就等于了5,continue后就不能再循环了,所以选D。8.1)publicclassX{2)publicObjectm(){3)Objecto=newFloat(3.14F);4)Object[]oa=newObject[1];5)oa[0]=o;6)o=null;7)oa[0]=null;8)System.out.println(oa[0]);9)}10)}whichlineistheearliestpointtheobjectareferedisdefinitelyelibiletobegarbagecollectioned?A.Afterline4B.Afterline5C.Afterline6D.Afterline7E.Afterline9(thatis,asthemethodreturns)Answer:d当执行第6行后,仍然有对象指向o,所以o不能满足条件,当第7条语句被执行后,就再也没有对象指向o了,所以选D。9.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@1)interfaceFoo{2)intk=0;3)}4)publicclassTestimplementsFoo{5)publicstaticvoidmain(Stringargs[]){6)inti;7)Testtest=newTest();8)i=test.k;9)i=Test.k;10)i=Foo.k;11)}12)}Whatistheresult?A.Compilationsucceeds.B.Anerroratline2causescompilationtofail.C.Anerroratline9causescompilationtofail.D.Anerroratline10causescompilationtofail.E.Anerroratline11causescompilationtofail.Answer:A编译通过,通过测试的10.whatisreserved(保留)wordsinjava?A.runB.defaultC.implementD.importAnswer:b,D11.1)publicclassTest{2)publicstaticvoidmain(String[]args){3)Stringfoo=args[1];4)Sringbar=args[2];5)Stringbaz=args[3];6)}7)}javaTestRedGreenBluewhatisthevalueofbaz?A.bazhasvalueofB.bazhasvalueofnullC.bazhasvalueofRedD.bazhasvalueofBlueE.bazhasvalueofGreenF.thecodedoesnotcompileG.theprogramthrowanexceptionAnswer:G当执行javaTestRedGreenBlue时,数组args只有[0][1][2],运行时ArrayIndexOutOfBoundsException这个异常会被抛出,数组越界。12.intindex=1;intfoo[]=newint[3];intbar=foo[index];intbaz=bar+index;whatistheresult?A.bazhasavalueof0B.bazhasvalueof1C.bazhasvalueof2D.anexceptionisthrownE.thecodewillnotcompileAnswer:b数组初始化后默认值是0,所以baz=0+1=113.whichthreearevaliddeclaraction(行为)ofafloat?A.floatfoo=-1;B.floatfoo=1.0;C.floatfoo=42e1;D.floatfoo=2.02f;E.floatfoo=3.03d;F.floatfoo=0x0123;Answer:A,D,F其它的系统都会认为是double类型,所以出错。说一下A和C的区别吧,-1系统会认为是一个int类型,把int类型再赋给float类型的foo,当然没错了,可C就不同啦,42e1是int类型吗??14.1)publicclassFoo{2)publicstaticvoidmain(Stringargs[]){3)Strings;4)System.out.println(s=+s);5)}6)}whatistheresult?A.Thecodecompilesand“s=”isprinted.B.Thecodecompilesand“s=null”isprinted.C.Thecodedoesnotcompilebecausestringsisnotinitialized(初始化).D.Thecodedoesnotcompilebecausestringscannotbereferenced(引用).E.Thecodecompiles,butaNullPointerExceptionisthrownwhentoStringiscalled.Answer:C只有实例变量系统才给予自动赋默认值的这种待遇15.1)publicclassTest{2)publicstaticvoidmain(Stringargs[]){3)inti=oxFFFFFFF1;4)intj=~i;5)6)}7)}whichisdecimalvalueofjatline5?A.0B.1C.14D.-15E.compileerroratline3F.compileerroratline4Answer:C算一算就知道了。16.floatf=4.2F;Floatg=newFloat(4.2F);Doubled=newDouble(4.2);Whicharetrue?A.f==gB.g==gC.d==fD.d.equals(f)