zoukankan      html  css  js  c++  java
  • Java题库——Chapter11 继承和多态

    1)Analyze the following code:

    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 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中没有重写方法m。B继承了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();
        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;    
      }
    }

    A)Program 1 displays true and Program 2 displays true
    B)Program 1 displays false and Program 2 displays true
    C)Program 1 displays false and Program 2 displays false
    D)Program 1 displays true and Program 2 displays false


    3)Invoking ________ removes all elements in an ArrayList x. 3) _______
    A)x.clear() B)x.delete() C)x.remove() D)x.empty() E)x.clean()


    4)Analyze the following code:
    Cylinder cy = new Cylinder(1, 1);
    Circle c = cy; 4) _______
    A)The code has a runtime error. B)The code has a compile error. C)The code is fine.


    5)Which of the following statements are true? (Choose all that apply.) 5) _______
    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();
        t.print();
      }
    }
    class A {
      String s;
      A(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().
    B)The program would compile if a default constructor A(){ } is added to class A explicitly.
    C)The program compiles, but it has a runtime error due to the conflict on the method name print.
    D)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.

    如果父类是一个无缺省参数的构造函数,那么对于派生类一旦没有构造函数,那么就不会自动的先构造父类的构造函数,这是不允许的

    解决方案:

    1、给父类增加一个含有缺省的构造函数

    2、删除掉父类的无缺省参数的构造函数,使用隐式的缺省构造函数


    8)Suppose you create a class Cylinder to be a subclass of Circle. Analyze the following code:

    class Cylinder extends Circle {
      double length;
     
      Cylinder(double radius) {
         Circle(radius);
      }
    }

    A)The program compiles fine, but it has a runtime error because of invoking the Circle class's constructor illegally.
    B)The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not specify the length of the cylinder.
    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();
        System.out.print((o1 == o2) + " " + (o1.equals(o2)));
      }
    }

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

    o1和o2值不相同,引用也不相同


    10)Invoking ________ returns the first element in an ArrayList x. 10) ______
    A)x.get() B) x.get(1) C) x.first() D) x.get(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.

    s、o和d引用相同的字符串对象。


    12)Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors? (Choose all that apply.) 12) ______
    A)x.get(2) B)x.set(2, "New York"); C)x.remove(2) D)x.size() E)x.get(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)x.remove(1) B) x.remove(0) C)x.remove("Singapore") D) x.remove(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());
      }
      public static void m(Student x) {
        System.out.println(x.toString());
      }
    }
    class GraduateStudent extends Student {
    }
    class Student extends Person {
      public String toString() {
        return "Student";
      }
    }
    class Person extends Object {
      public String toString() {
        return "Person";
      }
    }

    A)m(new GraduateStudent()) causes an error
    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();

    是非法的,因为Person和Object的实例不是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();
        System.out.println(a1);
        System.out.println(a2);
      }
    }
    class A {
      int x;
      public String toString() {
        return "A's x is " + x;
      }
    }

    A)When executing System.out.println(a2), the toString() method in the Object class is invoked.
    B)When executing System.out.println(a1), the toString() method in the Object class is invoked.
    C)When executing System.out.println(a1), the toString() method in the A class is invoked.
    D)The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString());

    Object是A的父类,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
    C)that data fields should be declared private
    D)that a class can contain another class


    19)Which of the following statements are true? (Choose all that apply.) 19) ______
    A)Dynamic binding can apply to instance methods.
    B)Dynamic binding can apply to static methods.
    C)The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compilation time.  
    D)A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime.
    E)You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism.

    1、动态绑定可以应用于实例方法。
    2、动态绑定不能应用于静态方法。
    3、编译器根据参数类型、参数数量和编译时参数的顺序找到匹配方法。
    4、一个方法可以在几个子类中实现。Java虚拟机在运行时动态绑定方法的实现。

    5、总是可以将子类的实例传递给父类类型的参数。这个特性称为多态性。

    20)Which of the following statements are true? (Choose all that apply.) 20) ______
    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();
    list.add("Beijing");
    list.add("Tokyo");
    list.add("Shanghai");
    list.set(3, "Hong Kong");

    A)If you replace the last line by list.add(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 list.add(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();
        System.out.println(a1.equals(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;    
      }
    }


    A)Program 1 displays true and Program 2 displays true
    B)Program 1 displays true and Program 2 displays false
    C)Program 1 displays false and Program 2 displays true
    D)Program 1 displays false and Program 2 displays false


    23)Which of the following are Java keywords? 23) ______
    A)instanceOf B) cast
    C)casting D) instanceof


    24)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 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 super.super.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();

    Which of the following expressions evaluates to false?
    A)c4 instanceof C2 B) c2 instanceof C1 C)c1 instanceof C1 D) c3 instanceof C1

    A instanceof B 用来检测A是否为B的一个实例


    27)A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this? 27) ______
    A)The variable should be marked private and an accessor method provided.
    B)The variable should have no special access modifier.  
    C)The variable should be marked protected.
    D)The variable should be marked public.
    E)The variable should be marked private.

    类设计要求特定的成员变量必须能被该类的任何子类访问,否则不能被不是同一包成员的类访问。这要求类的数据成员被标记为protected


    28)Polymorphism means ________. 28) ______
    A)that a variable of supertype can refer to a subtype object
    B)that a class can extend another class
    C)that data fields should be declared private
    D)that a class can contain another class

    父类变量可以引用子类类型对象


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

    import java.util.StringTokenizer;
    public class A extends StringTokenizer {
    }

    A)The program would compile fine if you add the following constructor into A: A(String s) { super(s); }
    B)The program has a compilation error because the default constructor of A invokes the default constructor of StringTokenizer, but 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 {
      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";
      }
    }


    A)I B) II C)Both I and II D) Neither  

    重写的方法必须与被重写的方法具有一样的签名,以及一样或者兼容的返回类型。兼容的含义是重写方法的返回类型可以是被重写方法的返回类型的子类型。


    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 java.util.Date[100]
    E)new String[100]

    double、int和char等基本数据类型不是对象,不能被new创建


    38)Invoking ________ returns the number of the elements in an ArrayList x. 38) ______
    A)x.getSize() B) x.getLength(0) C)x.size() D) x.length(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 {
      public static void main(String[ ] args) {
        String s1 = new String("Java");
        String s2 = new String("Java");
        System.out.print((s1 == s2) + " " + (s1.equals(s2)));
      }
    }

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


    41)The visibility of these modifiers increases in this order: 41) ______
    A)none (if no modifier is used), protected, private, and public.
    B)none (if no modifier is used), private, protected, and public.  
    C)private, protected, none (if no modifier is used), and public.  
    D)private, none (if no modifier is used), protected, and public.  

    修饰符的可见性递增排序


    42)What is the output of running class 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)"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) ______
    A)x.add(2, "Chicago") B) x.add(0, "Chicago")
    C)x.add("Chicago") D) x.add(1, "Chicago")


    44)Object-oriented programming allows you to derive new classes from existing classes. This is called ________. 44) ______
    A)abstraction B) encapsulation C)inheritance D) generalization

    面向对象编程允许从现有类中派生新类,这种方式称为继承。


    45)What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it? 45) ______
    A)public B) protected C)private D) Use the default modifier.


  • 相关阅读:
    Gym
    Gym
    Gym
    Gym
    Gym
    hdu2586 LCA带边权的Targan算法
    bryce1010专题训练——LCA
    POJ1470 LCA (Targan离线)
    bryce1010专题训练——LCT&&树链剖分
    模板——2.7 欧拉函数
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/11672656.html
Copyright © 2011-2022 走看看