首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 认证考试 > JAVA认证 >

SCJP认证套题解析之二

2008-10-05 
21、which of the following assignment is not correct? a. float f 11.1 b. double d 5.3e ...
21、which of the following assignment is not correct?
a. float f = 11.1;
b. double d = 5.3e12;
c. double d = 3.14159;
d. double d = 3.14d.
(a)
题目:下面的哪些赋值语句是对的。
浮点数的赋值是带有小数点的数字缺省是double型的,如果在浮点数后面加f或者f则是float,后面加d或者d则是double,科学计数法形式的浮点数也是double型的,而double的精度比float高,将一个高精度的double赋值给一个低精度的float时需要进行强制类型转换,反之则不需要。
22、given the uncompleted code of a class:
class person {
string name, department;
int age;
public person(string n){ name = n; }
public person(string n, int a){ name = n; age = a; }
public person(string n, string d, int a) {
// doing the same as two arguments version of constructor
// including assignment name=n,age=a
department = d;
}
}
which expression can be added at the "doing the same as..." part of the constructor?
a. person(n,a);
b. this(person(n,a));
c. this(n,a);
d. this(name,age).
(c)
题目:给出下面的不完整的类代码:

下面的哪些表达式可以加到构造方法中的"doing the same as..."处?
在同一个类的不同构造方法中调用该类的其它构造方法需要使用this(…)的形式,而且必须是在构造方法的第一行调用,这个和普通的方法重载调用的方式不同,普通的方法可以直接使用方法名加参数来调用,而且调用位置没有限制,因此答案a是不行的,b的语法就是错误的,d的错误在于在父类型的构造函数被调用前不能引用类的成员。构造方法是一个类对象实例化的起点(虽然严格来说首先执行的并不是构造方法的第一个语句,而是内存的分配),因此在构造方法中不能将成员作为参数引用。

23、which of the following statements about variables and their scopes are true?

a. instance variables are member variables of a class.
b. instance variables are declared with the static keyword.
c. local variables defined inside a method are created when the method is executed.
d. local variables must be initialized before they are used.
(acd)
题目:下面关于变量及其范围的陈述哪些是对的。
a. 实例变量是类的成员变量。
b. 实例变量用关键字static声明。
c. 在方法中定义的局部变量在该方法被执行时创建
d. 局部变量在使用前必须被初始化。
类中有几种变量,分别是:局部变量(英文可以为:local\automatic\temporary\stack variable)是定义在方法里的变量;实例变量(英文为:instance variable)是在方法外而在类声明内定义的变量,有时也叫成员变量;类变量(英文为:class variable)是用关键字static声明的实例变量,他们的生存期分别是:局部变量在定义该变量的方法被调用时被创建,而在该方法退出后被撤销;实例变量在使用new xxxx()创建该类的实例时被创建,而其生存期和该类的实例对象的生存期相同;类变量在该类被加载时被创建,不一定要用new xxxx()创建,所有该类的实例对象共享该类变量,其生存期是类的生存期。任何变量在使用前都必须初始化,但是需要指出的是局部变量必须显式初始化,而实例变量不必,原始类型的实例变量在该类的构造方法被调用时为它分配的缺省的值,整型是0,布尔型是false,而浮点型是0.0f,引用类型(类类型)的实例变量的缺省值是null(没有进行实际的初始化,对它的使用将引起nullpointexception),类变量的规则和实例变量一样,不同的是类变量的初始化是在类被加载时。