Java題庫——Chapter13抽象類和接口

1)What is the output of running class Test?java

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

調用過程:先初始化一個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)這樣層層調用,在調用棧中就是BEDCide


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以前ui


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


4)The printout from the following code is ________.spa

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]code

注意這裏的list1不是new出來的,與list指向相同的內存區域對象


5)Analyze the following code.blog

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的一個子類


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.

子類能夠覆蓋父類中的具體方法來聲明它是抽象的。
抽象類不能被實例化!

相關文章
相關標籤/搜索