Java题库——Chapter8 对象和类
1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______
A)A data field B) An object
C)A method D) A class
2)________ is a construct that defines objects of the same type. 2) _______
A)A method B) A data field
C)A class D) An object
3)An object is an instance of a ________. 3) _______
A)data B) method C) class D) program
对象是类的一个实例
4)The keyword ________ is required to declare a class. 4) _______
A)private B) class C)public D) All of the above.
5)________ is invoked to create an object. 5) _______
A)A constructor
B)A method with the void return type
C)The main method
D)A method with a return type
6)Which of the following statements are true? (Choose all that apply.) 6) _______
A)Constructors must have the same name as the class itself.
B)Constructors do not have a return type, not even void.
C)Constructors are invoked using the new operator when an object is created.
D)A default no-arg constructor is provided automatically if no constructors are explicitly declared in the class.
E)At least one constructor must always be defined explicitly. 必须始终显式定义至少一个构造函数。
构造函数是在创建一个对象时由new 操作符调用的。构造函数可以不显式定义
7) Analyze the following code: (Choose all that apply.)
public class Test {
public static void main(String[ ] args) {
A a = new A();
a.print();
}
}
class A {
String s;
A(String s) {
this.s = s;
}
void print() {
System.out.println(s);
}
}
A)The program has a compilation error because class A does not have a default constructor.
B)The program has a compilation error because class A is not a public class.
C)The program would compile and run if you change A a = new A() to A a = new A("5").
D)The program compiles and runs fine and prints nothing.
当且仅当类中没有明确定义任何构造函数的时候才会自动提供一个缺省构造函数。该类中有了构造函数,不会再提供缺省构造函数了。
8)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);
}
}
A)The program has a compilation error because TempClass does not have a constructor with an int argument.
B)The program has a compilation error because TempClass does not have a default constructor.
C)The program compiles and runs fine.
D)The program compiles fine, but it does not run because class C is not public.
注意这里的TempClass的TempClass函数并不是构造函数,该函数有void返回值!!!
9)Given the declaration Circle x = new Circle(), which of the following statement is most accurate?
A)You can assign an int value to x.
B)x contains an int value.
C)x contains a reference to a Circle object.
D)x contains an object of the Circle type.
10)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);
}
}
A)The program has a runtime NullPointerException because test is null while executing test.x.
B)The program has a compile error because you cannot create an object from the class that defines the object.
C)The program has a compile error because x has not been initialized.
D)The program has a compile error because test is not initialized.
E)The program has a compile error because Test does not have a default constructor.
11)The default value for data field of a boolean type, numeric type, object type is ________, respectively. 11) ______
A)false, 0, null B)true, 1, null C)true, 1, Null D)false, 1, null E)true, 0, null
12)Which of the following statements are true? (Choose all that apply.) 12) ______
A)You may assign an int value to a reference variable.
B)Data fields have default values.
C)Local variables do not have default values.
D)A variable of a primitive(原始的、基本的) type holds a value of the primitive type.
E)A variable of a reference type holds a reference to where an object is stored in the memory.
D基本数据类型的变量含有基本数据类型的值
13)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);
}
}
A)The program has no compile errors but will get a runtime error because radius is not initialized.
B)The program has compile errors because the variable radius is not initialized.
C)The program has a compile error because a constant PI is defined inside a method.
D)The program compiles and runs fine.
14)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 = new Test();
System.out.println(test.x);
}
}
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 System.out.println method cannot be invoked from the constructor.
C)The program has a compile error because x has not been initialized.
D)The program has a compile error because Test does not have a default constructor.
将这道题变化一下
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);
}
}
a. The program has a compile error because test is not initialized.
b. The program has a compile error because x has not been initialized.
c. The program has a compile error because you cannot create an object from the class that defines the object.
d. The program has a compile error because Test does not have a default constructor.
e. The program has a runtime NullPointerException because test is null while executing test.x.
Key:e
现在由于test=null,造成了空指针异常
15)Suppose TestCircle and Circle in Listing 7.1 in the textbook are in two separate files named TestCircle.java and Circle.java, respectively. What is the outcome of compiling TestCircle.java and then Circle.java? 15) ______
A)Neither compiles successfully.
B)Only TestCircle.java compiles.
C)Only Circle.java compiles.
D)Both compile fine.
16)Which of the following statement is most accurate? (Choose all that apply.) 16) ______
A)An object may contain other objects.
B)An object may contain the references of other objects.
C)A reference variable is an object.
D)A reference variable refers to an object.
一个对象中可能包含其他对象的引用,一个引用变量指向一个对象。
17)The java.util.Date class is introduced in this section. Which of the following code creates an object of the Date class? (Choose all that apply.)
A:
public class Test {
public Test() {
new java.util.Date();
}
}
B:
public class Test {
public Test() {
java.util.Date date = new java.util.Date();
}
}
A)A B) B C) neither
18)Which of the following statements are correct? (Choose all that apply.) 18) ______
A)The nextInt() method in the Random class returns the next random int value.
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)If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical(相同的).
创建一个Random对象时,必须指定一个种子或使用默认的种子。种子是一个用于初始化随机数字生成器的数字。无参构造方式使用当前已经逝去的时间作为种子,创建一个Random对象。如果这两个Random对象有相同的种子,那么它们将产生相同的数列。
19)How many JFrame objects can you create and how many can you display? 19) ______
A)one B) three C)two D) unlimited
20)Variables that are shared by every instances of a class are ________. 20) ______
A)public variables B) class variables C)private variables D) instance variables
21)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}
A)in line 4 B) in both line 4 and line 8 C)in line 8 D) none
22)A method that is associated with an individual object is called ________. 22) ______
A)an object method B) an instance method C)a static method D) a class method
调用与单个对象关联的方法称为实例方法,只有在实例被创建后才能使用。
23)To declare a constant MAX_LENGTH as a member of the class, you write 23) ______
A)static double MAX_LENGTH = 99.98;
B)final static float MAX_LENGTH = 99.98;
C)final static double MAX_LENGTH = 99.98;
D)final static MAX_LENGTH = 99.98;
E)final double MAX_LENGTH = 99.98;
类中的常量是被类的所有对象所共享的。注意要有关键词final static和变量数据类型double
24)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++;
}
}
A)The code prints n is 2.
B)The code has a compile error because xMethod does not return a value.
C)The code prints n is 1.
D)The code prints n is 3.
E)The code has a compile error because xMethod is not declared static.
该类由于没有创建实例,必须要用静态方法 xMethod前需要加static
25)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++;
}
}
A)f2.i is 1 f2.s is 1 B) f2.i is 1 f2.s is 2 C)f2.i is 2 f2.s is 1 D) f2.i is 2 f2.s is 2
注意到s是静态变量,被该类的所有对象所共享,而main方法也是一个静态方法。
26)What is the printout of the third 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++;
}
}
A)f3.i is 3 f3.s is 3 B)f3.i is 1 f3.s is 3 C)f3.i is 1 f3.s is 1 D)f3.i is 3 f3.s is 1 E)f3.i is 1 f3.s is 2
27)What code may be filled in the blank without causing syntax or runtime errors:
public class Test {
java.util.Date date;
public static void main(String[ ] args) {
Test test = new Test();
System.out.println(________);
}
}
A)date B) test.date C)test.date.toString() D) date.toString()
不能使用toString()的原因是没有创建一个指向date对象的引用。
28)To prevent a class from being instantiated(实例化), ________ 28) ______
A)use the public modifier on the constructor.
B)don't use any modifiers on the constructor.
C)use the static modifier on the constructor.
D)use the private modifier on the constructor.
为了防止类被实例化,要使用private修饰构造函数。
29)Analyze the following code:
public class Test {
public static void main(String args[ ]) {
NClass nc = new NClass();
nc.t = nc.t++;
}
}
class NClass {
int t;
private NClass() {
}
}
A)The program has a compilation error because the NClass class has a private constructor.
B)The program compiles and runs fine.
C)The program does not compile because the parameter list of the main method is wrong.
D)The program compiles, but has a runtime error because t has no initial value.
接着上面的题,使用private修饰构造函数,该类就无法被实例化了!
30)Analyze the following code:
public class Test {
private int t;
public static void main(String[ ] args) {
int x;
System.out.println(t);
}
}
A)The variable t is not initialized and therefore causes errors.
B)The variable t is private and therefore cannot be accessed in the main method.
C)t is non-static and it cannot be referenced in a static context in the main method.
D)The program compiles and runs fine.
E)The variable x is not initialized and therefore causes errors.
31)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);
}
}
A)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.
B)You cannot create a self-referenced object; that is, foo is created inside the class Foo.
C)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.
D)Since x is private, it cannot be accessed from an object foo.
不创建对象,将无法访问x,因为x不是static类型
32)Which of the following statements are true? (Choose all that apply.) 32) ______
A)Encapsulating data fields makes the program easy to maintain.
B)Encapsulating data fields makes the program short.
C)Encapsulating data fields helps prevent programming errors.
D)Use the private modifier to encapsulate data fields.
封装数据字段有助于防止编程错误
33)Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be? 33) ______
A)public void getFinished()
B)public void isFinished()
C)public boolean isFinished()
D)public boolean getFinished()
34)Which is the advantage of encapsulation? 34) ______
A)It changes the implementation without changing a class's contract and causes no consequential changes to other code.
B)Only public methods are needed.
C)Making the class final causes no consequential changes to other code.
D)It changes a class's contract without changing the implementation and causes no consequential changes to other code.
在不更改类契约的情况下更改实现,并且不会对其他代码造成相应的更改。
35)When invoking a method with an object argument, ________ is passed. 35) ______
A)the contents of the object
B)the reference of the object
C)a copy of the object
D)the object is copied, then the reference of the copied object
36)What is the value of myCount.count 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;
}
}
A)101 B) 99 C) 98 D) 100
37)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;
}
}
A)99 B) 100 C) 98 D) 0 E) 101
注意这里time传递给方法进行的是值传递!方法结束后不会改变原来的值。
38)What is the output of the following program?
import java.util.Date;
public class Test {
public static void main(String[ ] args) {
Date date = new Date(1234567);
m1(date);
System.out.print(date.getTime() + " ");
m2(date);
System.out.println(date.getTime());
}
public static void m1(Date date) {
date = new Date(7654321);
}
public static void m2(Date date) {
date.setTime(7654321);
}
}
A)7654321 1234567 B) 1234567 1234567 C)7654321 7654321 D) 1234567 7654321
39)Given the declaration Circle[ ] x = new Circle[10], which of the following statement is most accurate? 39) ______
A)x contains a reference to an array and each element in the array can hold a Circle object.
B)x contains an array of ten objects of the Circle type.
C)x contains a reference to an array and each element in the array can hold a reference to a Circle object.
D)x contains an array of ten int values.
40)Assume java.util.Date[ ] dates = new java.util.Date[10], which of the following statements are true? (Choose all that apply.) 40) ______
A)dates = new Date() is fine, which creates a new Date object and assigns to dates.
B)dates is null.
C)dates = new java.util.Date[5] is fine, which assigns a new array to dates.
D)dates[0] is null.
Java题库——Chapter8 对象和类的更多相关文章
- java中String是对象还是类?详解java中的String
有很多人搞不懂对象和类的定义.比如说java中String到底是对象还是类呢? 有人说String 既可以说是类,也可以说是对象. 其实他这么说也没问题, 类和对象其实都是一个抽象的概念. 我们可以把 ...
- Java基础语法<七> 对象与类
笔记整理 来源于<Java核心技术卷 I > <Java编程思想> 1. 类之间的关系 依赖 users– a 是一种最明显的.最常见的关系.如果一个类的方法操作另一个类的对象 ...
- Java 读书笔记 (二) 对象和类
Java 作为一种面向对象语言,支持以下基本概念: 多态 继承 封闭 抽象 类 对象 实例 方法 重载 对象: 是类的一个实例,有状态和行为.以人为例,黄种人.白种人.黑种人为类,每一个具体的人为类的 ...
- Java基础语法<七> 对象与类 封装
笔记整理 来源于<Java核心技术卷 I > <Java编程思想> 1. 类之间的关系 1.1 依赖 users– a 是一种最明显的.最常见的关系.如果一个类的方法操作另一个 ...
- Java入门之:对象和类
Java对象和类 Java作为一种面向对象语言,支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 本节我们重点研究对象和类的概念: 对象: 对象是类的一个实例,有状态和行为.例如 ...
- Java题库——Chapter13抽象类和接口
)What is the output of running class Test? public class Test { public static void main(String[ ] arg ...
- Java题库——Chapter17 二进制I/0
Introduction to Java Programming 的最后一章,完结撒花!Chapter 17 Binary I/O Section 17.2 How is I/O Handled in ...
- Java题库——Chapter14 JavaFX基础
Chapter 14 JavaFX Basics Section 14.2 JavaFX vs Swing and AWT1. Why is JavaFX preferred?a. JavaFX is ...
- Java题库——Chapter12 异常处理和文本IO
异常处理 1)What is displayed on the console when running the following program? class Test { public stat ...
随机推荐
- Java堆的结构是什么样子的?什么是堆中的永久代(Perm Gen space)?
JVM的堆是运行时数据区,所有类的实例和数组都是在堆上分配内存.它在JVM启动的时候被创建.对象所占的堆内存是由自动内存管理系统也就是垃圾收集器回收. 堆内存是由存活和死亡的对象组成的.存活的对象是应 ...
- Unity中文API参考手册
转载请标明原文地址:http://www.cnblogs.com/zhangyukof/p/6835582.html Unity5中文脚本手册 网页版 Unity API 执行顺序: Unity5中 ...
- 谈谈redis的特性以及使用场景
ok?先从String开始讲: String: 这是最简单的类型,就是普通的get和set,做简单的KV缓存. 但是在真实的开发环境中,很多men可能会吧很多复杂的结构也统一转成String去储存使用 ...
- HttpRunner学习6--使用parameters参数化
前言 在使用HttpRunner测试过程中,我们可能会遇到这种场景: 账号登录功能,需要输入用户名和密码,设计测试用例后有 N 种组合情况 如果测试组合比较少,比如只有2个,那我们直接在YAML脚本中 ...
- 使用node+express+mongodb实现用户注册、登录和验证功能
无论是手机端还是pc端,几乎都包含登录注册方面功能,今天就使用node+express+mongodb实现一套登录注册功能,这里需要自己去安装MongoDB环境,如果没有安装可以看这篇关于MongoD ...
- 利用FOR XML PATH行转列(根据某字段分组,多行数据转成一行,并用逗号隔开)
CREATE TABLE #TEST(A VARCHAR(10) NULL,B VARCHAR(MAX) NULL) INSERT INTO #TESTSELECT 'A','A001'UNION A ...
- java 读取 yaml 文件
做 java 项目用的最多的配置文件就是 properites 或者 xml, xml 确实是被用烂了,Struts, Spring, Hibernate(ssh) 无一不用到 xml.相比厚 ...
- 系统优化——建立linux回收站机制
前言: linux系统下的rm是不可挽回的,命令设计本身没有问题,问题在于我们通常非常的自信,执行的时候喜欢rm -rf,这样的话就非常危险了,在执行的时候如果执行命令不对,甚至是执行的目录不对,那么 ...
- 使用 ASP.NET Core MVC 创建 Web API——响应数据的内容协商(七)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 使用 ASP.NET Core MVC 创建 Web API(二) 使 ...
- Axure导出的原型无法在谷歌浏览器浏览
1.下载crx后缀的文件. 2.修改crx后缀名为rar的压缩文件 3.解压刚才的rar文件 4.打开谷歌浏览器右上角的三个点 更多工具==>扩展程序 选择刚才的解压文件夹. 上面的图表示安装成 ...