java面向对象考试题.doc
《java面向对象考试题.doc》由会员分享,可在线阅读,更多相关《java面向对象考试题.doc(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、第一题:public static void main (String args)String fileNames = abc.txt, bcd.exe, cde.exe,def.dat,efg.exe ; for (String fileName : fileNames) if (fileName.endsWith(.exe) System.out.print(fileName.substring(0, fileName.lastIndexOf(.exe)+ );fileName.endsWith(.exe)判断是否已.exe结尾substring(0,4)截取0-3的字符串,不包含4las
2、tIndexOf(.exe)获取.exe最后一次出现的位置答案:bcd cde efg第二题:public static void go(Long n) System.out.println(Long); public static void go(Short n) System.out.println(Short); public static void go(int n) System.out.println(int); public static void main(String args) short y = 6; long z = 7; go(y); go(z); 虽然y是short
3、型的,但是编译器把它转换为int型答案:int Long第三题:public static void main(String args)String test = Test A. Test B. Test C.;String regex = .s*; String result = test.split(regex); for(String s : result) System.out.print(s + ); s 空白字符 *表示0个或任意多个 text.split(.)按.拆分答案: Test A Test B Test C 第四题:public class AA private Long
4、 userId; private String nickName; public Long getUserId() return userId;public void setUserId(Long userId) this.userId = userId;public String getNickName() return nickName;public void setNickName(String nickName) this.nickName = nickName;public static void main(String args)AA m1=new AA(); m1.setUser
5、Id(new Long(100001); m1.setNickName(mick); AA m2=new AA(); m2.setUserId(new Long(100001); m2.setNickName(mick); System.out.println(m1=m2); System.out.println(m1.equals(m2); equals方法返回值也是return m1=m2答案:false false第五题运行下列代码,输出为false的是:()。A.String st1 = abc;System.out.println(abc = st1);B.String st2 =
6、abcSystem.out.println(st2.equals(newString(abc);C.Integer i = 100; System.out.println(100 = i);D.ArrayList list = new ArrayList();System.out.println(list.contains(null);答案:D第六题public interface Cookie Cookie cookie=new Cart (小面包,盼盼); public class Cart implements Cookie private String name; private St
7、ring production;public Cart(String name,String production) this.name=name; this.production=production; public void smell()cookie =new Cart(蛋黄派,达利园); final 要求属性不可更改cookie =new Cart(蛋黄派,达利园); 这个修改了 cookie的地址,所以报错第七题(多选)请看下列代码: public abstract class Shape int x;int y; public abstract void draw(); publi
8、c void setAnchor(int x, int y) this.x = x; this.y = y;下列选项中能正确使用Shape类的是:A.public class Circle implements Shape private int radius; B.public abstract class Circle extends Shape private int radius; C.public class Circle extends Shape private int radius; public void draw(); D.public class Circle exten
9、ds Shape private int radius;public void draw() 抽象类不可以实例化,如果一个类继承了抽象类,必须重新其抽象方法答案:BD第八题(多选)请看下列代码:package com.tarena;public class Geodetics public static final double DIAMETER = 12756.32;访问静态常量DIAMETER的方式正确的是:A.import com.tarena.Geodetics;public class TerraCarta public double halfway()return Geodetic
10、s.DIAMETER/2.0;B.import com.tarena.Geodetics;public class TerraCarta public double halfway()return DIAMETER/2.0;C.import com.tarena;public class TerraCarta public double halfway()return Geodetics.DIAMETER/2.0;D.import com.tarena.*;public class TerraCarta public double halfway()return Geodetics.DIAME
11、TER/2.0;答案:AD第九题(多选)在Java语言中,下列说法正确的是()。A.一个接口可以继承多个接口B.一个类可以继承多个类C.一个类可以实现多个接口D.一个类可以有多个子类答案:ACD第十题(多选)在处,填入下列代码编译正确的是:public void foo(int x) A.foreach(int z : x) System.out.println(z);B.for(int z : x) System.out.println(z);C.while( x.hasNext() System.out.println( x.next();D.for( int i=0; i 0) 插入代码
12、 A.int length = str.length(); char first=str.charAt(0); String strNew = str.replaceAll(String.valueOf(first), ); if (lengthstrNew.length() max_length = length - strNew.length(); System.out.println(first+:+max_length); B.int length = str.length(); char first=str.charAt(0); String strNew = str.replace
13、All(String.valueOf(first), ); if (lengthstrNew.length() max_length = length - strNew.length();str = strNew;System.out.println(first+:+max_length); C.int length = str.length();String first = str.substring(0, 1);String strNew = str.replaceAll(first, );if (lengthstrNew.length() max_length = length - st
14、rNew.length();str = strNew;System.out.println(first+:+max_length);D.int length = str.length();String first = str.substring(0, 1);String strNew = str.replaceAll(first, );if (lengthstrNew.length() max_length = length - strNew.length(); System.out.println(first+:+max_length);答案:BC第十四题(多选)下面的方法属于StringB
15、uffer的是:()。A.sizeB.insertC.deleteD.length答案:BCD第十五题(多选)请看下列代码: public class Old public static Object get(List list) return list.get(0); 以下选项调用get方法,能编译通过的是:A.Object o = Old.get(new LinkedList();B.Object o = Old.get(new LinkedList();C.String s = Old.get(new LinkedList();D.String s = (String)Old.get(n
16、ew LinkedList();答案:AD1.重写必须继承,重载不用。2.重写的方法名,参数数目相同,参数类型兼容,重载的方法名相同,参数列表不同。3.重写的方法修饰符大于等于父类的方法,重载和修饰符无关。4.重写不可以抛出父类没有抛出的一般异常,可以抛出运行时异常public void main (int x)int d = a+b+c;对的第十六题(单选题)从下面四段(A,B,C,D)代码中选择出正确的代码段( )(A)abstract class Name private String name;public abstract boolean isStupidName(String na
17、me) (B)public class Something void doSomething () private String s = ;int l = s.length();(C)public class Something public static void main(String args) Other o = new Other();new Something().addOne(o);public void addOne(final Other o) o.i+; class Other public int i;(D)public class Something public in
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 面向 对象 考试题
限制150内