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.