java题库——继承和多态

目录

一. 单选题

1. (单选题)What is the output of the following code?public class Test {public static void main(String[] args) { new Person().printPerson();new Student().printPerson();}}class Student extends Person {@Overridepublic String getInfo() { return "Student";}}class Person {public String getInfo() { return "Person";}public void printPerson() { System.out.println(getInfo());}}

2. (单选题)Which of the statements regarding the super keyword is incorrect?

3. (单选题)Analyze the following code:ArrayList list = new ArrayList<>(); list.add( );list.add("Tokyo"); list.add("Shanghai"); list.set(3, "Hong Kong");

4. (单选题)Assume Cylinder is a subtype of Circle. Analyze the following code:Circle c = new Circle (5); Cylinder c = cy;

5. (单选题)Invoking returns the number of the elements in an ArrayList x.

6. (单选题)Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code?String element = "red";for (int i = 0; i < list.size(); i++) if (list.get(i).equals(element)) list.remove(element);

7. (单选题)You can create an ArrayList using .

8. (单选题)Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code?String element = "red";for (int i = list.size() - 1; i >= 0; i--) if (list.get(i).equals(element)) list.remove(element);

9. (单选题)Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be .

10. (单选题)Analyze the following code:Double[] array = {1, 2, 3};ArrayList list = new ArrayList<>(Arrays.asList(array)); System.out.println(list);

11. (单选题)Given the following code, which of the following expressions evaluates to false?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();

12. (单选题)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;}}

13. (单选题)Analyze the following code: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

12. Which of the following statements are true?

14. (单选题)Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code:class Square extends GeometricObject { double length;Square(double length) { GeometricObject(length);}}

15. (单选题)Analyze the following code.// Program 1: public class Test {public static void main(String[] args) { Object a1 = new A();Object a2 = new A(); System.out.println(a1.equals(a2));}}class A { int x;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(); System.out.println(a1.equals(a2));}}class A { int x;public boolean equals(A a) { return this.x == a.x;}}

二. 多选题

16. (多选题)Analyze the following code:public class Test extends A {public static void main(String[] args) { Test t = new Test();t.print();}}class A { String s;

17. (多选题)You can assign to a variable of Object[] type.

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

19. (多选题)Given the following code, find the compile error.public class Test {public static void main(String[] args) { m(new GraduateStudent());m(new Student());m(new Person());m(new Object());}public static void m(Student x) { System.out.println(x.toString());}}class GraduateStudent extends Student {}class Student extends Person {@Overridepublic String toString() { return "Student";}}class Person extends Object {@Overridepublic String toString() { return "Person";}}

20. Polymorphism means .

一. 单选题

1. (单选题)What is the output of the following code?

public class Test {
public static void main(String[] args) { new Person().printPerson();
new Student().printPerson();
}
}

class Student extends Person {
@Override
public String getInfo() { return "Student";
}
}

class Person {
public String getInfo() { return "Person";
}

public void printPerson() { System.out.println(getInfo());
}
}

  • A. Student Person
  • B. Person Person
  • C. Student Student
  • D. Person Student

我的答案: D

答案解析:b See LiveExample 11.6.

2. (单选题)Which of the statements regarding the super keyword is incorrect?

  • 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 super to invoke a super class constructor.
  • D. You can use super.super.p to invoke a method in superclass's parent class.

我的答案: D

答案解析:c Using super.super is not allowed in Java. So, the answer to this question is C.

Section 11.4 Overriding Methods

3. (单选题)Analyze the following code:

ArrayList<String> list = new ArrayList<>(); list.add( );
list.add("Tokyo"); list.add("Shanghai"); list.set(3, "Hong Kong");

  • A. If you replace the last line by list.add(3, "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 list.add(4, "Hong Kong"), the code will compile and run fine. 
     

我的答案: C

4. (单选题)Assume Cylinder is a subtype of Circle. Analyze the following code:

Circle c = new Circle (5); Cylinder c = cy;

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

我的答案: A

答案解析:a You cannot assign a variable of a supertype to a subtype without explicit casting.

5. (单选题)Invoking returns the number of the elements in an ArrayList x.

  • A. x.size()
  • B. x.getSize()
  • C. x.getLength(0)
  • D. x.length(1)

我的答案: A

答案解析:d See Figure 11.3.

6. (单选题)Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code?

String element = "red";
for (int i = 0; i < list.size(); i++) if (list.get(i).equals(element)) list.remove(element);

  • A. {"red", "red", "green"}
  • B. {"red", "green"}
  • C. {}
  • D. {"green"}

我的答案: B

答案解析:b See Figure 11.3.

7. (单选题)You can create an ArrayList using .

  • A. ArrayList()
  • B. new ArrayList[100]
  • C. new ArrayList<>()
  • D. new ArrayList[]

我的答案: C

答案解析:c You can create an ArrayListing using its no-arg constructor. Since ArrayList is a generic type, new ArrayList<>() automatically discovers the type in the defining type. For example, ArrayList<String> list = new ArrayList<>();

8. (单选题)Suppose an ArrayList list contains {"red", "red", "green"}. What is the list after the following code?

String element = "red";
for (int i = list.size() - 1; i >= 0; i--) if (list.get(i).equals(element)) list.remove(element);

  • A. {"red", "red", "green"}
  • B. {}
  • C. {"red", "green"}
  • D. {"green"}

我的答案: D

答案解析:c See Figure 11.3.

9. (单选题)Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be .

  • A. false
  • B. true

我的答案: B

答案解析:a t1 and t2 refer to the same object.

10. (单选题)Analyze the following code:

Double[] array = {1, 2, 3};
ArrayList<Double> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list);

  • A. The code is correct and displays [1.0, 2.0, 3.0].
  • B. The code has a compile error because asList(array) requires that the array elements are objects.
  • C. The code has a compile error because an integer such as 1 is automatically converted into an Integer object, but the array element type is Double.
  • D. The code is correct and displays [1, 2, 3].

我的答案: C

答案解析:c See the discussion of the asList method in this section.

11. (单选题)Given the following code, which of the following expressions evaluates to false?

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();

  • A. c2 instanceof C1
  • B. c1 instanceof C1
  • C. c3 instanceof C1
  • D. c4 instanceof C2

我的答案: D

答案解析:d c4 is not an instace of C2.

12. (单选题)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 assigning s to o in Object o = s, a new object is created.
  • B. When casting o to s in String
  • C. When casting o to s in String d = (String)o, a new object is created.
  • D. = (String)o, the contents of o is changed.
    d. s, o, and d reference the same String object.

我的答案: D

答案解析:d Casting object reference variable does not affect the contents of the object.

13. (单选题)Analyze the following code:

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. The constructor of class A is called and it displays "i from A is 60".
  • B. { public
  • C. The constructor of class A is called and it displays "i from A is 40".
  • D. () {
    // 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".

我的答案: A

答案解析:d When invoking new B(), B's superclass A's constructor is invoked first. It invokes setI(20). The setI method in B is used because object created is new B(). The setI method in B assigns 3 * 20 to i. So it displays i from A is 60.

12. Which of the following statements are true?


a. A method can be overloaded in the same class.
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. If a method overrides another method, these two methods must have the same signature.
e. A method in a subclass can overload a method in the superclass.

答案:ade See the text at the end of the section.

Section 11.6 The Object Class and Its toString() Method 

14. (单选题)Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code:

class Square extends GeometricObject { double length;

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

  • A. The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally.
  • B. The program compiles fine, but you cannot create an instance of Square because the constructor does not specify the length of the Square.
  • C. The program compiles fine, but it has a runtime error because of invoking the Square class's constructor illegally.

我的答案: A

答案解析:b You have to use super() or super(withapproriatearguments) to invoke a super class constructor explicitly.

15. (单选题)Analyze the following code.

// Program 1: public class Test {
public static void main(String[] args) { Object a1 = new A();
Object a2 = new A(); System.out.println(a1.equals(a2));
}
}

class A { int x;

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(); System.out.println(a1.equals(a2));
}
}

class A { int x;

public boolean equals(A a) { return this.x == a.x;
}
}

  • A. Program 1 displays false and Program 2 displays true
  • B. Program 1 displays true and Program 2 displays false
  • C. Program 1 displays true and Program 2 displays true
  • D. Program 1 displays false and Program 2 displays false

我的答案: B

答案解析:c In Program 1, the equals method in the Object class is overridden. a1.equals(a2) invokes this method. It returns true. In Program 2, the equals method in the Object class is not overridden. a1.equals(a2) invokes the equals method defined in the Object class, which returns false in this case.

二. 多选题

16. (多选题)Analyze the following code:

public class Test extends A {
public static void main(String[] args) { Test t = new Test();
t.print();
}
}

class A { String s;

  • A. The program would compile if a default constructor A(){ } is added to class A explicitly.
  • B. The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed.
  • C. The program compiles, but it has a runtime error due to the conflict on the method name print.
  • D. (String s) { this.s = s;
    }

    public void print() { System.out.println(s);
    }
    }
    a. The program does not compile because Test does not have a default constructor Test().

我的答案: AB

答案解析:bc The class Test has a default constructor, which invokes the superclass A's no-arg constructor. But A has no no- arg constructor. This causes an error.

Section 11.3.2 Constructor Chaining

17. (多选题)You can assign to a variable of Object[] type.

  • A. new java.util.Date[100]
  • B. new int[100]
  • C. new String[100]
  • D. new double[100]
  • E. new char[100]

我的答案: AC

答案解析:de Primitive data type array is not compatible with Object[].

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

  • A. x.remove(2)
  • B. x.remove("Singapore")
  • C. x.remove(0)
  • D. x.remove(1)

我的答案: BD

答案解析:ac See Figure 11.3.

19. (多选题)Given the following code, find the compile error.

public class Test {
public static void main(String[] args) { m(new GraduateStudent());
m(new Student());
m(new Person());
m(new Object());
}

public static void m(Student x) { System.out.println(x.toString());
}
}

class GraduateStudent extends Student {
}

class Student extends Person {
@Override
public String toString() { return "Student";
}
}

class Person extends Object {
@Override
public String toString() { return "Person";
}
}

  • A. m(new Person()) causes an error
  • B. m(new Student()) causes an error
  • C. m(new GraduateStudent()) causes an error
  • D. m(new Object()) causes an error

我的答案: AD

答案解析:cd You cannot pass a supertype variable to a subtype without explicit casting.

20. Polymorphism means .


a. that data fields should be declared private
b. that a class can extend another class
c. that a variable of supertype can refer to a subtype object
d. that a class can contain another class

答案:C   See the Key Point.

Section 11.8 Dynamic Binding 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Sweep-

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

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

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

打赏作者

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

抵扣说明:

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

余额充值