您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 招聘面试 > JAVA基础面试题-2-答案版
JAVA语言基础笔试题-2Question1Given:11.classA{12.publicvoidprocess(){System.out.print(“A“)}}13.classBextendsA{14.publicvoidprocess()throwsRuntimeException{15.super.process();16.if(true)thrownewRuntimeException();17.System.out.print(“B”)}}18.publicstaticvoidmain(String[]args){19.try{((A)newB()).process();}20.catch(Exceptione){System.out.print(“Exception“)}21.}Whatistheresult?A.ExceptionB.AExceptionC.AExceptionBD.ABExceptionE.Compilationfailsbecauseofanerrorinline14.F.Compilationfailsbecauseofanerrorinline19.答案:B考点:方法的重写(重写方法异常抛出部分的理解)多态异常处理说明:子类重写父类方法,不能抛出比父类方法更多的异常,但此处子类重写方法声明抛出了RuntimeException,不算多抛,算是平抛,是可以的。RuntimeException是Exception的子类,可以被Exception捕获。Question2Given:11.staticclassA{12.voidprocess()throwsException{thrownewException();}13.}14.staticclassBextendsA{15.voidprocess(){System.out.println(“B”)}16.}17.publicstaticvoidmain(String[]args){18.Aa=newB();19.a.process();20.}Whatistheresult?A.BB.Thecoderunswithnooutput.C.Anexceptionisthrownatruntime.D.Compilationfailsbecauseofanerrorinline15.E.Compilationfailsbecauseofanerrorinline18.F.Compilationfailsbecauseofanerrorinline19.答案:F考点:方法的重写(重写方法异常抛出部分的理解)多态静态内部类以及其实例的创建说明:19.a.process();是多态调用,调用的应该是类B的process方法,这个方法只是允许抛出RuntimeException,所以19行在理论上不需要进行异常相关处理,系统会自动抛出该异常,但是多态只是在运行时,系统方能识别,在编译的时候,系统还是按照类A的process方法来进行验证,所以出现错误,因为类A抛出的是检查异常,必须显式被捕获或者抛出。Question3Given:11.staticclassA{12.voidprocess()throwsException{thrownewException();}13.}14.staticclassBextendsA{15.voidprocess(){System.out.println(“B”);16.}17.publicstaticvoidmain(String[]args){18.newB().process();19.}Whatistheresult?A.BB.Thecoderunswithnooutput.C.Compilationfailsbecauseofanerrorinline12.D.Compilationfailsbecauseofanerrorinline15.E.Compilationfailsbecauseofanerrorinline18.答案:A考点:方法的重写(重写方法异常抛出部分的理解)静态内部类以及其实例的创建Question4Given:84.try{85.ResourceConnectioncon=resourceFactory.getConnection();86.Resultsr=con.query(“GETINFOFROMCUSTOMER”)87.info=r.getData();88.con.close();89.}catch(ResourceExceptionre){90.errorLog.write(re.getMessage());91.}92.returninfo;WhichistrueifaResourceExceptionisthrownonline86?A.Line92willnotexecute.B.Theconnectionwillnotberetrievedinline85.C.Theresourceconnectionwillnotbeclosedonline88.D.Theenclosingmethodwillthrowanexceptiontoitscaller.答案:C考点:try..catch结构的理解异常的捕获和处理说明:由于抛出的ResourceException,能够被catch块捕获(标配),所以异常在方法中就被处理掉了,不会抛出该方法。根据try块操作上规定,一旦抛出异常,抛出异常语句后续部分的try块语句将不再运行。Question5ClicktheExhibitbutton.1.publicclassA{2.publicvoidmethod1(){3.Bb=newB();4.b.method2();5.//morecodehere6.}7.}1.publicclassB{2.publicvoidmethod2(){3.Cc=newC();4.c.method3();5.//morecodehere6.}7.}1.publicclassC{2.publicvoidmethod3(){3.//morecodehere4.}5.}Given:25.try{26.Aa=newA();27.a.method1();28.}catch(Exceptione){29.System.out.print(“anerroroccurred!”)30.}WhichtwoaretrueifaNullPointerExceptionisthrownonline3ofclassC?(Choosetwo.)A.Theapplicationwillcrash.B.Thecodeonline29willbeexecuted.C.Thecodeonline5ofclassAwillexecute.D.Thecodeonline5ofclassBwillexecute.E.Theexceptionwillbepropagatedbacktoline27.答案:BE考点:异常的抛出、捕获和处理说明:一般来说,异常的抛出,最终终将被处理,本方法不处理,则抛给主调方法,关键是要能抛的出去,因为NullpointerException是RuntimeException,所以一路没有阻碍,最终propagatedback到达27行,Exception是所有异常的父类,所以该异常在这里被处理了,Question6ClicktheExhibitbutton.1.publicclassA{2.publicvoidmethod1(){3.try{4.Bb=newB();5.b.method2();6.//morecodehere7.}catch(TestExceptionte){8.thrownewRuntimeException(te);9.}6.}7.}1.publicclassB{2.publicvoidmethod2()throwsTestException{3.//morecodehere4.}5.}1.publicclassTestExceptionextendsException{2.}Given:31.publicvoidmethod(){32.Aa=newA();33.a.method1();34.}WhichistrueifaTestExceptionisthrownonline3ofclassB?A.Line33mustbecalledwithinatryblock.B.Theexceptionthrownbymethod1inclassAisnotrequiredtobecaught.C.Themethoddeclaredonline31mustbedeclaredtothrowaRuntimeException.D.Online5ofclassA,thecalltomethod2ofclassBdoesnotneedtobeplacedinatry/catchblock.答案:B考点:异常抛出、捕获和处理说明:第8行,把检查异常TestException封装在RuntimeException中抛出是可以,因为系统仍然认为你抛出的是一个运行时异常,任何方法都默认支持抛出运行时异常。Question7Given:11.publicstaticvoidmain(String[]args){12.try{13.args=null;14.args[0]=”test”;15.System.out.println(args[0]);16.}catch(Exceptionex){17.System.out.println(“Exception”)18.}catch(NullPointerExceptionnpe){19.System.out.println(“NullPointerException”)20.}21.}Whatistheresult?A.testB.ExceptionC.Compilationfails.D.NullPointerException答案:C考点:异常的捕获处理说明:Exception是能够捕获所有异常的”万金油”,其一般放在捕获块的最后一环,用来捕获所有的漏网之鱼,如果放在捕获块最前头,将导致后头的捕获块永远无法得到运行,最后产生编译失败。Question8Given:11.staticvoidtest()throwsError{12.if(true)thrownewAssertionError();13.System.out.print(“test“)14.}15.publicstaticvoidmain(String[]args){16.try{test();}17.catch(Exceptionex){System.out.print(“exception“);18.System.out.print(“end”)19.}Whatistheresult?A.endB.Compilationfails.C.exceptionendD.exceptiontestendE.AThrowableisthrownbymain.F.AnExceptionisthrownbymain.答案:E考点:Error和Exception的关系的理解说明:Error和Exception是兄弟类,其有一个共同的父类Throwable,所以Error也可以被抛出。Exception捕获块无法捕获Error,所以这里Error会被抛出,由JVM做了最终处理。Question9Given:11.staticvoidtest(){12.try{13.Stringx=null;14.System.out.print(x.toString()+””);15.}16.finally{System.out.print(“finally”);17.}18.publicstaticvoidma
本文标题:JAVA基础面试题-2-答案版
链接地址:https://www.777doc.com/doc-2878603 .html