您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 资产评估/会计 > java面向对象考试题
第一题:publicstaticvoidmain(String[]args){String[]fileNames={abc.txt,bcd.exe,cde.exe,def.dat,efg.exe};for(StringfileName:fileNames){if(fileName.endsWith(.exe)){System.out.print(fileName.substring(0,fileName.lastIndexOf(.exe))+);}}}fileName.endsWith(.exe)判断是否已.exe结尾substring(0,4)截取0-3的字符串,不包含4lastIndexOf(.exe)获取.exe最后一次出现的位置答案:bcdcdeefg第二题:publicstaticvoidgo(Longn){System.out.println(Long);}publicstaticvoidgo(Shortn){System.out.println(Short);}publicstaticvoidgo(intn){System.out.println(int);}publicstaticvoidmain(String[]args){shorty=6;longz=7;go(y);go(z);}虽然y是short型的,但是编译器把它转换为int型答案:intLong第三题:publicstaticvoidmain(String[]args){Stringtest=TestA.TestB.TestC.;Stringregex=\\.\\s*;String[]result=test.split(regex);for(Strings:result)System.out.print(s+);}\s空白字符*表示0个或任意多个text.split(\\.)按.拆分答案:TestATestBTestC第四题:publicclassAA{privateLonguserId;privateStringnickName;publicLonggetUserId(){returnuserId;}publicvoidsetUserId(LonguserId){this.userId=userId;}publicStringgetNickName(){returnnickName;}publicvoidsetNickName(StringnickName){this.nickName=nickName;}publicstaticvoidmain(String[]args){AAm1=newAA();m1.setUserId(newLong(100001));m1.setNickName(mick);AAm2=newAA();m2.setUserId(newLong(100001));m2.setNickName(mick);System.out.println(m1==m2);System.out.println(m1.equals(m2));}equals方法返回值也是returnm1==m2答案:falsefalse第五题运行下列代码,输出为false的是:()。A.Stringst1=abc;System.out.println(abc==st1);B.Stringst2=abcSystem.out.println(st2.equals(newString(abc)));C.Integeri=100;System.out.println(100==i);D.ArrayListlist=newArrayList();System.out.println(list.contains(null));答案:D第六题publicinterfaceCookie{Cookiecookie=newCart(小面包,盼盼);}publicclassCartimplementsCookie{privateStringname;privateStringproduction;publicCart(Stringname,Stringproduction){this.name=name;this.production=production;}publicvoidsmell(){cookie=newCart(蛋黄派,达利园);}}final要求属性不可更改cookie=newCart(蛋黄派,达利园);这个修改了cookie的地址,所以报错第七题(多选)请看下列代码:publicabstractclassShape{intx;inty;publicabstractvoiddraw();publicvoidsetAnchor(intx,inty){this.x=x;this.y=y;}}下列选项中能正确使用Shape类的是:A.publicclassCircleimplementsShape{privateintradius;}B.publicabstractclassCircleextendsShape{privateintradius;}C.publicclassCircleextendsShape{privateintradius;publicvoiddraw();}D.publicclassCircleextendsShape{privateintradius;publicvoiddraw(){}}抽象类不可以实例化,如果一个类继承了抽象类,必须重新其抽象方法答案:BD第八题(多选)请看下列代码:packagecom.tarena;publicclassGeodetics{publicstaticfinaldoubleDIAMETER=12756.32;}访问静态常量DIAMETER的方式正确的是:A.importcom.tarena.Geodetics;publicclassTerraCarta{publicdoublehalfway(){returnGeodetics.DIAMETER/2.0;}}B.importcom.tarena.Geodetics;publicclassTerraCarta{publicdoublehalfway(){returnDIAMETER/2.0;}}C.importcom.tarena;publicclassTerraCarta{publicdoublehalfway(){returnGeodetics.DIAMETER/2.0;}}D.importcom.tarena.*;publicclassTerraCarta{publicdoublehalfway(){returnGeodetics.DIAMETER/2.0;}}答案:AD第九题(多选)在Java语言中,下列说法正确的是()。A.一个接口可以继承多个接口B.一个类可以继承多个类C.一个类可以实现多个接口D.一个类可以有多个子类答案:ACD第十题(多选)在插入代码处,填入下列代码编译正确的是:publicvoidfoo(int[]x){插入代码}A.foreach(intz:x)System.out.println(z);B.for(intz:x)System.out.println(z);C.while(x.hasNext())System.out.println(x.next());D.for(inti=0;ix.length;i++)System.out.println(x[i]);答案:BD第十一题(多选)查看如下代码:classA{protectedintmethod(inta,intb){return0;}}下列选项中,可以在A的子类中使用的是()。A.publicintmethod(inta,intb){return0;}B.privateintmethod(inta,intb){return0;}C.privateintmethod(inta,longb){return0;}D.publicshortmethod(inta,intb){return0;}A:重写A类中的方法C:重载A类中的方法答案:AC第十二题(多选)在Java语言中,下列说法正确的是:()。A.StringBuffer和StringBuilder的区别在于:StringBuffer是线程安全的而StringBuilder不是。B.String是不可变对象,而StringBuffer中封装的字符串数据是可以动态改变的C.判断两个StringBuilder对象的字符序列是否相同,可以调用其equlas方法进行比较。D.String的重写了equals方法,重写的逻辑是:字符序列相同的String对象equals方法返回true。答案:ABD第十三题(多选)题目代码的功能为:输出每个字符在一个字符串中出现的次数(不区分大小写)。Stringstr=ewrwqFrewqfsadfdsfdsfs;str=str.toLowerCase();intmax_length=0;while(str.length()0){《插入代码》}A.intlength=str.length();charfirst=str.charAt(0);StringstrNew=str.replaceAll(String.valueOf(first),);if(lengthstrNew.length()){max_length=length-strNew.length();System.out.println(first+:+max_length);}B.intlength=str.length();charfirst=str.charAt(0);StringstrNew=str.replaceAll(String.valueOf(first),);if(lengthstrNew.length()){max_length=length-strNew.length();str=strNew;System.out.println(first+:+max_length);}C.intlength=str.length();Stringfirst=str.substring(0,1);StringstrNew=str.replaceAll(first,);if(lengthstrNew.length()){max_length=length-strNew.length();str=strNew;System.out.println(first+:+max_length);}D.intlength=str.length();Stringfirst=str.substring(0,1);StringstrNew=str.replaceAll(first,);if(lengthstrNew.length()){max_length=length-strNew.length();System.out.println(first+:+max_length);}答案:BC第十四题(多选)下面的方法属于StringBuffer的是:()。A.sizeB.insertC.deleteD.length答案:BCD第十五题(多选)请看下列代码:publicclassOld{publicstaticObjectget(Listlist){returnlist.get(0);}}以下选项调用get方法,能编译通过的是:A.Objecto=Old.get(newLinkedList());B.Objecto=Old.get(newLinkedList?());C.Strings=Old.get(newLinkedListString());D.Strings=(String)Old.get(newLinkedListString());答案:AD1.重写必须继承,重载不用。2.重写的方法名,参数数目相同,参数
本文标题:java面向对象考试题
链接地址:https://www.777doc.com/doc-5772973 .html