面向对象程序设计(Java) chapter9

An immutable class cannot have _______.

C
单选题 (1 分) 1 分
A.
static data fields

B.
no-arg constructors

C.
public data fields

D.
public constructors

E.
private data fields
2.
A source code file may contain several classes.

A
判断题 (1 分) 1 分
A.
true

B.
false
4.
You can declare variables of the same name in a method even though they are in the same block.
B
判断题 (1 分) 0 分
A.
true

B.
false
5.
All data fields in an object have default values(默认值).
A
判断题 (1 分) 1 分
A.
true

B.
false
6.
What is the printout of the second println statement in the main method?

public class Foo {

int i;

static int s;

public static void main(String[] args) {

Foo f1 = new Foo();

System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s);

Foo f2 = new Foo();

System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s);

Foo f3 = new Foo();

System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s);

}

public Foo() {

i++;

s++;

}

}

D
单选题 (1 分) 0 分
A.
f2.i is 2 f2.s is 1

B.
f2.i is 2 f2.s is 2

C.
f2.i is 1 f2.s is 1

D.
f2.i is 1 f2.s is 2
9.You can declare variables of the same name in a method if they are in non-nesting blocks.
判断题 (1 分)
B
A.false
B.true
10.
What is the value of times displayed?

public class Test {

public static void main(String[] args) {

Count myCount = new Count();

int times = 0;

for (int i=0; i<100; i++)

  increment(myCount, times);

System.out.println(

  "myCount.count = " + myCount.count);

System.out.println("times = "+ times);

}

public static void increment(Count c, int times) {

c.count++;

times++;

}

}

class Count {

int count;

Count(int c) {

count = c;

}

Count() {

count = 1;

}

}
单选题 (1 分)
D
A.98

B.99

C.100

D.0

E.101
12.
Analyze the following code:

public class Test {

private int t;

public static void main(String[] args) {

int x;

System.out.println(t);

}

}
单选题 (1 分)
A
A.t is non-static and it cannot be referenced in a static context in the main method.

B.The program compiles and runs fine.

C.The variable x is not initialized and therefore causes errors.

D.The variable t is not initialized and therefore causes errors.

E.The variable t is private and therefore cannot be accessed in the main method.
13.Which of the following statements are true?
多选题 (2 分)
ABCD
A.Constructors(构造函数) must have the same name as the class itself.

B.Constructors are invoked using the new operator when an object is created.

C.Constructors do not have a return type, not even void.

D.Multiple constructors can be defined in a class.
14.
Analyze the following code:

public class Test {

public static void main(String[] args) {

double radius;

final double PI= 3.15169;

double area = radius * radius * PI;

System.out.println("Area is " + area);

}

}
单选题 (1 分)
A
A.The program has compile(编译) errors because the variable radius is not initialized.

B.The program has a compile error because a constant PI is defined inside a method.

C.The program has no compile errors but will get a runtime error because radius is not initialized.

D.The program compiles and runs fine.
15.A static method in a class can access(访问) the class variables in the same class.
判断题 (1 分)
B
A.false
B.true
16.
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);

}

}
单选题 (1 分)
A
A.k is 2

B.k is 1

C.k is 0

D.k is 3
19.Array variable(数组变量) are reference variables(引用变量).
判断题 (1 分)
B
A.false

B.true
23.Which of the following statements are true?
多选题 (2 分)
CD
A.Every class has a default constructor(默认构造函数).

B.At least one constructor must always be defined explicitly(明确的).

C.The default constructor is a no-arg(无参) constructor.

D.A default constructor is provided automatically if no constructors are explicitly declared in the class.
24.Given the declaration Circle x = new Circle(), which of the following statement is most accurate.
单选题 (1 分)
C
A.You can assign an int value to x.

B.x contains an object of the Circle type.

C.x contains a reference(引用) to a Circle object.

D.x contains an int value.
25.
Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is _________ in the class.

public static void main(String[] args) {

xMethod();

}
单选题 (1 分)
B
A.a static method or an instance method

B.a static method

C.an instance method
26.A method that is associated(关联) with an individual(单独的) object is called __________.
单选题 (1 分)
B
A.an object method(对象方法)

B.an instance method(实例方法)

C.a static method(静态方法)

D.a class method(类方法)
27.Variables that are shared by every instances of a class are __________.
单选题 (1 分)
A
A.class variables

B.private variables

C.public variables

D.instance variables
28.The default value for data field of a boolean type, numeric type, object type is ___________, respectively.
单选题 (1 分)
E
A.true, 0, null

B.true, 1, null

C.false, 1, null

D.true, 1, Null

E.false, 0, null
29.Suppose TestSimpleCircle and SimpleCircle in Listing 9.1 are in two separate files named TestSimpleCircle.java and SimpleCircle.java, respectively. What is the outcome of compiling TestsimpleCircle.java and then SimpleCircle.java?
单选题 (1 分)
D
A.Neither compiles successfully.

B.Only TestSimpleCircle.java compiles.

C.Only SimpleCircle.java compiles.

D.Both compile fine.
30.
Analyze the following code.

public class Test {

public static void main(String[] args) {

int n = 2;

xMethod(n);           

System.out.println("n is " + n);

}

void xMethod(int n) {

n++;

}

}
单选题 (1 分)
B
A.The code has a compile error because xMethod does not return a value.

B.The code has a compile error because xMethod is not declared static.

C.The code prints n is 1.

D.The code prints n is 2.

E.The code prints n is 3.
32.
Analyze the following code and choose the best answer:

public class Foo {

private int x;

public static void main(String[] args) {

Foo foo = new Foo();

System.out.println(foo.x);

}

}
单选题 (1 分)
D
A.Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code.

B.You cannot create a self-referenced object; that is, foo is created inside the class Foo.

C.Since x is private, it cannot be accessed from an object foo.

D.Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code.
33.A static method in a class can access(访问) the instance variables in the same class.
判断题 (1 分)
B
A.true

B.false
34.Which of the following statements are true about an immutable(不可变) object?
多选题 (2 分)
ABDE
A.The contents(内容) of an immutable object cannot be modified(修改).

B.An immutable object contains no mutator(修改器) methods.

C.All properties(属性) of an immutable object must be of primitive((原始) types.

D.An object type property in an immutable object must also be immutable.

E.All properties of an immutable object must be private.
36.Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate?
单选题 (1 分)
C
A.x contains a reference to an array and each element in the array can hold a Circle object.
(x 包含对数组的引用,并且数组中的每个元素都可以包含一个 Circle 对象)
B.x contains an array of ten int values.
(x 包含一个由十个 int 值组成的数组。)
C.x contains a reference to an array and each element in the array can hold a reference to a Circle object.
(x 包含对数组的引用,并且数组中的每个元素都可以包含对 Circle 对象的引用。)
D.x contains an array of ten objects of the Circle type.
(x 包含一个由十个 Circle 类型的对象组成的数组)
37.Which of the following statement is most accurate?
多选题 (2 分)
AB
A reference variable refers to an object.

B.An object may contain the references of other objects.

C.An object may contain other objects.

D.A reference variable is an object
38.You can declare two variables with the same name in __________.
单选题 (1 分)
B
A.two nested blocks in a method (two nested blocks means one being inside the other)

B.different methods in a class

C.a method one as a formal parameter(形式参数) and the other as a local variable(局部变量)

D.a block
42.
You should add the static keyword in the place of ? in Line ________ in the following code:

1 public class Test {

2 private int age;

3

4 public ? int square(int n) {

5 return n * n;

6 }

7

8 public ? int getAge() {

9 }

10}
单选题 (1 分)
D
A.in both line 4 and line 8

B.none

C.in line 8

D.in line 4
43.
Analyze the following code:

class Circle {

private double radius;

public Circle(double radius) {

radius = radius;

}

}
单选题 (1 分)
B
A.The program has a compilation error because it does not have a main method.

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 you cannot assign radius to radius.

D.The program does not compile because Circle does not have a default constructor.
45.To prevent a class from being instantiated(实例化), _____________________
单选题 (1 分)
D
A.use the public modifier on the constructor.

B.use the static modifier on the constructor.

C.don’t use any modifiers on the constructor.

D.use the private modifier on the constructor.
46.
What is wrong in the following code?

class TempClass {

int i;

public void TempClass(int j) {

int i = j;

}

}

public class C {

public static void main(String[] args) {

TempClass temp = new TempClass(2);

}

}
单选题 (1 分)
B
A.The program has a compilation error because TempClass does not have a default constructor.

B.The program has a compilation error because TempClass does not have a constructor with an int argument.

C.The program compiles and runs fine.

D.The program compiles fine, but it does not run because class C is not public.
47.A constructor can access ___________.
多选题 (2 分)
ACD
A.A static variable

B.A local variable defined in any method

C.A public instance variable

D.A private instance variable
48.Java assigns a default value(默认值) to a local variable in a method if the variable is not initialized(初始化).
判断题 (1 分)
B
A.true

B.false
49.To declare a constant MAX_LENGTH as a member of the class, you write
单选题 (1 分)
B
A.final double MAX_LENGTH = 99.98;

B.final static double MAX_LENGTH = 99.98;

C.final static float MAX_LENGTH = 99.98;

D.final static MAX_LENGTH = 99.98;

E.static double MAX_LENGTH = 99.98;
51.Which of the following statements are correct?
多选题 (2 分)
ABCD
A.If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical.

B.The nextDouble() method in the Random class returns the next random double value.

C.When creating a Random object, you have to specify the seed or use the default seed.

D.The nextInt() method in the Random class returns the next random int value.
52.
The java.util.Date class is introduced in this section. Analyze the following code and choose the best answer:

Which of the following code in A or B, or both creates an object of the Date class:

A:

public class Test {

public Test() {

new java.util.Date();

}

}

B:

public class Test {

public Test() {

java.util.Date date = new java.util.Date();

}

}
多选题 (2 分)
BC
A.Neither

B.B.

C.A.
54.If the parameter(参数) is an object, both formal parameter(形参) and actual parameter(实参) reference to the same object. 回答错误
判断题 (1 分)
A
A.true

B.false
56.All local variables(局部变量) in a method have default values.
判断题 (1 分)
A
A.false

B.true
57.
Analyze the following code.

public class Test {

int x;

public Test(String t) {

 System.out.println("Test");

}

public static void main(String[] args) {

Test test = null;

System.out.println(test.x);

}

}
单选题 (1 分)
C
A.The program has a compile error because you cannot create an object from the class that defines the object.

B.The program has a compile error because test is not initialized.

C.The program has a runtime NullPointerException because test is null while executing test.x.

D.The program has a compile error because Test does not have a default constructor.

E.The program has a compile error because x has not been initialized
59.___________ can be accessed from any instance method in the class.
多选题 (2 分)
BC
A.A local variable

B.A static variable

C.An instance variable
60.You can declare a local variable in a method that has same name as an instance variable in the class.
判断题 (1 分)
B
A.false

B.true
62.The internal state of an immutable class cannot be changed. String is an immutable class.
判断题 (1 分)
A
A.true

B.false
63.When invoking a method with an object argument, ___________ is passed.
单选题 (1 分) B
A.the contents of the object

B.the reference of the object

C.the object is copied, then the reference of the copied object

D.a copy of the object
65.You cannot use modifiers(修饰符) on local variables inside a method except final.
判断题 (1 分)
A
A.true

B.false
70.Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true?
多选题 (2 分)
AC
A.dates[0] is null.

B.dates = new Date() is fine, which creates a new Date object and assigns to dates.

C.dates = new java.util.Date[5] is fine, which assigns a new array to dates.

D.dates is null.
71.The order(顺序) of methods in a class is immaterial(无关紧要).
判断题 (1 分)
B
A.false

B.true
74.The default value null is assigned to a data member of object type, even though the data member is not created yet.
判断题 (1 分)
B
A.false

B.true
76.__________ represents an entity in the real world that can be distinctly identified.
单选题 (1 分)
A
A.An object

B.A class

C.A method

D.A data field
78.Which of the following statements are true?
多选题 (2 分)
ABD
A.Encapsulating(封装) data fields makes the program easy to maintain(维护).

B.Encapsulating data fields helps prevent programming errors.

C.Encapsulating data fields makes the program short.

D.Use the private modifier to encapsulate data fields.
79.Which of the following statements are true?
多选题 (2 分)
ABCD
A.A variable of a reference type holds a reference to where an object is stored in the memory.
引用类型的变量保存对对象在内存中存储位置的引用
B.A variable of a primitive type holds a value of the primitive type.
原始类型的变量保存原始类型的值
C.Data fields have default values.

D.Local variables do not have default values.
80.Which is the advantage of encapsulation?
单选题 (1 分)
B
A.Making the class final causes no consequential changes to other code.

B.It changes the implementation without changing a class’s contract and causes no consequential changes to other code.

C.Only public methods are needed.

D.It changes a class’s contract without changing the implementation and causes no consequential changes to other code.
82.You can create an instance of the Math class.
判断题 (1 分)
B
A.true

B.false
83.Which of the following are properties(属性) of a constructor(构造函数)?
多选题 (2 分)
ABC
A.
A constructor is called using the new operator.

B.
A constructor must have the same name as the class.

C.
Constructors may be overloaded(重载).
84.
Analyze the following code:

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");

}

}
多选题 (2 分)
BD
A.this.i may be replaced by i.

B.this(1) must be called before System.out.println(“Default constructor”).

C.this(1) must be replaced by this(1.0).

D.this.t() may be replaced by t().

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值