21、Which of the following assignment is not correct? 6 o% n* H& n0 W0 \5 \5 H: {
A. float f = 11.1;
# @7 O; y$ x5 L& y9 l* X4 VB. double d = 5.3E12; o' L# ~9 [3 N% k: d4 J6 u4 ?" O
C. double d = 3.14159;
3 T8 x5 B" H7 w n! nD. double d = 3.14D.
9 Z4 p% T1 D" S2 E(a)
) [* X3 u9 E; k6 \题目:下面的哪些赋值语句是不对的。 % U4 \/ r1 p/ v" L, j
浮点数的赋值是带有小数点的数字缺省是double型的,如果在浮点数后面加f或者F则是float,后面加d或者D则是double,科学计数法形式的浮点数也是double型的,而double的精度比float高,将一个高精度的double赋值给一个低精度的float时需要进行强制类型转换,反之则不需要。
' [; ?! @; k) I" _% l22、Given the uncompleted code of a class: ( _7 m# k8 H7 d8 m
class Person { * u3 w) O! S* t3 T- T; l! b6 {
String name, department;
& e2 I# B4 ~: Qint age;
: R) e& s: u( d x( J3 i9 R, Lpublic Person(String n){ name = n; }
2 z9 S1 A8 G! d# _" npublic Person(String n, int a){ name = n; age = a; } - {$ r4 [" Z9 ?1 O; s& ~
public Person(String n, String d, int a) { 7 i4 |* w0 ?5 C( g
// doing the same as two arguments version of constructor
. J4 z5 R+ Z0 N) ^// including assignment name=n,age=a 5 h& o8 P8 s4 j' Q2 ]7 p$ ?0 n
department = d;
; s7 Z3 w% T8 R: Q0 @- \; `" I} + F5 q+ y) E f8 E3 [% H
}
2 t5 Y5 W6 d* PWhich expression can be added at the "doing the same as..." part of the constructor? 9 Y, A# c& [ n/ Z8 c: j
A. Person(n,a);
B: J7 k4 v' q, i/ [1 IB. this(Person(n,a));
" H' W" s- v; YC. this(n,a); & R& l6 X5 [. n6 [$ ~
D. this(name,age). 0 i) T" C2 z6 y8 s# E# o2 |) c$ _
(c) ( b/ J u$ j& g, r, c H( E) a
题目:给出下面的不完整的类代码: ; n5 d+ x/ r* n* j4 o! B0 V
…
, i4 i& z: { i6 P @. Q9 f' q) e下面的哪些表达式可以加到构造方法中的"doing the same as..."处? 8 D, w( k# t7 J% g; G
在同一个类的不同构造方法中调用该类的其它构造方法需要使用this(…)的形式,而且必须是在构造方法的第一行调用,这个和普通的方法重载调用的方式不同,普通的方法可以直接使用方法名加参数来调用,而且调用位置没有限制,因此答案A是不行的,B的语法就是错误的,D的错误在于在父类型的构造函数被调用前不能引用类的成员。构造方法是一个类对象实例化的起点(虽然严格来说首先执行的并不是构造方法的第一个语句,而是内存的分配),因此在构造方法中不能将成员作为参数引用。 23、Which of the following statements about variables and their scopes are true?
H9 }- \& ~& \! R, \A. Instance variables are member variables of a class.
/ V. b) P2 \8 AB. Instance variables are declared with the static keyword. 9 `+ n. e9 m+ N' O1 Q' |7 x+ C+ Y
C. Local variables defined inside a method are created when the method is executed. 8 h9 `0 ^8 Z' N1 c
D. Local variables must be initialized before they are used. # h# u9 {( l& [3 P
(acd)
2 }6 Q- t. F4 f- X7 ?题目:下面关于变量及其范围的陈述哪些是对的。
/ k: \6 x9 R% U% D9 X* CA. 实例变量是类的成员变量。
3 K+ n8 p( N. w3 i9 f0 Y* y; ?B. 实例变量用关键字static声明。
" t( G) u6 L! I# \- CC. 在方法中定义的局部变量在该方法被执行时创建 * h* t7 S- T2 ~8 ?( Y2 y
D. 局部变量在使用前必须被初始化。
- ]5 N* t. O4 U. \类中有几种变量,分别是:局部变量(英文可以为:local\automatic\temporary\stack variable)是定义在方法里的变量;实例变量(英文为:instance variable)是在方法外而在类声明内定义的变量,有时也叫成员变量;类变量(英文为:class variable)是用关键字static声明的实例变量,他们的生存期分别是:局部变量在定义该变量的方法被调用时被创建,而在该方法退出后被撤销;实例变量在使用new Xxxx()创建该类的实例时被创建,而其生存期和该类的实例对象的生存期相同;类变量在该类被加载时被创建,不一定要用new Xxxx()创建,所有该类的实例对象共享该类变量,其生存期是类的生存期。任何变量在使用前都必须初始化,但是需要指出的是局部变量必须显式初始化,而实例变量不必,原始类型的实例变量在该类的构造方法被调用时为它分配的缺省的值,整型是0,布尔型是false,而浮点型是0.0f,引用类型(类类型)的实例变量的缺省值是null(没有进行实际的初始化,对它的使用将引起NullPointException),类变量的规则和实例变量一样,不同的是类变量的初始化是在类被加载时。9 g# s9 f: c/ L
24、public void test() {
% _: K$ |* M: c- m- Dtry { oneMethod(); 4 `6 f; l/ r- [) Y% ?$ Z
System.out.println("condition 1"); 5 Z) Z. h7 X6 I& h1 N
} catch (ArrayIndexOutOfBoundsException e) { . e8 O8 r& K! B" E, ?' M
System.out.println("condition 2"); ) K: f7 m$ ]; K
} catch(Exception e) {
0 J* h% \ s: Q8 y* z- d% M4 F/ {( JSystem.out.println("condition 3"); , j* Q1 z8 o1 ?( P' [, k% p
} finally { " c# ?2 }3 M$ y4 b
System.out.println("finally");
. J* Y$ T( g+ a( `$ f& [6 J* w} 4 @6 T* U& W% D
} : P& [0 r* R. d: H0 R' l; y
Which will display if oneMethod run normally?
, f2 ]. W4 G- L- ?8 z/ }A. condition 1
5 V m+ @( Q9 f# U9 c( SB. condition 2 % r5 S" h$ z( Y5 f1 I) A1 U" k% b
C. condition 3
6 j* W6 O: W/ e. [3 S3 U# A/ xD. finally
7 b4 {1 [: I. d& {(ad) & K. X) m6 C* T, \- [
题目:在oneMethod()方法运行正常的情况下将显示什么?
0 t2 R1 Y' O2 h+ n. J, w* ?如果try块中的语句在执行时发生异常,则执行从该处中断而进入catch块,根据异常的类型进行匹配,最前面的优先进行匹配比较,只要该异常是catch中指定的异常的子类就匹配成功进而执行相应的catch中的内容,而finally块中的内容无论是否发生异常都将被执行。 7 u) T# |: @! h) X& D
1 s" h5 R0 ?) }& j' f
25、Given the following code: 6 r+ {, `4 y( O+ p% o7 u1 o
public class Test {
9 K- q1 }; r6 Pvoid printValue(int m){ J! V' P" V! u* `4 M/ T2 P" \* ^
do { System.out.println("The value is"+m); . x' q. x( ^3 h6 [- |. Z. P' M
} & Z3 W) d6 K4 r1 [; K; g
while( --m > 10 )
+ B3 H+ \: ~8 g$ x} % w1 o& c: i3 z" i8 [) |( A6 X2 Z; W
public static void main(String arg[]) {
- ^' a$ s0 [3 V; o9 C% Dint i=10; 7 h3 b- Q' [+ s# t/ O4 y
Test t= new Test();
$ O2 k8 }# k# R o, @7 f( J. u3 ct.printValue(i);
3 w8 q) o t+ D# Y}
& p; f$ J4 v5 w}
7 R; s! ~( x9 B' w$ oWhich will be output? - ]8 U7 r Y4 z
A. The value is 8
; E! @. J3 k# t; b' C, _$ zB. The value is 9 0 R: C- R9 Z" n) w% a
C. The value is 10 9 k5 j2 ~7 T. w
D. The value is 11
' K4 }- s- v+ m5 o" ^) b- k6 I* V(c)
: c5 l1 I' |& |' l3 }题目:给出下面的代码:
5 S8 d& O# N2 C8 N. o# o… ( y8 k, l3 m; @5 A
输出将是什么?
1 _7 w0 R' Z2 s$ I+ z此题考察的是do… while循环和 -- 操作符的知识,do…while最少被执行一次,在执行完do中的内容后判断while中的条件是否为true,如果为true的话就再执行do中的内容,然后再进行判断,以此类推直到while的判断为false时退出循环执行循环后面的内容,而?操作符的规则是在变量右边的 -- 将先进行运算,然后才是使变量的值减一,而在变量左边的是先将变量的值减一再运算。 + t# }% x2 p3 [3 B8 \6 c
26、Which of the following statements about declaration are true? 2 t- s( x( {" Y# s
A. Declaration of primitive types such as boolean, byte and so on does not allocate memory space for the variable. 4 f2 F0 J1 A$ M r/ }
B. Declaration of primitive types such as boolean, byte and so on allocates memory space for the variable. 3 m8 \; F/ a9 v- ~% G
C. Declaration of nonprimitive types such as String, Vector and so on does not allocate memory space for the object.
) x5 ?% c" G6 r+ A! w6 a, \D. Declaration of nonprimitive types such as String, Vector ans so on allocates memory space for the object. M. S4 i9 d! w; u
(ad) * G# V8 \+ S% U
题目:下面的有关声明的哪些叙述是对的。
# U2 W5 P8 O9 o, G/ P9 m! zA. 对原始数据类型例如boolean,byte的变量的声明不会为该变量分配内存空间。
, W% ~/ {: S+ I# o1 V \B. 对原始数据类型例如boolean,byte的变量的声明将为之分配内存空间。 - m7 b0 r2 N0 w- B( B# _4 G2 O
C. 非原始数据类型例如String,Vector的变量的声明不会为该对象分配内存。
' w3 W8 a: ?$ I$ u8 ~D. 非原始数据类型例如String,Vector的变量的声明会为该对象分配内存。 & E2 F, b$ T: c8 l, ~
对原始数据类型的变量的声明将为之分配内存并赋予一个缺省值,参见23题的叙述,而非原始数据类型的变量必须用new Xxxx()分配内存及初始化。但是严格来讲此题的答案有待确定,因为只有原始类型的实例变量和类变量的声明在类对象被创建/类被加载时完成内存的自动分配,而原始类型的局部变量必须显式初始化,从这点来看原始类型的局部变量没有被自动分配内存,SL275中只提出了非原始数据类型的变量必须使用new Xxxx()完成内存的分配而没有指出原始数据类型的变量是否在声明时即自动进行内存分配,而从局部变量不能在显式初始化前使用这点来看在声明时没有进行内存分配。因此答案a的正确性还有待官方的确定。 |