码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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 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 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 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 

  • 相关阅读:
    adb安装及使用大全
    PHP 数组在底层的实现原理
    【Java 进阶篇】JQuery 遍历 —— `each()` 方法的奇妙之旅
    python——Django框架
    java 堆外内存常见误解
    Matlab创建类
    【HCIP】MSTP
    C++类的继承
    RLHF(从人类反馈中进行强化学习)详解(一)
    RCE(Pikachu)
  • 原文地址:https://blog.csdn.net/x20020402/article/details/128163794
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号