1)You can declare two variables with the same name in ________. 1) _______
A)a method one as a formal parameter and the other as a local variable
B)different methods in a class
C)two nested blocks in a method (two nested blocks means one being inside the other)
D)a block
重載函數
2)What is the printout for the second statement in the main method?
public class Foo { static int i = 0; static int j = 0; public static void main(String[ ] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } }
A)k is 0 B) k is 2 C) k is 3 D) k is 1
注意 大括號里的int j = 3是局部變量,離開作用域無效
3)Analyze the following code: (Choose all that apply.)
class Test { private double i; public Test(double i) { this.t(); this.i = i; } public Test() { System.out.println("Default constructor"); this(1); } public void t() { System.out.println("Invoking t"); } }
A)this.t() may be replaced by t().
B)this(1) must be called before System.out.println("Default constructor").
C)this.i may be replaced by i. //同名需要用this來區別
D)this(1) must be replaced by this(1.0).
4)What is the printout for the first statement in the main method?
public class Foo { static int i = 0; static int j = 0; public static void main(String[ ] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } }
A)i + j is 5 B) i + j is 23 C) i + j is 22 D) i + j is 6
注意這里的輸出是字符串,"i + j is " + i + j
5)Analyze the following code:
class Circle { private double radius; public Circle(double radius) { radius = radius; } }
A)The program does not compile because Circle does not have a default constructor.
B)The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.
C)The program has a compilation error because it does not have a main method.
D)The program has a compilation error because you cannot assign radius to radius.
6)What is the printout for the third statement in the main method?
public class Foo { static int i = 0; static int j = 0; public static void main(String[ ] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } }
A)j is 1 B) j is 0 C) j is 3 D) j is 2
7)Which of the following statements are true about an immutable object? (Choose all that apply.)
A)All properties of an immutable object must be of primitive types.
B)All properties of an immutable object must be private.
C)An immutable object contains no mutator methods.
D)The contents of an immutable object cannot be modified.
E)An object type property in an immutable object must also be immutable.
不可變對象的所有屬性必須是私有的。
不可變對象不包含任何更改器(mutator)方法。
不可變對象的內容不能修改。
不可變對象中的對象類型屬性也必須是不可變的。
8)Which of the following statements are correct? (Choose all that apply.) 1) _______
A)new java.math.BigInteger(343);
B)new java.math.BigDecimal("343.445");
C)new java.math.BigDecimal(343.445);
D)new java.math.BigInteger("343");
new BigDecimal(String)
new BigInteger(String)
9)Which of the following statements will convert a string s into i of int type? (Choose all that apply.) 3) _______
A)i = Integer.valueOf(s);
B)i = Integer.parseInt(s);
C)i = (new Integer(s)).intValue();
D)i = Integer.valueOf(s).intValue();
E)i = (int)(Double.parseDouble(s));
paseInt(s)方法將一個數值字符串s轉換為一個int值
inValue()返回包裝類對象對應的int值
靜態方法valueOf(String s)創建一個新對象,並將t初始化為指定字符串表示的值
10)In JDK 1.5, you may directly assign a primitive data type value to a wrapper object. This is called ________. 5) _______
A)auto conversion B) auto casting C)auto unboxing D) auto boxing
將基本數據類型轉換為包裝類對象稱為裝箱(boxing)
將包裝類對象轉換為基本數據類型對象稱為拆箱(unboxing)
基本數據類型和包裝類對象之間的轉換是自動的(auto)
11)In JDK 1.5, analyze the following code. (Choose all that apply.)
Line 1: Integer[ ] intArray = {1, 2, 3};
Line 2: int i = intArray[0] + intArray[1];
Line 3: int j = i + intArray[2];
Line 4: double d = intArray[0];
A)It is OK to mix an int value with an Integer object in an expression in Line 3.
B)Line 4 is OK. An int value from intArray[0] object is assigned to a double variable d.
C)It is OK to assign 1, 2, 3 to an array of Integer objects in JDK 1.5.
D)It is OK to automatically convert an Integer object to an int value in Line 2.
12)Which statements are most accurate regarding the following classes?
class A { private int i; protected int j; } class B extends A { private int k; protected int m; }
A)An object of B contains data fields k, m.
B)An object of B contains data fields j, k, m.
C)An object of B contains data fields j, m.
D)An object of B contains data fields i, j, k, m.
私有成員也會被繼承,但不能被訪問。
13)To divide BigDecimal b1 by b2 and assign the result to b1, you write ________. 17) ______
A)b2.divide(b1);
B)b1 = b2.divide(b1);
C)b1 = b1.divide(b2);
D)b1.divide(b2);
E)b1 = b2.divide(b1);
14)Which of the following statements convert a double value d into a string s? 21) ______
A)s = (new Double(d)).toString();
B)s = String.stringOf(d);
C)s = new Double(d).stringOf();
D)s = (Double.valueOf(s)).toString();
15)Which of the following statements is correct? (Choose all that apply.) 22) ______
A)Integer.parseInt(100);
B)Integer.parseInt("345", 8);
C)Integer.parseInt(100, 16);
D)Integer.parseInt("12", 2);
E)Integer.parseInt("100");
parseInt(String s)將一個數值字符串轉換從一個int值
16)Which of the following classes are immutable? (Choose all that apply.) 24) ______
A)Double B)Integer C)String D)BigInteger E)BigDecimal
17)To create an instance of BigDecimal for 454.45, use 25) ______
A)BigInteger("454.45"); B) new BigInteger(454.45);
C)BigInteger(454.45); D) new BigDecimal("454.45");
18)To add BigInteger b1 to b2, you write ________. (Choose all that apply.) 28) ______
A)b2 = b1.add(b2);
B)b2.add(b1);
C)b1 = b2.add(b1);
D)b2 = b2.add(b1);
E)b1.add(b2);
19)Which statements are most accurate regarding the following classes?
class A { private int i; protected int j; } class B extends A { private int k; protected int m; // some methods omitted }
A)In the class B, an instance method can only access j, k, m.
B)In the class B, an instance method can only access j, m.
C)In the class B, an instance method can only access i, j, k, m.
D)In the class B, an instance method can only access k, m.
子類只可以訪問父類中的非私有成員
20)Analyze the following code.
Number[ ] numberArray = new Integer[2]; numberArray[0] = new Double(1.5);
A)Since each element of numberArray is of the Number type, you cannot assign a Double object to it.
B)Since each element of numberArray is of the Number type, you cannot assign an Integer object to it.
C)At runtime, new Integer[2] is assigned to numberArray. This makes each element of numberArray an Integer object. So you cannot assign a Double object to it.
D)You cannot use Number as a data type since it is an abstract class.
21)BigInteger and BigDecimal are immutable 39) ______
A)true B) false
BigInteger 和 BigDecimal都是不可變類
22)Which of the following statements will convert a string s into a double value d? 40) ______
A)d = (new Double(s)).doubleValue();
B)d = Double.valueOf(s).doubleValue();
C)d = Double.parseDouble(s);
D)All of the above.