继承与多态.doc

上传人:苏美尔 文档编号:9041679 上传时间:2021-01-31 格式:DOC 页数:10 大小:69.50KB
返回 下载 相关 举报
继承与多态.doc_第1页
第1页 / 共10页
继承与多态.doc_第2页
第2页 / 共10页
继承与多态.doc_第3页
第3页 / 共10页
继承与多态.doc_第4页
第4页 / 共10页
继承与多态.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《继承与多态.doc》由会员分享,可在线阅读,更多相关《继承与多态.doc(10页珍藏版)》请在三一文库上搜索。

1、1. 下面关于继承的描述正确的有: 【选择两项】ADA 在java 中只允许单一继承。 /B 在java 中一个类只能实现一个接口。 /一个类只能实现多个接口C 在java 中一个类不能同时继承一个类和实现一个接口。 /所有的类都继承objectD java 的单一继承使代码更可靠。/2. 下面程序中第10行调用的方法是在第几行声明的: 【选择一项】 D1 class Person 2 public void printValue(int i, int j) /*/ 3 public void printValue(int i)/*.*/ 4 5 public class Teacher ex

2、tends Person 6 public void printValue() /*.*/ 7 public void printValue(int i) /*.*/ 8 public static void main(String args) 9 Person t = new Teacher();/多态10 t.printValue(10); /多态调用子类方法、父类属性11 12 A 第2行 B 第3行 C 第6行 D 第7行 3. 对于下面给定的代码,下面哪些选项可以添加到程序的第11行处: 【选择一项】D 1 class Person 2 String name,department;

3、 3 public void printValue() 4 System.out.println(name is +name); 5 System.out.println(department is +department); 6 7 8 public class Teacher extends Person 9 int salary; 10 public void printValue() 11 /doing the same as in the parent method printValue() 12 System.out.println(salary is +salary); 13 1

4、4 A printValue();/递归B this.printValue();/递归C Person.printValue();/不是静态的D super.printValue()./子类继承父类,重写父类方法4. 对于下面给定的程序,下列选项中的方法哪些可以添加到类Child中:【选择二项】BC 1 public class Parent 2 public int addValue( int a, int b) 3 int s; 4 s = a+b; 5 return s; 6 7 class Child extends Parent 8 A int addValue( int a, in

5、t b )/ do something. /重写,权限不对=B public void addValue ()/ do something. /C public int addValue( int a )/ do something. /D public int addValue( int a, int b )throws MyException /do something. /不能抛出比父类更大类型的异常=5. 编译运行下面程序的结果是: 【选择一项】 E 1 class Parent 2 String one, two; 3 public Parent(String a, String b

6、) 4 one = a; 5 two = b; 6 7 public void print() 8 System.out.println(one); 9 10 11 public class Child extends Parent 12 public Child(String a, String b) 13 super(a,b); 14 15 public void print() 16 System.out.println(one + to + two); 17 18 public static void main(String arg) 19 Parent p = new Parent(

7、south, north); 20 Parent t = new Child(east, west); 21 p.print(); 22 t.print(); 23 24 A 编译失败 B 编译成功,输出:south east C 编译成功,输出: south to north east to west D 编译成功,输出: south to north east E 编译成功,输出: south east to west6. 下面程序哪行将产生编译错误: 【选择一项】 D 1 class Parent 2 private String name; 3 public Parent() 4 5

8、public class Child extends Parent 6 private String department; 7 public Child() 8 public String getValue() return name; /父类私有的属性,子类看不见,可以继承,但不能用 9 public static void main(String arg) 10 Parent p = new Parent(); 11 12 A 第3行 B 第6行 C 第7行 D 第8行 E 第10行7. 类Teacher和类Student都是类Person的子类。 Person p; Student s

9、; Teacher t; 现在假定p,s,t都不为空, 那么对于下面的语句描述正确的是: 【选择一项】 Cif(t instanceof Person) s = (Student)t; /类没有继承关系,不能相互转换A 将构造一个Student 对象。 B 表达式合法 C 编译失败 D 编译成功,但是运行时产生异常8. 下列关于派生类访问基类成员的描述正确的有: 【选择一项】 CA 派生类对象可以使用基类中的所有成员 B 派生类只能访问基类中的private成员C 派生类不能访问基类中的private/父类私有的属性,子类看不见,可以继承,但不能用成员D 派生类不能访问基类中的任何成员9. 对

10、于下面程序的描述正确的有: 【选择一项】 C 1 public class Test 2 public static void main(String args) 3 Vehicle v; 4 Car c; 5 v=new Vehicle(); 6 c=new Car(); 7 v.drive(); 8 c.drive(); 9 v=c; /原来是父类的变量,指向子类的堆内空间 10 v.drive(); /多态调用子类方法 11 12 13 class Vehicle 14 public void drive() 15 System.out.println(Vehicle:drive); 1

11、6 17 18 class Car extends Vehicle 19 public void drive() 20 System.out.println(Car:drive); 21 22 A 在第9行产生编译错误 B 在第9行产生运行时错误 C 输出: Vehicle:drive Car:drive Car:drive D 输出: Vehicle:drive Car:drive Vehicle:drive10. 在Java语言中,声明公共的abstract方法的正确格式: 【选择一项】 AA public abstract void add();/B public abstract vo

12、id add() /不该有方法体C public abstract add();/构造方法不能是抽象的D public virtual add(); 11. 在Java语言中,声明公共的abstract方法的正确格式: 【选择一项】 AA public abstract void add(); B public abstract void add() C public abstract add(); D public virtual add();12. 给定下面的代码片段: class Top public Top(String s)System.out.print(“B”); public

13、class Bottom2 extends Top public Bottom2(String s)System.out.println(“D”); public static void main(String args) new Bottom2(“C”); /不能调用父类有参的构造方法 System.out.println(“ “); 下面哪个是正确的? 【选择一项】 EA BD B DB C BDC D DBC E 编译失败13. 给定下面的代码片段: 1. class Xvoid do1() 2. class Y extends Xvoid do2() 3. 4. class Chrom

14、e 5. public static void main(String args) 6. X x1=new X(); 7. X x2=new Y(); 8. Y y1=new Y(); 9. /insert code here 10. 11. 下面哪个选项插入到第9行是正确的? 【选择一项】 CA x2.do2();/只能是子类重写的方法 B (Y)x2.do2(); C (Y)x2).do2(); D 以上都不正确14. 给定下面的代码片段: 1 class A 2 class B extends A 3 public class ComingThru 4 static String s=“

15、-”; 5 public static void main(String args) 6 A aa=new A2; 7 B ba=new B2; 8 sifter(aa); 9 sifter(ba); 10 sifter(7); 11 System.out.println(s); 12 13 static void sifter(A. a2) s+=“1”; 14 static void sifter(B. b1) s+=“2”; 15 static void sifter(B b1) s+=“3”; 16 static void sifter(Object o) s+=“4”; 17 下面哪

16、个选项是正确的? 【选择一项】DA -124 B -134 C -424 D -434 E -444 F 编译失败15. 下列选项中的描述正确的有: 【选择二项】 ADA Object类的equals()方法判定引用值是否指向同一对象。/比地址 B 如果用= =操作符比较两个变量,是比较他们内容是否一致。 C Object类的equals()方法是判定引用值的内容是否一致。 D String类重写了equals()方法,比较的是内容。16. 下面程序定义了一个类,关于该类说法正确的是【选择一项】Dabstract class abstractClassA 该类能调用new abstractCl

17、ass(),方法实例化为一个对象B 该类不能被继承/抽象类必须被继承,构造方法私有不能被继承,finalC 该类的方法都不能被重写D 以上说法都不对17. 对于下列代码,哪些说法是正确的?【选择两项】BCclass A A() class B extends A A 类B的构造方法的方法体是没有具体语句的。/super()B 类B的构造方法是没有参数的。C 类B的构造方法是public的。D 类B的构造方法隐含调用了this()。/super18. 试图编译、运行下列程序,结果是什么?【选择一项】Eclass Parentint a = 55; public void function()Sy

18、stem.out.println(Parents function();Parent() System.out.println(This is Parent); public class Child extends Parent int a = 15; Child () public static void main(String argv)Parent p = new Child(); System.out.println(p.a); p.function(); public void function()System.out.println(Childs function(); A 编译失

19、败。B 运行异常。C 打印输出结果:55 Childs function()D 打印输出结果:15 Parents function()E 打印输出结果:This is Parent55 Childs function()F 打印输出结果:This is Parent 15 Parents function()19. 给定如下代码,哪一个构造函数能添加至MySub而不带来编译时错误?【选择一项】Eclass MySuper int number; MySuper(int i) number = i; class MySub extends MySuper int count; MySub(in

20、t cnt, int num) super(num); count = cnt; / 在此处添加A MySub( ) super()B MySub(int cnt) count = cnt;C MySub(int cnt) super( ); count = cnt;D MySub(int cnt) count = cnt; super(cnt);/ super(cnt);该是第一句E MySub(int cnt) this (cnt , cnt); /调用自己的构造方法F MySub(int cnt) super(cnt); this (cnt , 0); 20. 定如下程序,哪种说法是正确

21、的?【选择一项】C/ 文件名: MyClass.javapublic class MyClass public static void main (String args) A arrA; B arrB; arrA = new A10; arrB = new B20; arrA = arrB; / (1) arrB = (B ) arrA; / (2) arrA = new A10; arrB = (B ) arrA; / (3) arrA指向父类,没有指向一个子类的对象 class A class B extends A A 该程序因(1)处的赋值而无法被编译。B 该程序在运行时会因(2)处

22、的赋值而抛出java.lang.ClassCastException。C 该程序在运行时会因(3)处的赋值而抛出java.lang.ClassCastException。D 该程序可以编译,并且可以无错地运行,甚至可以移去(2)、(3)两条语句中的(B)强制转换运算符。E 该程序可以编译,并且可以无错地运行,但移去(2)、(3)两条语句中的(B)强制转换运算符之后就并非如此了。21. 在子类的构造方法内的什么位置可以对超类的构造方法进行调用?【选择一项】BA 子类构造方法的任何地方B 子类构造方法的第一条语句处C 子类构造方法的最后一条语句处D 不能对超类的构造方法进行调用22. 给出下面的代

23、码:1. import java.awt.*;2. class Ticker extends Component 3. public static void main (String args) 4. Ticker t = new Ticker();5.6. 7. 下面哪两条语句能够独立地合法地被插入到第5行?【选择两项】BDA boolean test = (Component instanceof t);判断instanceof左边的变量是否是右边变量的类型或者子类型B boolean test = (t instanceof Ticker);C boolean test = t.inst

24、anceof(Ticker);D boolean test = (t instanceof Component);E boolean test = t.instanceof(Object);F boolean test = (t instanceof String);/变量和类型无继承关系,编译失败23. 给出下面的代码,class Foo String doStuff(int x) return hello; 在子类中哪一个方法是非法的? 【选择一项】BA String doStuff(int x) return hello; /B int doStuff(int x) return 42;

25、 /重写的返回类型不一致C public String doStuff(int x) return Hello; /D protected String doStuff(int x) return Hello; /E String doStuff(String s) return Hello; /F int doStuff(String s) return 42; /重载24. 哪两个方法在A类的子类里是合法的? 【选择两项】AC1. class A 2. protected int methodl (int a, int b) return 0;3. A public int methodl (int a, int b) return 0;B private int methodl (int a, int b) return 0;/重写,访问权限小C private int methodl (int a, long b) return 0;重载D public short methodl (int a, int b) return 0:/返回类型不对E static protected int methodl (int a, int b) return 0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1