欢迎来到天天文库
浏览记录
ID:56783176
大小:91.50 KB
页数:24页
时间:2020-07-11
《java面向对象考精彩试题.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、第一题: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"结尾subs
2、tring(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;long
3、z=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第四题:publi
4、cclassAA{privateLonguserId;privateStringnickName;publicLonggetUserId(){returnuserId;}publicvoidsetUserId(LonguserId){this.userId=userId;}publicStringgetNickName(){returnnickName;}publicvoidsetNickName(StringnickName){this.nickName=nickName;}publicstaticvoidmain(String[]args){AAm1=newAA
5、();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("ab
6、c"==st1);B.Stringst2="abc"System.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{pri
7、vateStringname;privateStringproduction;publicCart(Stringname,Stringproduction){this.name=name;this.production=production;}publicvoidsmell(){cookie=newCart("蛋黄派","达利园");}}final要求属性不可更改cookie=newCart("蛋黄派","达利园");这个修改了cookie的地址,所以报错第七题(多选)请看下列代码:publicabstractclassShape{intx;inty;publi
此文档下载收益归作者所有