Java题目集-Chapter 11 Inheritance and Polymorphism
创始人
2024-05-19 14:22:18
0

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);}@Overridepublic 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);}@Overridepublic 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)。


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

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...