JAVA面向对象基础测试题提示:本题为第一阶段,JAVA面向对象基础部分练习题,包括对象,类,继承,封装,多态,接口,内部类等等,java核心基础,适合初学者对面向对象基础的知识进行测试,以便查漏补缺。1.程序执行的结果是:()。01publicclassPoint{02inty=7;03publicvoidstep(inty){04y+=y;05System.out.println(y);06}07publicstaticvoidmain(String[]args){08Pointp=newPoint();09p.step(10);10}11}A.14B.20C.10D.17正确答案:B解析:2.程序的执行结果是:()。01publicclassQuestion{02privateintnum;03publicstaticvoidmain(String[]args){04Questionq=newQuestion();05q.num=13;06update(q);07System.out.println(q.num);08}09publicstaticvoidupdate(Questionq){10q.num=9;11}12}A.13B.9C.0D.4正确答案:B解析:3.程序执行的结果是:()。01publicclassAnswer{02publicstaticvoidmain(String[]args){03intscore=20;04Answerans=newAnswer();05ans.add(score);06System.out.println(main:score=+score);07}08voidadd(intscore){09System.out.println(add:score=+score++);10}11}A.1add:score=212main:score=21B.1add:score=202main:score=21C.1add:score=212main:score=20D.1add:score=202main:score=20正确答案:D解析:4.下列代码编译或运行的结果是:()。01publicclassSnow{02publicStringjump(intx,inty){03returnjumpone;04}05publicStringjump(int[]vals){06returnjumptwo;07}08publicstaticvoidmain(String[]args){09Snows=newSnow();10System.out.println(s.jump(4,5));11}12}A.publicStringjump(int[]vals){行,编译出错B.System.out.println(s.jump(4,5));行,抛出运行时异常C.输出:jumponeD.输出:jumptwo正确答案:C解析:5.关于下列代码说法正确的是:()。01publicclassStorm{02publicvoidfind(){03}04publicStringfind(){05returnfind;06}07publicdoublefind(intx){08return2.0;09}10}A.无编译错误B.代码publicStringfind(){行,出现编译错误C.代码publicdoublefind(intx){行,出现编译错误D.代码returnfind;行处出现编译错误正确答案:B解析:6.请看下列代码:01classClassA{}02classClassBextendsClassA{}03classClassCextendsClassA{}04publicclassTest{05publicstaticvoidmain(String[]args){06ClassAp0=newClassA();07ClassBp1=newClassB();08ClassCp2=newClassC();09ClassAp3=newClassB();10ClassAp4=newClassC();11插入代码12}13}下列选项中放置在插入代码处,使程序编译正确的是:()。A.p0=p1;B.p1=p2;C.p2=p4;D.p2=(ClassC)p1;正确答案:A解析:7.下列代码的运行结果是:()。01publicclassAnimal{02publicStringnoise(){03returnAnimal;04}05publicstaticvoidmain(String[]args){06Catcat=null;07Animalanimal=newDog();08if(animalinstanceofCat){09cat=(Cat)animal;10System.out.println(cat.noise());11}else{12System.out.println(animalisnotCat'sinstance);13}14}15}16classDogextendsAnimal{17publicStringnoise(){18returnDog;19}20}21classCatextendsAnimal{22publicStringnoise(){23returnCat;24}25}A.AnimalB.DogC.CatD.animalisnotCat'sinstance正确答案:D解析:8.请看下列代码编译和运行的结果是:()。1publicclassTeacher{2privateStringname=sun;3publicstaticvoidmain(String[]args){4Teacher[]teachers=newTeacher[2];5System.out.println(teachers[0].name);6System.out.println(teachers.length);7}8}A.sun2B.null2C.null1D.运行时抛出NullPointerException异常正确答案:D解析:9.下列代码编译和运行的结果是:()。01classPerson{02Stringname=;0304publicPerson(Stringname){05this.name=name;06}07}0809classEmployeeextendsPerson{10StringempNO=0000;1112publicEmployee(StringempNO){13this.empNO=empNO;14}15}1617publicclassEmployeeTest{18publicstaticvoidmain(String[]args){19Employeee=newEmployee(1109);20System.out.println(e.empNO);21}22}A.输出:0000B.输出:1109C.代码publicEmployee(StringempNO){行,出现编译错误D.抛出运行时异常正确答案:C解析:10.下列代码编译和运行的结果是:()。01classA{02publicvoidstart(){03System.out.println(AStart);04}05}06publicclassBextendsA{07publicvoidstart(){08System.out.println(BStart);09}10publicstaticvoidmain(String[]args){11((A)newB()).start();12}13}A.输出:AStartB.输出:BStartC.输出:AStartBStartD.编译错误正确答案:B解析:11.关于下列代码说法正确的是:()。01publicinterfaceA{02publicvoiddoSomething(Stringthing);03}04interfaceB{05}06interfaceCextendsA,B{07}08interfaceD{09publicvoiddoIt(Stringthing);10}11classAImplimplementsC,D{12publicvoiddoSomething(Stringmsg){}13publicvoiddoIt(Stringthing){}14}A.所有类和接口都编译成功B.接口A编译失败C.类AImpl编译失败D.接口C编译失败正确答案:A解析:12.在Java中,关于final关键字的说法正确的是:()。A.如果修饰局部变量,必须定义时初始化B.如果修饰类,则该类只能被一个子类继承C.如果修饰方法,则该方法不能在子类中被覆盖D.如果修饰方法,则该方法所在的类不能被继承正确答案:C解析:13.关于下列代码说法正确的是:()。01publicclassCell{02privatefinalintid;03publicCell(intid){04this.id=id;05}06publicvoidupdateId(intid){07this.id=id;08}09publicstaticvoidmain(String[]args){10Cellcell=newCell(1001);11cell.updateId(1002);12System.out.println(cell.id);13}14}A.编译错误B.运行时异常抛出C.运行后,cell对象属性id的值没有改变,仍然是1001D.运行后,cell对象属性id的值改变成新的值1002正确答案:A解析:14.下列选项中,不属于Java的访问控制修饰符的是:()。A.privateB.protectedC.friendlyD.public正确答案:C解析:15.下面关于import,class和package的声明顺序正确的是:()。A.package,import,classB.class,import,packageC.import,package,classD.package,class,import正确答案:A解析:16.有一个名为Product的类,下列方法中属于该类的构造方法的是:()。A.publicProduct(){}B.publicProduct(Stringname){}C.publicvoidProduct(){}D.publicvoidProduct(Stringname){}正确答案:AB解析:17.请看下列代码:1publicclassTetromino{2protectedintmove(intx){3return0;4}5}6classTextendsTetromino{7插入代码8}在插入代码处填入选项中的代码,使T类没有编译错误的是:()。A.publicintmove(intx){return0;}B.privateintmove(intx){return0;}C.privateintmove(longx){return0;}D.protectedlongmove(intx){return0;}正确答案:AC解析:18.下列关于JVM的内存结构描述正确的是:()。A.类的各种信息在堆中保存B.栈用于存放程序运行过程当中所有的局部变量C.堆一般用于存储使用new关键字创建的对象D.类是JVM的内存结构正确答案:BC解析:19.请看下列代码:1publicclassFoo{2staticvoidalpha(){/*morecodehere*/}3voidbeta(){/*morecodehere*/}4}下列说法正确的是:()。A.Foo.beta()是调用beta方法的正确方式B.Foo.alpha()是调用alpha方法的正确方式C.beta方法可以直接调用alpha方法D.alpha方法可以直接调用beta方法正确答案:BC解析: