当前位置:文档之家› 05继承与多态

05继承与多态

05继承与多态
05继承与多态

1.下面关于继承的描述正确的有:【选择两项】AD

A在java 中只允许单一继承。//

B在java 中一个类只能实现一个接口。//一个类只能实现多个接口

C在java 中一个类不能同时继承一个类和实现一个接口。//所有的类都继承object

D java 的单一继承使代码更可靠。//

2.下面程序中第10行调用的方法是在第几行声明的:【选择一项】D

1 class Person {

2 public void printValue(int i, int j) {/*…*/ }

3 public void printValue(int i){/*...*/ }

4 }

5 public class Teacher extends 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 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 }

14 }

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, int 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){

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("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 west

6.下面程序哪行将产生编译错误:【选择一项】D

1 class Parent {

2 private String name;

3 public Parent(){}

4 }

5 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;

Teacher t;

现在假定p,s,t都不为空,

那么对于下面的语句描述正确的是:【选择一项】C

if(t instanceof Person) {s = (Student)t; } //类没有继承关系,不能相互转换

A将构造一个Student 对象。

B表达式合法

C编译失败

D编译成功,但是运行时产生异常

8.下列关于派生类访问基类成员的描述正确的有:【选择一项】C A派生类对象可以使用基类中的所有成员

B派生类只能访问基类中的private成员

C派生类不能访问基类中的private成员

D派生类不能访问基类中的任何成员

9.对于下面程序的描述正确的有:【选择一项】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");

16 }

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:drive

10.在Java语言中,声明公共的abstract方法的正确格式:【选择一项】A

A public abstract void add();//

B public abstract void add(){} //不该有方法体

C public abstract add();//构造方法不能是抽象的

D public virtual add();

11.在Java语言中,声明公共的abstract方法的正确格式:【选择一项】A

A 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 class Bottom2 extends Top{

public Bottom2(String s){System.out.println(“D”);}

public static void main(String[] args){

new Bottom2(“C”); //不能调用父类有参的构造方法

System.out.println(“ “);

}

}

下面哪个是正确的?【选择一项】E

A BD

B DB

C BDC

D DBC

E编译失败

13.给定下面的代码片段:

1. class X{void do1(){}}

2. class Y extends X{void do2(){}}

3.

4. class Chrome{

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行是正确的?【选择一项】C

A 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=“-”;

5 public static void main(String[] args){

6 A[] aa=new A[2];

7 B[] ba=new B[2];

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 }

下面哪个选项是正确的?【选择一项】D

A-124

B-134

C-424

D-434

E-444

F编译失败

15.下列选项中的描述正确的有:【选择二项】AD

A Object类的equals()方法判定引用值是否指向同一对象。//比地址B如果用= =操作符比较两个变量,是比较他们内容是否一致。

C Object类的equals()方法是判定引用值的内容是否一致。

D String类重写了equals()方法,比较的是内容。

16.下面程序定义了一个类,关于该类说法正确的是【选择一项】D abstract class abstractClass{

……

}

A该类能调用new abstractClass(),方法实例化为一个对象

B该类不能被继承//抽象类必须被继承,构造方法私有不能被继承,final C该类的方法都不能被重写

D以上说法都不对

17.对于下列代码,哪些说法是正确的?【选择两项】BC

class A {

A() {}

}

class B extends A {

}

A类B的构造方法的方法体是没有具体语句的。//super()

B类B的构造方法是没有参数的。

C类B的构造方法是public的。

D类B的构造方法隐含调用了this()。//super

18.试图编译、运行下列程序,结果是什么?【选择一项】E

class Parent{

int a = 55;

public void function(){

System.out.println("Parent's 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("Child's function()");

}

}

A编译失败。

B运行异常。

C打印输出结果:55

Child's function()

D打印输出结果:15

Parent's function()

E打印输出结果:This is Parent

55

Child's function()

F打印输出结果:This is Parent

15

Parent's function()

19.给定如下代码,哪一个构造函数能添加至MySub而不带来编译时错误?【选择一项】

E

class MySuper {

int number;

MySuper(int i) { number = i; }

}

class MySub extends MySuper {

int count;

MySub(int 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.定如下程序,哪种说法是正确的?【选择一项】C

// 文件名: MyClass.java

public class MyClass {

public static void main (String[ ] args) {

A [ ] arrA;

B [ ] arrB;

arrA = new A[10];

arrB = new B[20];

arrA = arrB; // (1)

arrB = (B[ ]) arrA; // (2)

arrA = new A[10];

arrB = (B[ ]) arrA; // (3) arrA指向父类,没有指向一个子类的对象

}

}

class A {}

class B extends A {}

A该程序因(1)处的赋值而无法被编译。

B该程序在运行时会因(2)处的赋值而抛出https://www.doczj.com/doc/f116707568.html,ng.ClassCastException。

C该程序在运行时会因(3)处的赋值而抛出https://www.doczj.com/doc/f116707568.html,ng.ClassCastException。

D该程序可以编译,并且可以无错地运行,甚至可以移去(2)、(3)两条语句中的(B[])强制转换运算符。

E该程序可以编译,并且可以无错地运行,但移去(2)、(3)两条语句中的(B[])强制转换运算符之后就并非如此了。

21.在子类的构造方法内的什么位置可以对超类的构造方法进行调用?【选择一项】B

A子类构造方法的任何地方

B子类构造方法的第一条语句处

C子类构造方法的最后一条语句处

D不能对超类的构造方法进行调用

22.给出下面的代码:

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行?【选择两项】BD

A boolean test = (Component instanceof t);判断instanceof左边的变量是否是右边变量的类型或者子类型

B boolean test = (t instanceof Ticker);

C boolean test = t.instanceof(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"; }

}

在子类中哪一个方法是非法的? 【选择一项】B

A String doStuff(int x) { return "hello"; }//

B int doStuff(int x) { return 42; }//重写的返回类型不一致

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类的子类里是合法的? 【选择两项】AC

1. 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;}

相关主题
文本预览
相关文档 最新文档