Java 面向对象程序设计 错题整理 chapter 11

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

I:

public class Test {

public static void main(String[] args) {

A a = new A();

System.out.println(a.getValue());

}

}

class B {

public String getValue() {

return "Any object";

}

}

class A extends B {

public Object getValue() {

return "A string";

}

}

II:

public class Test {

public static void main(String[] args) {

A a = new A();

System.out.println(a.getValue());

}

}

class B {

public Object getValue() {

return "Any object";

}

}

class A extends B {

public String getValue() {

return "A string";

}

}

单选题 (1 分) 1 分

  1.  A.

    Neither

  2.  B.

    I

  3.  C.

    Both I and II

  4.  D.

    II

3.

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

System.out.print((o1 == o2) + " " + (o1.equals(o2)));

}

}

单选题 (1 分) 1 分

  1.  A.

    true true

  2.  B.

    true false

  3.  C.

    false false

  4.  D.

    false true

4.

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(String s) {

this.s = s;

}

public void print() {

System.out.println(s);

}

}

多选题 (1 分) 1 分

  1.  A.

    The program would compile if a default constructor A(){ } is added to class A explicitly.

  2.  B.

    The program does not compile because Test does not have a default constructor Test().

  3.  C.

    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.

  4.  D.

    The program compiles, but it has a runtime error due to the conflict on the method name print.

6.

____________ can search in an unsorted list.

单选题 (1 分) 1 分

  1.  A.

    Linear search

  2.  B.

    Binary search

9.

Which of the following statements are true?

多选题 (1 分) 1 分

  1.  A.

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

  2.  B.

    Dynamic binding can apply to instance methods.

  3.  C.

    Dynamic binding can apply to static methods.

  4.  D.

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

  5.  E.

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

10.

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

单选题 (1 分) 1 分

  1.  A.

    public boolean equals(Object other)

  2.  B.

    public static boolean equals(String other)

  3.  C.

    public boolean equals(String other)

  4.  D.

    public static boolean equals(Object other)

11.

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;

}

}

 回答错误

单选题 (1 分) 0 分

  1.  A.

    Program 1 displays false and Program 2 displays false

  2.  B.

    Program 1 displays true and Program 2 displays true

  3.  C.

    Program 1 displays false and Program 2 displays true

  4.  D.

    Program 1 displays true and Program 2 displays false

14.

You can always successfully cast a subclass to a superclass.

 回答错误

判断题 (1 分) 0 分

  1.  A.

    false

  2.  B.

    true

16.

Invoking _________ removes all elements in an ArrayList x.

单选题 (1 分) 1 分

  1.  A.

    x.delete()

  2.  B.

    x.remove()

  3.  C.

    x.clean()

  4.  D.

    x.empty()

  5.  E.

    x.clear()

17.

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

System.out.println(a1.equals(a2));

}

}

class A {

int x;

public boolean equals(A a) {

return this.x == a.x;

}

}

单选题 (1 分) 1 分

  1.  A.

    Program 1 displays false and Program 2 displays true

  2.  B.

    Program 1 displays false and Program 2 displays false

  3.  C.

    Program 1 displays true and Program 2 displays true

  4.  D.

    Program 1 displays true and Program 2 displays false

19.

Which of the following statements is false?

单选题 (1 分) 1 分

  1.  A.

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

  2.  B.

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

  3.  C.

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

  4.  D.

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

20.

Analyze the following code:

double[] array = {1, 2, 3};

ArrayList<Double> list = new ArrayList<>(Arrays.asList(array));

System.out.println(list);

 回答错误

单选题 (1 分) 0 分

  1.  A.

    The code is correct and displays [1, 2, 3].

  2.  B.

    The code has a compile error because asList(array) requires that the array elements are objects.

  3.  C.

    The code is correct and displays [1.0, 2.0, 3.0].

  4.  D.

    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.

23.

Which of the following is incorrect?

单选题 (1 分) 1 分

  1.  A.

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

  2.  B.

    A constructor may be static.

  3.  C.

    A constructor may be private.

  4.  D.

    A constructor may invoke a static method.

  5.  E.

    A constructor may invoke an overloaded constructor.

25.

Analyze the following code.

// Program 1:

public class Test {

public static void main(String[] args) {

Object circle1 = new Circle();

Circle circle2 = new Circle();

System.out.println(circle1.equals(circle2));

}

}

class Circle {

double radius;

public boolean equals(Circle circle) {

return this.radius == circle.radius;

}

}

// Program 2:

public class Test {

public static void main(String[] args) {

Circle circle1 = new Circle();

Circle circle2 = new Circle();

System.out.println(circle1.equals(circle2));

}

}

class Circle {

double radius;

public boolean equals(Object circle) {

return this.radius ==

((Circle)circle).radius;

}

}

单选题 (1 分) 1 分

  1.  A.

    Program 1 displays false and Program 2 displays true

  2.  B.

    Program 1 displays true and Program 2 displays true

  3.  C.

    Program 1 displays true and Program 2 displays false

  4.  D.

    Program 1 displays false and Program 2 displays false

27.

If a method is declared public in the superclass, you may declare the method private in the subclass.

判断题 (1 分) 1 分

  1.  A.

    true

  2.  B.

    false

31.

You can always successfully cast a superclass to a subclass.

 回答错误

判断题 (1 分) 0 分

  1.  A.

    true

  2.  B.

    false

32.

Which of the following are Java keywords?

单选题 (1 分) 1 分

  1.  A.

    casting

  2.  B.

    cast

  3.  C.

    instanceof

  4.  D.

    instanceOf

33.

Which of the following methods override the toString method in the Object class?

单选题 (1 分) 1 分

  1.  A.

    public String toString(String s)

  2.  B.

    public void toString(String s)

  3.  C.

    public String toString()

  4.  D.

    public static String toString()

39.

Analyze the following code.

// Program 1:

public class Test {

public static void main(String[] args) {

Circle circle1 = new Circle();

Circle circle2 = new Circle();

System.out.println(circle1.equals(circle2));

}

}

class Circle {

double radius;

public boolean equals(Circle circle) {

return this.radius == circle.radius;

}

}

// Program 2:

public class Test {

public static void main(String[] args) {

Circle circle1 = new Circle();

Circle circle2 = new Circle();

System.out.println(circle1.equals(circle2));

}

}

class Circle {

double radius;

public boolean equals(Object circle) {

return this.radius ==

((Circle)circle).radius;

}

}

单选题 (1 分) 1 分

  1.  A.

    Program 1 displays true and Program 2 displays true

  2.  B.

    Program 1 displays false and Program 2 displays true

  3.  C.

    Program 1 displays true and Program 2 displays false

  4.  D.

    Program 1 displays false and Program 2 displays false

41.

Analyze the following code:

Circle c = new Circle (5);

Cylinder c = cy;

单选题 (1 分) 1 分

  1.  A.

    The code is fine.

  2.  B.

    The code has a compile error.

  3.  C.

    The code has a runtime error.

43.

Which of the following statements are true?

多选题 (1 分) 1 分

  1.  A.

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

  2.  B.

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

  3.  C.

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

  4.  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.

  5.  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.

46.

Which statements are most accurate regarding the following classes?

class A {

private int i;

protected int j;

}

class B extends A {

private int k;

protected int m;

}

单选题 (1 分) 1 分

  1.  A.

    An object of B contains data fields j, k, m.

  2.  B.

    An object of B contains data fields i, j, k, m.

  3.  C.

    An object of B contains data fields j, m.

  4.  D.

    An object of B contains data fields k, m.

48.

If a method is declared protected in the superclass, you may declare the method private in the subclass.

 回答错误

判断题 (1 分) 0 分

  1.  A.

    true

  2.  B.

    false

49.

An interface can be a separate unit and can be compiled into a bytecode file.

判断题 (1 分) 1 分

  1.  A.

    false

  2.  B.

    true

50.

What is the output of the following code?

ArrayList<java.util.Date> list = new ArrayList<java.util.Date>();

java.util.Date d = new java.util.Date();

list.add(d);

list.add(d);

System.out.println((list.get(0) == list.get(1)) + " " + (list.get(0)).equals(list.get(1)));

 此题未答

单选题 (1 分) 0 分

  1.  A.

    false false

  2.  B.

    true false

  3.  C.

    true true

  4.  D.

    false true

52.

Which statements are most accurate regarding the following classes?

class A {

private int i;

protected int j;

}

class B extends A {

private int k;

protected int m;

// some methods omitted

}

单选题 (1 分) 1 分

  1.  A.

    In the class B, an instance method can only access j, m.

  2.  B.

    In the class B, an instance method can only access i, j, k, m.

  3.  C.

    In the class B, an instance method can only access k, m.

  4.  D.

    In the class B, an instance method can only access j, k, m.

57.

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

单选题 (1 分) 1 分

  1.  A.

    inheritance

  2.  B.

    generalization

  3.  C.

    abstraction

  4.  D.

    encapsulation

58.

Every class has a toString() method and an equals() method.

判断题 (1 分) 1 分

  1.  A.

    false

  2.  B.

    true

59.

A final class can have instances.

判断题 (1 分) 1 分

  1.  A.

    true

  2.  B.

    false

60.

Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors?

多选题 (1 分) 1 分

  1.  A.

    x.get(1)

  2.  B.

    x.get(2)

  3.  C.

    x.set(2, "New York");

  4.  D.

    x.size()

  5.  E.

    x.remove(2)

64.

The order in which modifiers appear before a class or a method is important.

判断题 (1 分) 1 分

  1.  A.

    false

  2.  B.

    true

65.

Analyze the following code:

Integer[] c = {3, 5};

java.util.Collections.shuffle(c);

System.out.println(java.util.Arrays.toString(c));

单选题 (1 分) 1 分

  1.  A.

    The code is correct and displays [5, 3].

  2.  B.

    The code is correct and displays [3, 5].

  3.  C.

    The code has a compile error on Integer[] c = {3, 5}.

  4.  D.

    The code has a compile error on Collections.shuffle(c). c cannot be an array.

66.

Consider the following declaration for a class A.

class A {

private int x;

private int y;

public A(int x, int y) {

this.x = x;

this.y = y;

}

}

Class B is a subclass of A. Which of the following can be constructors in B?

I:

public B() {

}

II:

public B(int x, int y) {

super(x, y);

}

III:

public B() {

super(0, 0);

}

IV:

public B(int x, int y) {

this.x = x;

this.y = y;

}

多选题 (1 分) 1 分

  1.  A.

    IV

  2.  B.

    II

  3.  C.

    I

  4.  D.

    III

69.

Normally you depend on the JVM to perform garbage collection automatically. However, you can explicitly use ____ to request garbage collection.

单选题 (1 分) 1 分

  1.  A.

    System.gc(0)

  2.  B.

    System.exit()

  3.  C.

    System.exit(0)

  4.  D.

    System.gc()

70.

Analyze the following code:

Double[] array = {1, 2, 3};

ArrayList<Double> list = new ArrayList<>(Arrays.asList(array));

System.out.println(list);

单选题 (1 分) 1 分

  1.  A.

    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.

  2.  B.

    The code is correct and displays [1.0, 2.0, 3.0].

  3.  C.

    The code has a compile error because asList(array) requires that the array elements are objects.

  4.  D.

    The code is correct and displays [1, 2, 3].

71.

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;

}

}

单选题 (1 分) 1 分

  1.  A.

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

  2.  B.

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

  3.  C.

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

  4.  D.

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

72.

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

单选题 (1 分) 1 分

  1.  A.

    The statement is correct.

  2.  B.

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

  3.  C.

    c3 is cast into c2 successfully.

  4.  D.

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

75.

Given two reference variables t1 and t2, if t1.equals(t2) is true, t1 == t2 ___________.

单选题 (1 分) 1 分

  1.  A.

    is always false

  2.  B.

    may be true or false

  3.  C.

    is always true

77.

Analyze the following code:

double[] c = {1, 2, 3};

System.out.println(java.util.Collections.max(c));

单选题 (1 分) 1 分

  1.  A.

    The code has a compile error on Integer[] c = {1, 2, 3}.

  2.  B.

    The code is correct and displays 3.

  3.  C.

    The code is correct and displays 3.0.

  4.  D.

    The code has a compile error on Collections.max(c). c cannot be an array.

83.

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 {

public B() {

// System.out.println("i from B is " + i);

}

public void setI(int i) {

this.i = 3 * i;

}

}

单选题 (1 分) 1 分

  1.  A.

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

  2.  B.

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

  3.  C.

    The constructor of class A is not called.

  4.  D.

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

84.

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

单选题 (1 分) 1 分

  1.  A.

    {}

  2.  B.

    {"red", "green"}

  3.  C.

    {"green"}

  4.  D.

    {"red", "red", "green"}

85.

If a method is declared protected in the superclass, you may declare the method public in the subclass.

 回答错误

判断题 (1 分) 0 分

  1.  A.

    true

  2.  B.

    false

91.

The UML uses _______ before a member name to indicate that the member is protected.

单选题 (1 分) 1 分

  1.  A.

    #

  2.  B.

    +

  3.  C.

    -

  4.  D.

    ?

93.

Which of the statements regarding the super keyword is incorrect?

单选题 (1 分) 1 分

  1.  A.

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

  2.  B.

    You can use super to invoke a super class method.

  3.  C.

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

  4.  D.

    You can use super to invoke a super class constructor.

95.

Analyze the following code.

// Program 1:

public class Test {

public static void main(String[] args) {

Object circle1 = new Circle();

Object circle2 = new Circle();

System.out.println(circle1.equals(circle2));

}

}

class Circle {

double radius;

public boolean equals(Circle circle) {

return this.radius == circle.radius;

}

}

// Program 2:

public class Test {

public static void main(String[] args) {

Object circle1 = new Circle();

Object circle2 = new Circle();

System.out.println(circle1.equals(circle2));

}

}

class Circle {

double radius;

public boolean equals(Object circle) {

return this.radius ==

((Circle)circle).radius;

}

}

单选题 (1 分) 1 分

  1.  A.

    Program 1 displays false and Program 2 displays false

  2.  B.

    Program 1 displays true and Program 2 displays false

  3.  C.

    Program 1 displays false and Program 2 displays true

  4.  D.

    Program 1 displays true and Program 2 displays true

6.

Composition means ______________.

单选题 (1 分) 1 分

  1.  A.

    that a variable of supertype refers to a subtype object

  2.  B.

    that a class extends another class

  3.  C.

    that a class contains a data field that references another object

  4.  D.

    that data fields should be declared private

98.

If a data field is declared in the superclass, you may hide it by redeclaring it in the subclass.

判断题 (1 分) 1 分

  1.  A.

    false

  2.  B.

    true

99.

Analyze the following code:

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

}

public void setI(int i) {

this.i = 3 * i;

}

}

 回答错误

单选题 (1 分) 0 分

  1.  A.

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

  2.  B.

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

  3.  C.

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

  4.  D.

    The constructor of class A is not called.

100.

Analyze the following code:

ArrayList<String> list = new ArrayList<String>();

list.add("Beijing");

list.add("Tokyo");

list.add("Shanghai");

list.set(3, "Hong Kong");

多选题 (1 分) 1 分

  1.  A.

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

  2.  B.

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

  3.  C.

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

  4.  D.

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值