Java题目集-Chapter 11 Inheritance and Polymorphism

1.Object-oriented programming allows you to derive new classes from existing classes. This is called ______B______.

A.encapsulation

B.inheritance

C.abstraction

D.generalization

解:面向对象的编程允许您从现有类派生新类。这称为继承

2.Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code:(B

class Square extends GeometricObject {
  double length;

  Square(double length) {
    GeometricObject(length);
  }
}

A.The program compiles fine, but you cannot create an instance of Square because the constructor does not specify the length of the Square.

B.The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally.

C.The program compiles fine, but it has a runtime error because of invoking the Square class's constructor illegally.

解:子类可以继承父类所有的成员变量和成员方法,但不继承父类的构造方法

3.What is the output of running class C?(C)

class A {
  public A() {
    System.out.println(
      "The default constructor of A is invoked");
  }
}

class B extends A {
  public B() {
    System.out.println(
      "The default constructor of B is invoked");
  }
}

public class C  {
  public static void main(String[] args) {
    B b = new B();
  }
}

A.Nothing displayed

B."The default constructor of B is invoked"

C."The default constructor of A is invoked" followded by "The default constructor of B is invoked"

D."The default constructor of B is invoked" followed by "The default constructor of A is invoked"

E."The default constructor of A is invoked"

解:1. 如果子类没有定义构造方法,则调用父类的无参数的构造方法。 2. 如果子类定义了构造方法,不论是无参数还是带参数,在创建子类的对象的时候,首先执行父类无参数的构造方法,然后执行自己的构造方法。

4.Which of the following is incorrect?(A)

A.A constructor may be static.

B.A constructor may be private.

C.A constructor may invoke a static method.

D.A constructor may invoke an overloaded constructor.

E.A constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded constructor or its superclass constructor.

解:由于构造函数不是类属性,因此有理由认为它不能是静态的

5.Which of the statements regarding the super keyword is incorrect?(C)

A.You can use super to invoke a super class constructor.

B.You can use super to invoke a super class method.

C.You can use super.super.p to invoke a method in superclass's parent class.

D.You cannot invoke a method in superclass's parent class.

解:super.super 破坏了Java的封装性,对于子类而言,它只考虑它自己和父类,而不关心父类的父类

6.Analyze the following code:(D)

public class Test { 
  public static void main(String[] args) {
    B b = new B();
    b.m(5);
    System.out.println("i is " + b.i);
  }
}

class A {
  int i;

  public void m(int i) { 
    this.i = i; 
  }
}

class B extends A {
  public void m(String s) {
  }
}

A.The program has a compile error, because m is overridden with a different signature in B.

B.The program has a compile error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.

C.The program has a runtime error on b.i, because i is not accessible from b.

D.The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

解:方法 m 在 B 中未被重写。B 从 A 继承方法 m,并在 B 中定义重载方法 m。

P.S:重写和重载的区别:

        作用范围:重写的作用范围是父类和子类之间;重载是发生在一个类里面;

        参数列表:重载必须不同;重写不能修改

        返回类型:重载可修改;重写方法返回相同类型或子类

7.Analyze the following code:(B)

public class Test {
  public static void main(String[] args) {
    new B();
  }
}

class A {
  int i = 7;

  public A() {
    System.out.println("i from A is " + i);
  }

  public void setI(int i) {
    this.i = 2 * i;
  }
}

class B extends A {
  public B() {
    setI(20);
    // System.out.println("i from B is " + i);
  }

  @Override
  public void setI(int i) {
    this.i = 3 * i;

  }
}

A.The constructor of class A is not called.

B.The constructor of class A is called and it displays "i from A is 7".

C.The constructor of class A is called and it displays "i from A is 40".

D.The constructor of class A is called and it displays "i from A is 60".

8.Analyze the following code:(D)

public class Test {
  public static void main(String[] args) {
    new B();
  }
}

class A {
  int i = 7;

  public A() {
    setI(20);
    System.out.println("i from A is " + i);
  }

  public void setI(int i) {
    this.i = 2 * i;
  }
}

class B extends A {
  public B() {
    // System.out.println("i from B is " + i);
  }

  @Override
  public void setI(int i) {
    this.i = 3 * i;
  }
}

A.The constructor of class A is not called.

B.The constructor of class A is called and it displays "i from A is 7".

C.The constructor of class A is called and it displays "i from A is 40".

D.The constructor of class A is called and it displays "i from A is 60".

9.Which of the following statements is false?(D)

A.You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism.

B.The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time.

C.A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime.

D.Dynamic binding can apply to static methods.

E.Dynamic binding can apply to instance methods.

解:动态绑定的属性和方法只有相应的实例可以使用;

10.Which of the following are Java keywords?(B)

A.instanceOf

B.instanceof

C.cast

D.casting

解:instanceof是Java中的二元运算符,左边是对象,右边是类;当对象是右边类或子类所创建对象时,返回true;否则,返回false。

11.The equals method is defined in the Object class. Which of the following is correct to override it in the String class?(B)

A.public boolean equals(String other)

B.public boolean equals(Object other)

C.public static boolean equals(String other)

D.public static boolean equals(Object other)

12.Which of the following classes cannot be extended?(C)

A.class A { }

B.class A { private A() { } }

C.final class A { }

D.class A { protected A() { } }

解:使用 final定义的类不能够有子类,并且该类中的成员方法都默认为final方法。 

13.Which of the following statements is false?(D)

A.A public class can be accessed by a class from a different package.

B.A private method cannot be accessed by a class in a different package.

C.A protected method can be accessed by a subclass in a different package.

D.A method with no visibility modifier can be accessed by a class in a different package.

解:

public表示他们可以被任何其他类访问。

protected允许子类访问父类中的数据域或方法,但不允许非子类访问这些数据域和方法。


如果没有使用可见性修饰符,那么默认类、方法和数据域可以被同一个包中的任何一个类访问。这称为包私有(package-private)或包内访问(package-access)。


限定方法和数据域只能在自己的类中访问。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxx_xiyuyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值