文档库 最新最全的文档下载
当前位置:文档库 › 【免费下载】第七章继承多态练习题

【免费下载】第七章继承多态练习题

第七章继承多态

一、选择题:

1、分析:

class A {

A() { }

}

class B extends A { //系统自动生成的构造方法和类的访问权限一样

}

哪两种说法是正确的? ()

A:类B的构造方法是public的. B:类B的构造方法包含对this()的调用. C:类B的构造方法没有参数. D:类B的构造方法包含对super()的调用.

2、运行结果是:()

class Base {

Base() { System.out.print("Base"); }

}

public class Alpha extends Base {

public static void main( String[] args ) {

new Alpha();

new Base();

}

}

A: Base B: BaseBase C: 编译失败. D: 没有输出.E: 运行时异常.

3. 程序的运行结果是?(

D: hello from b E: hello from a

hello from a hello from b

4. 运行结果是:()

class TestSuper {

TestSuper(int i) { }

}

class TestSub extends TestSuper{ }

class TestAll {

public static void main (String [] args) {

new TestSub();

}

}

A: 编译失败.B: 程序运行没有异常.

C: 第7行抛出异常.D: 第2行抛出异常.

5. 程序的运行结果是?(

A: 0B: 1C: 2D: 编译失败.6. 对于语句"B is a D" 和 "B has a C",一下哪两种说法是正确的? ()A:D 是B.B:B 是D.C:D 是C.D:B 是C.E:D 继承 B.

F:B 继承D.

7. 运行结果是?()

A: 1

B: 2C: 第8行编译失败.D: 第14行编译失败.

8. 分析: public class ConstOver { public ConstOver(int x, int y, int z) { } } 哪两个是对ConstOver 的构造方法的重载? ()A:ConstOver() { }B:protected int ConstOver() { }C:private ConstOver(int z, int y, byte x) { }D:public Object ConstOver(int x, int y, int z) { }E:public void ConstOver(byte x, byte y, byte z) { }9. 运行结果是?(

)A: 4,4B: 4,5C: 5,4D: 5,5E: 编译失败.

10. 分析:

public class X { public X aMethod() { return this;} } 1) public class Y extends X { 2) 3) } 在第2行可以插入哪两项? ()A:public void aMethod() { }B:private void aMethod() { }C:public void aMethod(String s) { }D:private Y aMethod() { return null; }E:public X aMethod() { return new Y(); }11. 运行结果是?(

)A: 4,4B: 4,5C: 5,4D: 5,5E: 编译失败.

12. 以下哪两个重载了方法setVar()? ()

public class MethodOver {

public void setVar(int a, int b, float c) {

}

}

A:private void setVar(int a, float c, int b) { } B:public int setVar(int a, float c, int b) {return a;}

C:protected void setVar(int a, int b, float c) { } D:public int setVar(int a, int b, float c) {return a;}

E:protected float setVar(int a, int b, float c) {return c;}13. 分析:

1) class BaseClass {

2) private float x = 1.0f;

3) protected void setVar(float f) { x = f; }

4) }

5) class SubClass extends BaseClass {

6) private float x = 2.0f;

7) // insert code here 8) } 在第7行插入哪两个覆盖了方法setVar()? ()A:void setVar(float f) { x = f; }B:public void setVar(int f) { x = f; }C:public void setVar(float f) { x = f; }D:public double setVar(float f) { return f; }E:public final void setVar(float f) { x = f; }F:protected float setVar() { x = 3.0f; return 3.0f; }14. 运行结果是?(

)A: 1B: 2

C: 运行时异常.D: 第8行编译错误.E: 第14行编译错误.15. 分析: class A { protected int method1(int a, int b) { return 0; } } 在A 的子类中,以下哪两个方法是合法的? ()A:public int method1(int a, int b) { return 0; } B:private int method1(int a, long b) { return 0; }C:private int method1(int a, int b) { return 0; } D:public short method1(int a, int b) { return 0; }E:static protected int method1(int a, int b) { return 0; }16. 分析:

1) public abstract class Test {

2) public abstract void methodA();

3)

4) public abstract void methodB()

5) {

6) System.out.println("Hello");

7) }

8) }

哪两种改法,可以使程序通过编译? ()

A:给方法methodA()加方法体C:在Test的声明中去掉abstract

B:用";"替换第5-7行D:在方法methodA()的声明中去掉abstract E: 在方法methodB()的声明中去掉abstract

17. 运行结果是:()

1) abstract class AbstractIt {

2) abstract float getFloat();

3) }

4) public class AbstractTest extends AbstractIt {

5) private float f1 = 1.0f;

6) private float getFloat() { return f1; }

7) }

A: 编译成功.B: 运行时异常.C: 第2行编译失败.D: 第6行编译失败.

18. 在接口中哪两个方法的声明是合法的? ()

A:void method1();B:public void method2();C:static public void method5();

D:protected void method3();E:final public void method4();

19. 分析:

1) public interface Foo {

2) int k = 4;

3) }

哪三项与第2行等价? ()

A:final int k = 4;B:public int k = 4;C:static int k = 4;

D:abstract int k = 4;E:volatile int k = 4;F:protected int k = 4;

20. 分析:

interface Inter { }

class A implements Inter { }

class B extends A {

B() {

A[] arr = new A[10];

boolean b1 = this instanceof Inter;

boolean b2 = arr instanceof Object;

System.out.println("b1 = " + b1 + ", b2 = " + b2);

}

}

创建B的对象时会输出?()

A: 编译失败.B: b1 = true, b2 = true C: b1 = true, b2 = false

D: b1 = false, b2 = true E: b1 = false, b2 = false

21. 哪一个能通过编译?()

A: new Animal().soundOff();B: Lion l = Alpha1.get("meat eater");

C: Elephant e = new Alpha1();D: new Alpha1().get("veggie").soundOff(); 22. 分析:

class Passenger { }

class Engine { }

interface TransportVehicle {

void loadPassengers();

}

interface Helicopter extends TransportVehicle {

int flyIt( String direction );

}

abstract class JetStream implements Helicopter { }

哪种说法是正确的?()

A: TransportVehicle has a Passenger.B: 类Engine在类JetStream中.

C: 接口TransportVehicle可以形成多态的基础.

D: 继承JetStream的非抽象类可以随意声明方法loadPassengers().

23. 哪三个是"is a" 关系? ()

A:public class X { } B:public interface Shape { }

public class Y extends X { } public interface Rectangle extends Shape{ }

C:public interface Color { } D:public class Species { }

public class Shape { private Color color; } public class Animal { private Species species; }

E:public class Person { }F:interface Component { }

public class Employee { class Container implements

Component {

public Employee(Person person) { } private Component[] children;}}

24. 运行结果是:()

public interface Test {

int frood = 42;

}

class TestImpl implements Test {

private static int frood;

public static void main(String[] args) { System.out.println(++frood); } } A: 0B: 1C: 42D: 43E: 编译失败.

F: 运行时异常.

25. 运行结果是?(

)A: 5B: 10C: 编译失败.D: 运行时异常.

26. 运行结果是:()

1) public class Test {

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

3) class Foo {

4) public int i = 3;

5) }

6) Object o = (Object)new Foo();

7) Foo foo = (Foo)o;

8) System.out.println("i = " + foo.i);

9) }

10) }

A: i = 3C: 第6行抛出ClassCastException异常.

B: 编译失败.D: 第7行抛出ClassCastException异常.

27. 分析:

String s = "abcde";

Object ob = (Object)s;

if (ob.equals(s)) {

System.out.println("AAAA");

} else {

System.out.println("BBBB");

}

if (s.equals(ob)) {

System.out.println("CCCC");

} else {

System.out.println("DDDD");

}

输出哪两行? ()

A:AAAA B:BBBB C:CCCC D:DDDD

二、简答题

1. 简述类与类之间的关系

2. 简述继承的基本概念及继承的过程

3. 简述方法重写以及方法重载与方法重写的区别

4. 简述super关键字的用法

5. 简述重写equals方法的步骤

6. 简述instanceof关键字的用法

7. 简述接口和抽象类的区别

8. 简述什么是多态

三、编程题

1. 设计一个学生类Student,包括的属性有姓名name,年龄age,学位degree。由学生类派生出本科生类Undergraduate和研究生类Graduate,本科生类包含的属性有专业specialty,研究生类包括的属性有研究方向studyDrection。每个类都有相关数据的输出方法。最后在一个测试类中对设计的类进行测试。

要求测试结果如下:

姓名:王军

年龄:23

学位:本科

专业:工业自动化

姓名:刘君

年龄:27

学位:硕士

研究方向:网络技术

2. 定义一组交通工具类,包括汽车类(AutoMobile)、飞机类(Plane)、船类(Ship),其中汽车类有坦克(Tank)、卡车(Truck)、小汽车(Car),飞机子类有客机(AirLiner)、战斗机(FighterPlane),船类有子类游艇(Yacht)、军舰(WarShip),坦克战斗机和军舰都具有攻击的功能,抽象并定义这些类,通过抽象类和接口以及对应的实现类实现多态和这些类的测试

相关文档