• Java题库——Chapter13抽象类和接口


    1)What is the output of running class Test?

    public class Test {
      public static void main(String[ ] args) {
        new Circle9();
      }
    }
    public abstract class GeometricObject {
      protected GeometricObject() {
        System.out.print("A");
      }
      protected GeometricObject(String color, boolean filled) {
        System.out.print("B");
      }
    }
    public class Circle9 extends GeometricObject {
      /** Default constructor */
      public Circle9() {
        this(1.0);
        System.out.print("C");
      }
      /** Construct circle with a specified radius */
      public Circle9(double radius) {
        this(radius, "white", false);
        System.out.print("D");
      }
      /** Construct a circle with specified radius, filled, and color */
      public Circle9(double radius, String color, boolean filled) {
        super(color, filled);
        System.out.print("E");
      }
    }

    A)CBAE B)BACD C)ABCD D)BEDC E)AEDC

    调用过程:先初始化一个Circle9对象时,需要调用Circle9的缺省参数的构造函数Circle9() ,而这个缺省参数的构造函数调用Circle9(double radius) ,而Circle9(double radius) 又调用Circle9(double radius, String color, boolean filled) ,而Circle9(double radius, String color, boolean filled)需要调用其父类构造函数GeometricObject(String color, boolean filled)这样层层调用,在调用栈中就是BEDC


    2)Which of the following class definitions defines a legal abstract class? 4) _______
    A)abstract class A { abstract void unfinished(); }
    B)class A { abstract void unfinished() {     } }
    C)public class abstract A { abstract void unfinished(); }
    D)class A { abstract void unfinished(); }

    abstract要写在 class或void之前


    3)Which of the following statements are correct? (Choose all that apply.) 6) _______
    A)Number i = 4.5; B) Double i = 4.5; C)Object i = 4.5; D) Integer i = 4.5;

    Number类是数值包装类的抽象父类,Object是所有类的父类
    4)The printout from the following code is ________.

    java.util.ArrayList list = new java.util.ArrayList();
    list.add("New York");
    java.util.ArrayList list1 = list;
    list.add("Atlanta");
    list1.add("Dallas");
    System.out.println(list1);

    A)[New York, Atlanta, Dallas] B) [New York, Atlanta] C)[New York, Dallas] D) [New York]

    注意这里的list1不是new出来的,与list指向相同的内存区域


    5)Analyze the following code.

    public class Test {
      public static void main(String[ ] args) {
        Number x = new Integer(3);
        System.out.println(x.intValue());
        System.out.println((Integer)x.compareTo(new Integer(4)));
      }
    }

    A)The program has a compile error because the member access operator (.) is executed before the casting operator.
    B)The program has a compile error because an Integer instance cannot be assigned to a Number variable.
    C)The program has a compile error because x cannot be cast into Integer.
    D)The program has a compile error because intValue is an abstract method in Number.  
    E)The program compiles and runs fine.

    该题本想现将Number对象x转换为子类,使用子类中才能使用的compareTo方法,但是这里是先使用访问操作符(.)在进行向子类型的转换,所以在没有完成转换的情况下,Number对象x没有compareTo方法


    6)Which of the following declares an abstract method in an abstract Java class? 11) ______
    A)public abstract method();  
    B)public void method() {}  
    C)public abstract void method();  
    D)public abstract void method() {}  
    E)public void abstract Method();  


    7)Which of the following is a correct interface? 12) ______
    A)interface A { void print();}
    B)abstract interface A { abstract void print() { };}
    C)abstract interface A { print(); }
    D)interface A { void print() { }; }

    接口中的变量是public static final

    接口中的方法是public abstract

    或者可以省略不写


    8)What is the output of Integer.parseInt("10", 2)? 13) ______
    A)1; B) 2;  C)Invalid statement; D) 10;

    parseInt(String s , int radix)有两个参数,一个参数是需要转换的数值字符串,另一个是指定的进制的基数

    parseInt("10", 2)就表示将“10”转换为2进制下的数

    9)To create an instance of BigInteger for 454, use 14) ______
    A)new BigInteger("454"); B) new BigInteger(454); C)BigInteger("454"); D) BigInteger(454);


    10)Suppose A is an interface, B is a concrete class with a default constructor that implements A. Which of the following is correct? (Choose all that apply.) 15) ______
    A)A a = new A(); B) B b = new A(); C)A a = new B(); D) B b = new B();

    可以看成,B是A的一个子类,A是接口,不能够实例化!
    11)The java.lang.Number and its subclasses are introduced in Chapter 11. Analyze the following code.

        Number numberRef = new Integer(0);
        Double doubleRef = (Double)numberRef;


    A)The program runs fine, since Integer is a subclass of Double.
    B)There is no such class named Integer. You should use the class Int.
    C)The compiler detects that numberRef is not an instance of Double.
    D)You can convert an int to double, so you can cast an Integer instance to a Double instance.
    E)A runtime class casting exception occurs, since numberRef is not an instance of Double.

    Number是Double的父类,父类对象不是子类对象的实例

    12)Which of the following statements regarding abstract methods are true? (Choose all that apply.)
    A)An abstract method cannot be contained in a nonabstract class.  
    B)Abstract classes have constructors.
    C)It is possible to declare an abstract class that contains no abstract methods.  
    D)A class that contains abstract methods must be abstract.
    E)A data field can be declared abstract.

    F)The constructors in an abstract class should be protected.

    G)You may declare a final abstract class.

    抽象方法不能包含在非抽象类中。
    抽象类有构造函数。
    可以声明一个不包含抽象方法的抽象类。
    包含抽象方法的类必须是抽象的。
    抽象类的数据字段不能声明为抽象的!!

    抽象类的构造方法应该定义为protected,因为它只被子类使用。


    13)Which of the following statements are true? (Choose all that apply.) 19) ______
    A)If you compile an interface without errors, but with warnings, a .class file is created for the interface.
    B)If you compile a class without errors but with warnings, a .class file is created.
    C)If you compile an interface without errors, a .class file is created for the interface.
    D)If you compile a class with errors, a .class file is created for the class.

    没有错误,即使有警告也能编译通过

    14)The java.lang.Cloneable interface is introduced in Chapter 11. Analyze the following code.

    public class Test {
      public static void main(String[ ] args) {
        java.util.Date x = new java.util.Date();
        java.util.Date y = x.clone();
        System.out.println(x = y);
      }
    }

    A)x = y in System.out.println(x = y) causes a runtime error because you cannot have an assignment statement inside a statement.  
    B)The program has a compile error because the return type of the clone() method is java.lang.Object.  
    C)x = y in System.out.println(x = y) causes a compile error because you cannot have an assignment statement inside a statement.
    D)A java.util.Date object is not cloneable.


    15)Analyze the following code.

    1. public class Test  {
    2.   public static void main(String[ ] args) {
    3.     Fruit[ ] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)};
    4.     java.util.Arrays.sort(fruits);
    5.   }
    6. }
    class Fruit {
      private double weight;
      public Fruit(double weight) {
        this.weight = weight;
      }
    }

    A)The program has a compile error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.
    B)The program has a runtime error on Line 3 because the Fruit class does not have a default constructor.
    C)The program has a compile error because the Fruit class does not have a default constructor.
    D)The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.

    想要使用sort方法,必须先要实现Comparable接口


    16)Assume Calendar calendar = new GregorianCalendar(). ________ returns the week of the year.
    A)calendar.get(Calendar.WEEK_OF_YEAR)
    B)calendar.get(Calendar.WEEK_OF_MONTH)
    C)calendar.get(Calendar.MONTH)
    D)calendar.get(Calendar.MONTH_OF_YEAR)


    17)Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which of the following is correct? (Choose all that apply.) 27) ______
    A)B b = new B(); B) A a = new B(); C)B b = new A(); D) A a = new A();


    18)Assume Calendar calendar = new GregorianCalendar(). ________ returns the number of days in a month. 29) ______
    A)calendar.get(Calendar.WEEK_OF_MONTH)
    B)calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
    C)calendar.get(Calendar.MONTH)
    D)calendar.get(Calendar.WEEK_OF_YEAR)
    E)calendar.get(Calendar.MONTH_OF_YEAR)


    19)The printout from the following code is ________.

    java.util.ArrayList list = new java.util.ArrayList();
    list.add("New York");
    java.util.ArrayList list1 = (java.util.ArrayList)(list.clone());
    list.add("Atlanta");
    list1.add("Dallas");
    System.out.println(list1);

    A)[New York, Atlanta, Dallas] B) [New York] C)[New York, Atlanta] D) [New York, Dallas]

    注意这里使用的是克隆,使用克隆后list1和list是内容相同的两个不同对象了


    20)What is the output of the following code?

    public class Test {
      public static void main(String[ ] args) {
        java.math.BigInteger x = new java.math.BigInteger("3");
        java.math.BigInteger y = new java.math.BigInteger("7");
        x.add(y);
        System.out.println(x);
      }
    }

    A)10 B) 11 C) 3 D) 4

    注意:这里x.add(y)之后并没有将结果赋值给x


    21)________ is a reference type. (Choose all that apply.) 32) ______
    A)A class type B) An array type C)A primitive type D) An interface type


    33)Assume Calendar calendar = new GregorianCalendar(). ________ returns the month of the year. 33) ______
    A)calendar.get(Calendar.WEEK_OF_YEAR)
    B)calendar.get(Calendar.WEEK_OF_MONTH)
    C)calendar.get(Calendar.MONTH_OF_YEAR)
    D)calendar.get(Calendar.MONTH)

    没有MONTH_OF_YEAR这种说法,每年一定是12个月!!!


    22)The java.lang.Comparable interface is introduced in Chapter 11. Analyze the following code: (Choose all that apply.)

    public class Test1  {
      public Object max(Object o1, Object o2) {
        if ((Comparable)o1.compareTo(o2) >= 0) {
          return o1;
        }
        else {
          return o2;
        }
      }
    }

    A)The program would compile if ((Comparable)o1.compareTo(o2) >= 0) is replaced by (((Comparable)o1).compareTo(o2) >= 0).
    B)The program has a compile error because Test1 does not have a main method.
    C)The program has a compile error because o1 is an Object instance and it does not have the compareTo method.  
    D)The program has a compile error because you cannot cast an Object instance o1 into Comparable.

     
    23)Show the output of running the class Test in the following code lines:

    interface A {
    }
    class C {  
    }
    class B extends D implements A {
    }
    public class Test extends Thread {
      public static void main(String[ ] args) {
        B b = new B();
        if (b instanceof A)
          System.out.println("b is an instance of A");
        if (b instanceof C)
          System.out.println("b is an instance of C");
      }
    }
    class D extends C {  
    }

    A)b is an instance of C.
    B)b is an instance of A followed by b is an instance of C.
    C)b is an instance of A.
    D)Nothing

    24) The java.util.Calendar and java.util.GregorianCalendar classes are introduced in Chapter 11. Analyze the following code.
    1. import java.util.*;
    2. public class Test {
    3.   public static void main(String[ ] args) {
    4.     Calendar[ ] calendars = new Calendar[10];
    5.     calendars[0] = new Calendar();
    6.     calendars[1] = new GregorianCalendar();
    7.   }
    8. }
    A)The program has a compile error on Line 5 because java.util.Calendar is an abstract class.  
    B)The program has no compile errors.
    C)The program has a compile error on Line 4 because java.util.Calendar is an abstract class.
    D)The program has a compile error on Line 6 because Calendar[1] is not of a GregorianCalendar type.

    抽象类对象Calendar不能使用new来实例化


    25)The GeometricObject and Circle classes are defined in Chapter 11. Analyze the following code. (Choose all that apply.)

    public class Test {
      public static void main(String[ ] args) {
        GeometricObject x = new Circle(3);
        GeometricObject y = (Circle)(x.clone());
        System.out.println(x);
        System.out.println(y);
      }
    }

    A)After you override the clone() method and make it public in the Circle class, the problem can compile and run just fine, but y is null if Circle does not implement the Cloneable interface.
    B)If GeometricObject implements Cloneable and Circle overrides the clone() method, the clone() method will work fine to clone Circle objects.
    C)To enable a Circle object to be cloned, the Circle class has to override the clone() method and implement the java.lang.Cloneable interface.
    D)The program has a compile error because the clone() method is protected in the Object class.  


    26)Analyze the following code.

    public class Test {
      public static void main(String[ ] args) {
        Number x = new Integer(3);
        System.out.println(x.intValue());
        System.out.println(x.compareTo(new Integer(4)));
      }
    }


    A)The program has a compile error because an Integer instance cannot be assigned to a Number variable.
    B)The program has a compile error because x does not have the compareTo method.
    C)The program compiles and runs fine.
    D)The program has a compile error because intValue is an abstract method in Number.  

    x为Number的实例对象,没有compareTo 的实例方法


    27)Which of the following statements regarding abstract methods are true? (Choose all that apply.)
    A)A subclass of a non-abstract superclass can be abstract.   
    B)An abstract class can be extended.
    C)A subclass can override a concrete method in a superclass to declare it abstract.
    D)An abstract class can be used as a data type.
    E)An abstract class can have instances created using the constructor of the abstract class.

    非抽象父类的子类可以是抽象的,比如一个抽象类其父类是Object,并不是一个抽象类

    子类可以覆盖父类中的具体方法来声明它是抽象的。
    抽象类不能被实例化!

  • 相关阅读:
    团队冲刺第四天
    团队冲刺第三天
    团队冲刺第二天
    团队冲刺第一天
    典型用户及用户场景描述
    《构建之法》阅读笔记01
    第九周学习进度条
    第八周学习进度条
    软件工程概论课堂作业3
    课堂练习-找水王续
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/11714609.html
Copyright © 2020-2023  润新知