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)方法。
不可变对象的内容不能修改。
不可变对象中的对象类型属性也必须是不可变的。