例题7:
2 z# N6 {% S& H3 H True or False: Readers have methods that can read and return floats and doubles. 3 I0 S! q. z$ n, a' W9 x; c
A. Ture
0 U* A9 H8 i) U2 z) D/ Z% o) e B. False
" s6 n( F! Q: Y7 h z B5 _' {' Z 解答:B ) f& t$ q7 t+ V% E
点评: Reader/Writer只处理Unicode字符的输入输出。float和double可以通过stream进行I/O. ' j( g+ t/ ~) n
例题8:
6 W) b9 ?/ w6 s9 R What does the following paint() method draw?
" [2 }' U5 q& \ 1. public void paint(Graphics g) {
) u( ~( k \' |, Y3 j 2. g.drawString(“Any question”, 10, 0); ! C. g! ~0 w7 z( T
3. } 4 G: a' A/ ~+ Q8 P
A. The string “Any question?”, with its top-left corner at 10,0 ) d. E1 u: R, Q+ K6 W7 Y& O
B. A little squiggle coming down from the top of the component. t p3 O! q# x' Y% O9 b# S
解答:B
' |1 _+ U+ {7 d 点评:drawString(String str, int x, int y)方法是使用当前的颜色和字符,将str的内容显示出来,并且最左的字符的基线从(x,y)开始。在本题中,y=0,所以基线位于最顶端。我们只能看到下行字母的一部分,即字母‘y’、‘q’的下半部分。
% q, M8 n+ ~3 [- N 例题9: . V7 U0 G4 |! \8 E/ l; o; H2 j
What happens when you try to compile and run the following application? Choose all correct options. ; J: R$ S1 v2 f$ `7 w) f" q: h
1. public class Z { : M1 b4 g; I* x& F/ }" D8 ^
2. public static void main(String[] args) { 0 q6 r6 V+ o; @
3. new Z(); 6 n+ r- ?1 d2 t2 ?; m D) A/ y9 X
4. } 1 D' h& i# w9 s8 f. y+ [
5. $ B+ Z- m/ U# h+ O/ c3 G
6. Z() {
5 \. F( j3 T" ^5 S1 O 7. Z alias1 = this;
" h0 r; j, X. u1 B+ }# f& G! v 8. Z alias2 = this;
6 n# d$ Z9 q- H. m* a) k4 j 9. synchronized(alias1) {
. c( @2 n" P. c- W0 H; f( A% ^ 10. try { ( I% L2 X$ @0 M8 g4 \# I
11. alias2.wait(); ; {6 Z) h# P& E
12. System.out.println(“DONE WAITING”); ) ]3 Q R! p" C* |% i. \$ I1 }
13. }
/ e A" Z; [( P* R; G( V 14. catch (InterruptedException e) { ; z* V7 l* f3 K2 @9 s4 w
15. System.out.println(“INTERR
% D+ y( x- l6 l+ A" k$ U3 h UPTED”); $ [) A' [4 B! }) `$ k2 F
16. } 2 H t* t/ J/ s+ x) W4 h: v2 e
17. catch (Exception e) {
! D- E3 O; ^! N1 I8 K! n) V0 y # m& I8 [6 I# T G
18. System.out.println(“OTHER EXCEPTION”);
, N# m/ Q1 m5 V% t$ U6 D5 o 19. }
3 c3 f* z9 d$ i# v3 ^ 20. finally {
7 [# r0 X l* b" k5 ?, [" u) L 21. System.out.println
0 w+ m. r k; P# j$ n- _ (“FINALLY”); 4 p" C8 g* ?) [4 @# w
22. } 8 R; o& Z* [2 f2 H Q; R2 g3 ^
23. } , ~2 i a! _2 j+ B
24. System.out.println(“ALL DONE”); ; Y% D+ K9 w2 O5 c% o" e7 s
25. } ' A5 ?: z1 N- p6 S. l
26. }
* T: H( V" e/ n3 G1 ~% Q A. The application compiles but doesn't print anything. 6 p a( |8 W6 I, f/ \8 j
B. The application compiles and print “DONE WAITING”
+ P; f& S) [( R& r, v/ ?3 ]% n1 S! M C. The application compiles and print “FINALLY” ( `0 i" @8 ~6 T' f
D. The application compiles and print “ALL DONE” 5 M3 S! @- |8 r8 l. S
E. The application compiles and print “INTERRUPTED”
* l' W3 o. K. ~) f: m+ b 解答:A % X% G7 d7 L, z: j8 B
点评:在Java中,每一个对象都有锁。任何时候,该锁都至多由一个线程控制。由于alias1与alias2指向同一对象Z,在执行第11行前,线程拥有对象Z的锁。在执行完第11行以后,该线程释放了对象Z的锁,进入等待池。但此后没有线程调用对象Z的notify()和notifyAll()方法,所以该进程一直处于等待状态,没有输出。 |