例题4: Which of the following lines of code will compile without error?
: l t4 m: ~6 \2 P A.
" W6 Z% b9 W5 b, R5 B int i=0; ) N- u) @. E) X4 D: @' R- k0 {+ a
if (i) {
, m0 c9 C8 c4 L: k# i, _6 d System.out.println(“Hi”); * B: j' {+ a- Z- y( D
} 9 f6 p3 N: v3 b. Y6 f9 M1 N0 i8 k
B. 2 ^ G9 g4 W: I4 m6 L5 d# X' x. d; t
boolean b=true;
! V T6 E/ X/ O3 ~; ]' w boolean b2=true; 6 |& A! s* }, |9 R+ T1 p1 ~
if(b==b2) {
) ?; c. f' x) ?( F+ v' o7 Z System.out.println(“So true”); 4 C5 a) s8 a/ |, W+ e) T. T
}
% `* i7 [9 R& G5 I* a4 e C. ( C8 g6 f' Z, z8 I6 D! v3 l O
int i=1;
) N7 t% b4 ]' G# {1 L4 z8 q& C int j=2;
7 \- n; X* `; {% r* B if(i==1|| j==2) " m( W$ g: ~9 ~
System.out.println(“OK”); 4 ?" w- O6 l; A& H- v G3 e
D. - j$ c% e# h5 F. J7 l
int i=1;
3 f( I5 Y3 h& P b$ l4 T int j=2; . Y+ Q' M# p6 O) R) A/ L0 u
if (i==1 &| j==2)
+ [4 A3 Q# i, t) p System.out.println(“OK”); * \; `& v* ~4 j; S; k% ]; K
解答:B, C ) k& i4 G2 O) o" E
点评:选项A错,因为if语句后需要一个boolean类型的表达式。逻辑操作有^、&、| 和 &&、||,但是“&|”是非法的,所以选项D不正确。 * h" ~" \ I' [& Z# P9 O- o
例题5: ! P; A5 x2 k4 {; ]8 Y1 O# X
Which two demonstrate a "has a" relationship? (Choose two) 1 X9 e5 U( N& C! x A) u
A. public interface Person { }3 V0 w3 `; x% t: S$ x
public class Employee extends Person{ }
! U( J' j) a1 Z8 f# K1 F2 Z! z2 s1 v6 {! Y* }
B. public interface Shape { }
3 I( p9 _/ q `6 ^& E* Q# B6 y3 A- K public interface Rectandle extends Shape { } 8 M a, U( n' {* `1 E8 \
C. public interface Colorable { }
. h; F! _ C9 ^0 Q) Z# } public class Shape implements Colorable 0 U7 P1 G J9 p1 w( D7 P+ j
{ } ' t. X. l& ?1 ]0 O9 h, L
D. public class Species{ }
0 z1 ^( C. o1 L8 b) e public class Animal{private Species species;}
2 F. T' g1 Z7 ^2 k. s E. interface Component{ }
B" h' M' m3 y$ i: W1 r class Container implements Component{ + l0 [( i- r+ @! Z8 @* }7 ^
private Component[] children; % _3 a6 z' `6 K/ p
} (www.Examw.com)
- w: Q/ T0 ?$ [$ Z 解答:D, E ' D- f& L3 m' A8 T$ t) ]
点评: 在Java中代码重用有两种可能的方式,即组合(“has a”关系)和继承(“is a”关系)。“has a”关系是通过定义类的属性的方式实现的;而“is a”关系是通过类继承实现的。本例中选项A、B、C体现了“is a”关系;选项D、E体现了“has a”关系。 $ \5 n& w u! b
例题6:
4 O& L0 C3 V" @: T# P Which two statements are true for the class java.util.TreeSet? (Choose two)
" r/ r: l9 P2 Q5 v, n 6 ~( W" b: k5 L4 _: R
A. The elements in the collection are ordered. ( O) r: ]. c4 {6 W' A) U
B. The collection is guaranteed to be immutable.
, D4 Q1 u* a3 ~" \# G8 A C. The elements in the collection are guaranteed to be unique. $ `8 A4 v, r; @( i1 }$ b
D. The elements in the collection are accessed using a unique key. 3 d7 H1 a! R; a9 W$ o
E. The elements in the collection are guaranteed to be synchronized
5 l* V( E0 b+ {; b' y 解答:A, C 3 C) [+ p8 K% s! }- j6 j
点评:TreeSet类实现了Set接口。Set的特点是其中的元素惟一,选项C正确。由于采用了树形存储方式,将元素有序地组织起来,所以选项A也正确。 |