2023年11月29日发(作者:)

Java题库——Chapter11继承和多态

1)Analyze the following code:

public class Test {

public static void main(String[ ] args) {

B b = new B();

b.m(5);

n("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 compilation error, because m is overridden with a different signature in B.

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

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

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

B中没有重写⽅法mB继承了A中的⽅法m,并在B中定义了⼀个重载的⽅法m

2)Analyze the following code.

// Program 1

public class Test {

public static void main(String[ ] args) {

Object a1 = new A();

Object a2 = new A();

n(((A)a1).equals((A)a2));

}

}

class A {

int x;

public boolean equals(A a) {

return this.x == a.x;

}

}

// Program 2

public class Test {

public static void main(String[ ] args) {

A a1 = new A();

A a2 = new A();

n((a2));

}

}

A)Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them.

B)A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely

unrelated.

C)It is a compilation error if two methods differ only in return type in the same class.

D)A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the

superclass is hidden.

E)To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its

superclass.

1、重载⼀个⽅法是提供多个具有相同名称但具有不同签名的⽅法来区分它们。

2、不能覆盖私有⽅法,因为私有⽅法在他的类本⾝以外是不能被访问的。如果在⼦类中定义的⽅法在⽗类中是私有的,那么这两个⽅法是

完全不相关的。

3、如果两个⽅法在同⼀个类中仅在返回类型上不同,则为编译错误。

4、静态⽅法不能覆盖。如果⽗类中定义的静态⽅法在⼦类中被重新定义,那么定义在⽗类中的静态⽅法将被隐藏

5、要重写⽅法,必须在⼦类中使⽤与其超类相同的签名和兼容的返回类型来定义⽅法。

6)Encapsulation means ________. 6) _______

A)that a variable of supertype can refer to a subtype object

B)that a class can contain another class

C)that a class can extend another class

D)that data fields should be declared private

封装

7)Analyze the following code: (Choose all that apply.)

public class Test extends A {

public static void main(String[ ] args) {

Test t = new Test();

();

C)The program has a compile error because you attempted to invoke the Circle class's constructor illegally.

9)What is the output of the following code:

public class Test {

public static void main(String[ ] args) {

Object o1 = new Object();

Object o2 = new Object();

((o1 == o2) + " " + ((o2)));

}

}

A)true true B) false true C)true false D) false false

o1o2值不相同,引⽤也不相同

10)Invoking ________ returns the first element in an ArrayList x. 10) ______

A)() B) (1) C) () D) (0)

11)Analyze the following code:

public class Test {

public static void main(String[ ] args) {

String s = new String("Welcome to Java");

Object o = s;

String d = (String)o;

}

}

A)When casting o to s in String d = (String)o, a new object is created.

B)When casting o to s in String d = (String)o, the contents of o is changed.

C)s, o, and d reference the same String object.

D)When assigning s to o in Object o = s, a new object is created.

sod引⽤相同的字符串对象。

12)Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors? (Choose all that

apply.) 12) ______

A)(2) B)(2, "New York"); C)(2) D)() E)(1)

两个String元素,下标范围0,1

13)Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing]?

(Choose all that apply.) 13) ______

A)(1) B) (0) C)("Singapore") D) (2)

14)Given the following code, find the compile error. (Choose all that apply.)

public class Test {

public static void main(String[ ] args) {

m(new GraduateStudent());

m(new Student());

m(new Person());

m(new Object());

B)m(new Person()) causes an error

C)m(new Object()) causes an error

D)m(new Student()) causes an error

m(new Person()) m(new Object())

相当于

Student x = new Person();

Student x = new Object();

是⾮法的,因为PersonObject的实例不是Student的实例

15)Which of the following classes cannot be extended? 15) ______

A)final class A { } B) class A { private A();} C)class A { protected A();} D) class A { }

16)Analyze the following code: (Choose all that apply.)

public class Test {

public static void main(String[ ] args) {

Object a1 = new A();

Object a2 = new Object();

n(a1);

n(a2);

}

}

class A {

int x;

public String toString() {

return "A's x is " + x;

}

}

A)When executing n(a2), the toString() method in the Object class is invoked.

B)When executing n(a1), the toString() method in the Object class is invoked.

C)When executing n(a1), the toString() method in the A class is invoked.

D)The program cannot be compiled, because n(a1) is wrong and it should be replaced by n(ng());

ObjectA的⽗类,A类重写了toString()⽅法

17)Inheritance means ________. 17) ______

A)that a variable of supertype can refer to a subtype object

B)that a class can extend another class

C)that a class can contain another class

D)that data fields should be declared private

18)Composition means ________. 18) ______

A)that a class can extend another class

B)that a variable of supertype can refer to a subtype object

A)If a method overrides another method, these two methods must have the same signature.

B)A method can be overridden in the same class.

C)If a method overloads another method, these two methods must have the same signature.

D)A method can be overloaded in the same class.

覆盖必然有相同的函数签名,重载的函数签名必然不同

⼀个类中可以有被重载的⽅法,但不能出现覆盖,覆盖关系必然是出现在继承的⽗⼦类中。

21)Analyze the following code: (Choose all that apply.)

ArrayList list = new ArrayList();

("Beijing");

("Tokyo");

("Shanghai");

(3, "Hong Kong");

A)If you replace the last line by (4, "Hong Kong"), the code will compile and run fine.

B)The last line in the code causes a runtime error because there is no element at index 3 in the array list.

C)The last line in the code has a compile error because there is no element at index 3 in the array list.

D)If you replace the last line by (3, "Hong Kong"), the code will compile and run fine.

22)Analyze the following code.

// Program 1:

public class Test {

public static void main(String[ ] args) {

Object a1 = new A();

Object a2 = new A();

n((a2));

}

}

public boolean equals(Object a) {

return this.x == ((A)a)x;

}

}

// Program 2:

public class Test {

public static void main(String[ ] args) {

Object a1 = new A();

Object a2 = new A();

n((a2));

}

}

class A {

int x;

public boolean equals(A a) {

return this.x == a.x;

}

}

A)Program 1 displays false and Program 2 displays false

B)Program 1 displays false and Program 2 displays true

C)Program 1 displays true and Program 2 displays false

D)Program 1 displays true and Program 2 displays true

25)Which of the statements regarding the super keyword is incorrect? 25) ______

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

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

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

D)You can use super to invoke a super class constructor.

26)Given the following code:

class C1 {}

class C2 extends C1 { }

class C3 extends C2 { }

class C4 extends C1 {}

C1 c1 = new C1();

C2 c2 = new C2();

C3 c3 = new C3();

C4 c4 = new C4();

StringTokenizer does not have a default constructor.

C)The program has a compilation error because A does not have a default constructor.

D)The program would compile fine if you add the following constructor into A: A(String s) { }

30)Analyze the following code:

Circle c = new Circle (5);

Cylinder c = cy;

30) ______

A)The code has a runtime error. B)The code has a compile error. C)The code is fine.

31)Given the following classes and their objects:

class C1 {};

class C2 extends C1 {};

class C3 extends C1 {};

C2 c2 = new C2();

C3 c3 = new C3();

Analyze the following statement:

c2 = (C2)((C1)c3);

A)You will get a runtime error because you cannot cast objects from sibling classes.

B)You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form.

C)c3 is cast into c2 successfully.

D)The statement is correct.

运⾏时错误,不能从兄弟类型转换对象

32)Which of the following statements are true? (Choose all that apply.) 32) ______

A)"class A extends B" means A is a subclass of B.

B)A subclass is usually extended to contain more functions and more detailed information than its superclass.

C)"class A extends B" means B is a subclass of A.

D)A subclass is a subset of a superclass.

33)The getValue() method is overridden in two ways. Which one is correct?

I:

public class Test {

回类型的⼦类型。

34)What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are

accessible to any subclasses in any package?

A)public B) protected C)private D) Use the default modifier.

protected允许位于任何包中的⼦类或者同⼀包中的类访问该类的成员。

35)You can create an ArrayList using ________. 35) ______

A)ArrayList() B) new ArrayList[100] C)new ArrayList() D) new ArrayList[ ]

ArrayList⽤来存储不限定个数的对象,初始化时没有个数参数。

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

A)public boolean equals(String other)

B)public static boolean equals(Object other)

C)public static boolean equals(String other)

D)public boolean equals(Object other)

37)You can assign ________ to a variable of Object[ ] type. (Choose all that apply.) 37) ______

A)new double[100]

B)new int[100]

C)new char[100]

D)new [100]

E)new String[100]

doubleintchar等基本数据类型不是对象,不能被new创建

38)Invoking ________ returns the number of the elements in an ArrayList x. 38) ______

A)e() B) gth(0) C)() D) (1)

39)Which of the following statements is false? 39) ______

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

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

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

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

40)What is the output of the following code:

public class Test {

class B extends A {

public B() {

n(

"The default constructor of B is invoked");

}

}

public class C {

public static void main(String[ ] args) {

B b = new B();

}

}

A)"The default constructor of B is invoked"

B)"The default constructor of B is invoked""The default constructor of A is invoked"

C)"The default constructor of A is invoked"

D)"The default constructor of A is invoked""The default constructor of B is invoked"

E)Nothing displayed

先创建⽗类对象再创建⼦类对象

43)Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing,

Chicago, Singapore]? 43) ______