知道美河 | 上传资料 | VIP申请 | 精品课程 | 资料搜索 | 问题反馈 | 会员手册 | 积分消费 | 积分充值 | 帐号保护
美河学习学习在线赞助VIP

美河学习在线(主站) eimhe.com

 找回密码
 建立账号
查看: 21790|回复: 6

方联scjp权威考题

[复制链接]
javalea 该用户已被删除
发表于 2005-3-19 18:54:17 | 显示全部楼层 |阅读模式
Sample TextJava培训考题: R4 a# M* z& L4 F0 p7 `% G
1.        给出以下程序:5 c; [& _" P, u# S
1. public class Colors {, m7 t( D9 K3 H2 r. B  Z
2. public static void main(String args[]) {
* I' a2 }0 H  W* l5 u2 x/ _3. int n = 1;
1 U' m- h2 m' i- c4. System.out.println("The Color is " + args[n]);
* b9 Z# g6 v. u7 G; z5. }
) Z/ }+ d( ~: a5 ^- B- |' z* k6. }. Y- g+ v+ `: g8 ?5 N
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”! D1 F4 [# j! A% s3 S7 t; U7 [2 r+ f
A.        Colors red green blue yellow
1 X( j7 V; I1 ?( g; XB.        java Colors blue green red yellow8 v/ X1 i. m: u' K$ q
C.        java Colors green blue red yellow
$ a, R' X) J! A; O7 aD.        java Colors.class blue green red yellow
0 M. A* f9 Y0 @/ k# }6 Q% W( j' N- s2 kE.        java Colors.class green blue red yellow9 A, a! E6 X6 n7 C5 J* {0 h/ Y- ^
2.        读程序:
; P- {- @7 {, z/ E3 S1. public class Test{
/ e2 f" ~8 W% S. N% O% _2. public static void main(String args[]){0 ~- q; e& }& y& J: D# H
3. char c = '\u0009';$ [; g; t$ P2 @4 B7 J
4. if( c == 0x0009L)
, \9 j- @, r) y- p# C& L( O. g5. System.out.println("Equal");) W6 Z& ]* E( a) p4 {# w0 t; k
6. else System.out.println("Not Equal");
; Q; F! t; X0 h: M3 R7. }& A/ U) B3 K" I9 g
8. }
3 M( w4 N, y* C  t, c, ^当编译和运行该类时,将会发生什么:
  h9 R# a6 f. G+ T5 h6 k- qA.        程序因为第4行将出现一个编译错误4 Q* Z2 e5 e! e& Y  J' u3 g& R3 F) l
B.        程序因为第4行将出现一个运行错误  @/ u# E# j5 v2 H5 n
C.        程序因为第3行将出现一个编译错
7 g& |$ L& B! y5 uD.        程序输出”Equal"% v5 i9 N/ B* Y% c6 ^7 T& _+ Y9 N
E.        程序输出"Not Equal”
  \2 D6 d. E; V; u3.        读程序:+ [5 I; a0 v4 r) t) [
1. public class StrEq{# g% D, _  e$ A5 Z# A, w2 z. [, Z
2. private StrEq(){' W% t0 u: N* n9 x5 H4 |9 z3 R1 e: G
3. String s = "Bob";  V" v2 A2 Z3 [! j# W. ^: D# U
4. String s2 = new String("Bob");& F, Q2 N5 k+ Z9 v
5. if(s == s2){0 k& o( F( Q) ^' L9 |8 N
6. System.out.println("Equal");
1 W% x# K9 J: s- M; I) [% K1 ~* Z7. }  D) L+ _9 u( i$ K  l  x, o( a5 H7 h0 o
8. else{
! Z. B( F+ P9 c1 n9. System.out.println("Not equal");8 b4 l" p3 Q  i+ j( _4 D4 S9 m1 z  }
10. }
& y$ D( K7 P: W9 P11. }0 ^* V8 w# T8 m4 a/ i
12. public static void main(String args[]){: X4 V9 ]/ O9 m# H' x: P1 }
13. StrEq s = new StrEq();! w8 ]9 h) t  |8 Y' g$ J
14. }
" X, {# E) O4 j/ Q+ q+ v* u. Y15. }
! R6 m: C# W& w# I: f. L" }; k2 i$ \当编译和运行该类时,将会发生什么:
7 R- e1 X9 I! m$ vA.        程序能够编译,并且输出“Equal”' B) \+ \# U  f. T% v. L
B.        程序能够编译,并且输出“Not equal”- r2 G; ]9 `; L' L* Y  o
C.        程序因为第2行导致一个运行时错误
% S6 A) f; t/ ^' t1 OD.        程序因为第2行导致一个编译错误
! }, e7 @9 z3 k6 Y* Q4.        以下那个赋值是非法的:
0 N2 J2 W# a( G% Z0 nA.        long  test = 012;+ Q! ^/ j& k2 x9 P  ]
B.        float  b = -412;, O. G& x. m+ @5 b& H8 B# J1 a  S. X
C.        int other = (int )true;
- M* R. I1 u/ S; C$ j0 ID.        double d = 0x12345678;
1 z4 r; l- O0 `$ `) ~, `7 G. Q4 ZE.        short   s = 10;1 x! S  ?( l% x& v3 c
5.        读程序:( }2 [& G2 k6 D. |  X
1. class CompareString{
- [- ]0 W2 M% m- R; @9 j2. public static void main(String[] args) {
; L. T( x" E' i/ e8 S& L2 B3. Integer x = new Integer(5);
( }, o* `0 w: e0 o7 z4. Integer y = new Integer(5);
$ q. c% Q2 E9 l% `6 \5. Integer z = x;
  u' S; ^: c8 b1 W  \: c6. if ( ______ )
# z0 o' W0 I. Q' I$ L- |& p7. System.out.println("true");& K1 ?3 n6 r! A0 j
8. }
0 L) s0 j; r9 l1 c9. }
4 R, J( K( N, j4 N# f以下哪个选项填入第6行后,不能输出true:  j5 R! c2 o& i- a* D5 ]% J" z
A.        x==z
% G. |" B) v* x2 {8 V+ ], ~B.        x==y
8 @: H2 i' y8 S8 j& {C.        y.equals(z)
5 _7 e' a" q$ \# b1 E/ _# X2 }$ iD.        x.equals(y)$ Z. _+ }4 L! e. k
6.        读程序(双选):
, o7 L4 ^0 j, V, f$ |$ ^1. public class Declare {
+ b, g& Q* S& {" I2.7 [$ B& ], c7 }5 A2 {
3. public static void main(String [] args) {$ B* v9 e. G4 ]
4.
; X  b9 `# P( \% h6 ~: o5. System.out.println("The variable is " + x);& r% T+ f+ \* f3 b; ?
6. }) N* l8 A$ Z, Z  w, E* t
7. }
. ^$ m. u! y1 _1 E% M' ^$ |* L以下哪个选项中的代码可以使程序编译通过
2 q' D9 D! b7 HA.        把“int x;”放在程序的第2行
6 `* J; l7 Z4 XB.        把“int x;”放在程序的第4行
* F$ [: n# D) C+ z* u4 D. P2 H6 |C.        把“int x=5;”放在程序的第2行
( b% w' {! N6 \; j& C! T' ND.        把“int x=5;”放在程序的第4行5 ]1 g0 \* y4 E% ^2 C- z
E.        把“static int x;”放在程序的第2行, g0 ]8 b9 P9 x% X
3 C  D" K: z& G

* [7 J/ z2 w; C- s
; t  u) w/ R& @' \7.        读程序(双选):4 B1 g2 {$ _& n+ e
1. public class Child extends Parent{6 c" v6 ]/ h: t5 A6 y4 u, q
2. int a, b, c;
: O" y. _/ E. \8 }  ~, |0 u3. public Child(int x, int y) {
' J! K% X2 j2 h, A4. a = x; b = y;3 F5 x2 k8 t0 G, N+ C
5. }
8 F6 h9 r* J7 Q5 ~. o6. public Child(int x) {+ g* a( _- {, j
7. super(x);) y+ A5 `; X5 o" ?4 N2 L
8. }
* C8 V' o3 ~' Y9. }9 ?, w. _* z6 Q% N) O9 I7 J# X  e4 v
为了能使该类编译通过,下面那个构建器必须出现在父类中:2 G  D: J* b$ h1 H6 D# d! U/ ~
A. public Parent(int m,int n), o3 |! p7 n; ~8 n
B.public Parent(int m)
. h1 ]- o5 {$ T! [% h# UC.public Parent()$ ?# h# \+ q- B- q. l  k) |
D. public Parent(int m,int n, int o)3 g1 ~( K6 M3 g9 G/ Z
8.        读程序:
  O" P: o6 K# i1. public class Derive extends Base{
6 @5 Q# z  ~: m; X2 {2. public static void main(String argv[]){5 M& Z* i% T/ N4 q$ \
3. Derive a = new Derive();4 g. [+ Q" P/ `- p0 |9 N" {  p# {! G
4. a.method2();
1 L6 V( ~  ]7 c. n+ V8 ^5. }% y' k2 T2 n' v3 E# x/ e
6. public void method1(){0 J4 U6 O- V- G4 z
7. System.out.println("method 1");4 C% |$ u& x' ~6 F* q  }* k4 p
8. } 4 p# G! H2 H' {/ h5 e+ F
9. public void method2(){
# u  t3 B" z* r% z5 A10. method1(); - Z; n2 w, O3 E/ M1 q: Z
11. }1 C, o3 f3 l0 |5 V+ x
12. }4 \) I2 {! q& C$ m2 `: I
13. abstract class Base{
+ E* |$ r: m! J1 ~  g' I1 d14. abstract public void method1();2 D# z. a; O6 K
15. public void method3(){
7 b0 e8 y2 _8 n: i/ f16. System.out.println("method 3");
3 Z) d& s8 E$ y( H, R6 m( \) }17. }2 }! W) L; ?' D. j: N  g
}2 S- \) X( \" j
当编译和运行该类时,将出现什么:0 W' j, z4 T. y
A.        程序输出“method 1”
3 n/ E# l. Q( v/ |& F5 AB.        程序输出“method 3”/ _1 c% [1 T0 {2 c( I
C.        程序因为第15行而出现编译错误
/ S+ e: ]# G! }: Q" q0 D% ED.        程序因为第15行而出现运行错误, M" \; E- ?: J7 A
9.        以下哪个接口的定义是合法的:
' l; x4 A! M* ^, s6 UA.        public interface A {int a();}3 P+ x0 x* K5 P  t) f5 I: k
B.        public interface B implements A {}0 T* J4 z- g  l2 ~  _- \' V
C.        interface C {int a;}
' w% b6 H2 @$ X2 Y$ K. E7 s( A, T1 UD.        private interface D {}
( |! ^  C1 t: X4 Z10.        读程序:
- E3 ~+ u" f; Q1. //File SuperClass.java( ^* B" {( U2 X" E8 q9 @# p
2. package MyPackage;" g# t9 j+ k; {0 L' l
3. class SuperClass{9 \6 v5 U; M( z) \% A4 S5 Z
4. void myMethod(){2 `/ m6 u2 X" W& V
5. System.out.println("SuperClass");
% e/ ?' i& _2 }9 A9 Z( S$ Y6. }
; ~4 u1 r+ p1 d* l3 Z7. }
/ h" n3 D7 L! `* K  K$ s8. //File SubClass.java
1 K. a; `7 t  V" r, h! K9. public class SubClass extends SuperClass{
1 r% y& P2 k( q: f. _, X10. public static void main(String[] args) {8 u6 P* l, W! h# \
11. myMethod();
/ t" I* d5 r# }% V. h6 L( ]% r12. }5 @& {) W* J9 `( {+ x8 _( r- _0 s
13. }7 [* n5 D* n/ K  J* [" D" h2 G
当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:
" n7 k% E$ H/ L9 E6 q. x, pA.        两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”; t- T+ {2 y& z4 v7 k
B.        两个类都回出现编译时错误
  U, P9 f$ {" O% X& M& FC.        两个类都可以编译通过,但SubClass会在运行时出现运行时错误4 v' _) C6 \1 x6 i0 H3 Y
D.        SuperClass.java可以编译通过,但SunClass会出现一个编译错误
2 s/ E& o* b0 D2 \11.        读程序:, p) h1 q9 I1 o3 G. I
1. public class Test{3 h" a( h, e5 q
2. public static void main(String args[]){6 @  ?5 R* U' X8 e# g9 ]  w7 j9 @
3. int[] i = new int[5];1 J% c2 _7 X2 a/ \: i, t, K
4. System.out.println(i[5]);
# x! N8 y( q0 a; A) k  N, B9 r5. }- b4 |0 S/ Y) F
6. }
2 j- I' ?$ E8 v/ u- w9 X% C6 `% A当编译和运行该类时,会:; R$ l4 I% g& F' G, e
A.        出现编译时错误
/ n3 T* R+ o% c' S3 |3 \- r! u- bB.        出现运行时错误3 y3 \- W0 K! n( z; M
C.        输出0) ^% Q6 C- b# m3 K# U
D.        输出null5 l4 B) N1 d9 Z" j! K
12.        以下哪个选项是正确的数组声明或初始化:  S2 I8 [" e, x9 ]
A.        Array sizes = new Array(4);* b+ d+ Q6 J& d5 J
B.        double [] sizes = {1, 2, 3, 4};1 F) h; f' {: w0 n8 v
C.        int [4] sizes;9 ?8 y# s4 J; Y/ u
D.        long sizes [] = new [4] long;
& l& o. f/ K! O* s! z( f13.        读程序:& Y2 I7 X8 f: ?/ @+ o1 R  F  m
1. public class Initialize{
! e+ ^1 p) t9 q4 I" u1 c0 k" c2. int x = 200;) e6 e5 V6 G9 f% S+ \
3. static {# S+ s1 q! D" l! T7 n7 [% @
4. int x = 100;' W% K- j' e$ B% j3 j
5. }" z6 F, H& B- r0 ]. _. z
6. public static void main(String[] args){8 x6 X; E( e1 n# q, Q% @
7. Initialize init = new Initialize();6 k5 P3 y, D  N: L0 n! I  t
8. System.out.println(init.x);' {- i5 x% I! V. }: O$ p, k
9. }
4 m1 J8 P5 W' b: g$ t+ \10. }- u, P1 S) c7 R( x, L
当编译和运行该类时:. C8 t& R/ ]- p' a: g) \
A.        程序因为第3行产生一个编译错误
6 `& X$ l# Z% t' {4 A5 ], IB.        程序因为第4行产生一个运行时错误
3 j; X% J5 m! Q" nC.        程序输出100+ F  d8 k+ M9 X! A
D.        程序输出200
( ^9 m3 M% `/ v$ b. W$ D1 n$ ?8 Y14.        读程序:
- M1 o& f6 P! D1. public int abcd( char x) {  L! x  |$ [9 k; k$ M4 b
2. if ( x <= 'M' ) {/ [$ R& v5 @- N* v' D5 G- o! P
3. if ( x == 'D') 2 V! y0 s; \2 P8 M  u( W
4. return 2;
) T3 K5 |4 W- ^5 G* U5. return 1;# {7 E1 x, k) H9 i( w1 _! L
6. }
) j2 {" T  O) @9 X" Z: H7. else if( x == 'S') return 3;4 n. {/ Z" H" S  z/ G3 i& {
8. else if( x == 'U') return 4;7 w, P0 x; ^5 M0 y, o
9. return 0;
% c- B" @$ \& B4 N: `' q10. }
/ ^2 J: T% y, }5 e+ ~* J在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1
! l; [1 |+ I9 N/ o* t5 Z: ~+ t6 \$ }A.‘X’
- L/ B1 t/ b& f; a" V4 y0 m6 sB. ‘A’
6 d5 `8 h' Y  S  x' TC.‘D’
7 y  \# M& q, j  b& P+ x: t! e, wD.‘Z’) A3 M) y* ~  H; j+ {. H
15.        读程序:
- X+ r" I. f2 A8 P1. class TryTest {
' y/ ^. x3 s4 @2 z1 @2. public static void main(String[] args) {( d: l0 i$ S4 F
3. try{) m! c6 `- U2 K2 X- H
4. System.exit(0);
+ @' E; B! e- r5 a5. }
' q; I9 n: h/ \+ H: H( `) h6. finally {
9 T) z! Z0 n  k$ ]7. System.out.println("Finally");0 n* `/ c2 ]2 J1 d6 f
8. }
6 D# V' P! _3 T. A* o8 H9. }% v7 q! n" d, z1 u1 u, R$ z# Z" K
10. }9 L1 W+ s  L: V6 Z
当编译和运行该类时,将:7 Z1 `: `+ \* x) v, v& d
A.程序因为第4行出现编译错误+ b' y. K: x& |! m. }
B.程序因为第4行出现运行时错误) N1 O# X* K$ }
C.程序因为第6行出现编译错误
* K3 D2 m# V$ r/ {( n, y# S- TD.程序因为第6行出现运行时错误
1 N" r* H" z/ Q" k7 bE.程序输出“Finally”
) Z3 L  ?6 m0 ^4 C1 V! _F.程序可以运行,但什么也不打印
3 P. _, ^7 Q  B# {) k  ]* ~- t
6 j' }( o0 F8 `16.        Given the following variable declarations:
  n( W; H* T6 {( O/ I- \1 {Boolean b1 = new Boolean(true);8 x# c; b% H+ r; o0 b
Boolean b2 = new Boolean(true);
6 W' a2 h$ i2 ?. z* K$ O7 T7 a! GWhich expression is a legal Java expression that returns true? " j+ y6 h7 J: S, u5 E
A.        b1==b2
, n0 w& G/ H' VB.        b1.equals(b2)
7 Q% g( V* H# f) ~  Y, vC.        b1&&b2+ z: C, c* G! [5 n- j) Y
D.        b1||b2
+ x3 H+ A% d* y4 b9 ^! ^0 G: A$ `E.        b1&b2
5 }$ y6 P# q5 f3 }3 T. qF.        b1|b2
) x- }7 D- k) S4 a; I) P2 T4 o17.        Given: 0 x; ?+ r* x# {+ E2 I6 l) J* k
1. public class Employee {' e% Z1 b3 I" Y( z
2. , s: l$ F! B5 r. d  {
3. }$ S$ _! i# I) ]* o  E* |0 K
4.   b' j! q5 v  p6 [! G3 x. i+ k
5. class Manager  extends Employee{: H9 C, H+ j2 Y8 k! ~: @
6. }
/ @; s: K6 n1 e+ X, {  qWhat is the relationship between Employee and the Manager?' J% w$ P8 D4 _8 W5 d- Z
A.        has a
; I% z1 ]5 y) A+ ]( s% J, zB.        is a" k- r: P$ a: Z6 w& S9 S
C.        both has a and is a
4 o* e+ o& l6 P' k& H2 lD.        neither has a nor is a& X( G! ^1 h) O
18.        Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?
% b  l. `* ~+ \A.        BorderLayout. L" X2 I/ X" p' f. k8 N; H
B.        FlowLayout, z4 u9 I% o0 b: ~  \
C.        CardLayout# x  A( {; _* Z' K/ e7 _$ u
D.        GridLayout
& _1 R+ R* N1 u; s5 M% i19.        Which of the following AWT components can generate an ActionEvent?$ S# u9 o/ D" L5 k% y; y" j0 Z1 `) q
A.        java.awt.Button
) U, \+ W* X3 F9 l# cB.        java.awt.Panel; l( F" t" A( ?2 E
C.        java.awt.Canvas
$ U3 ]4 H, z  B3 n  D1 i' ID.        java.awt.Scrollbar, C9 W& ]* R# A4 T% Z
20.        What kind of Stream is the System.out object?
2 Z% h+ d' l" U' Z; GA.        java.io.PrintStream" T5 D# ~/ s7 u4 m& U( {- E
B.        java.io.TerminalStream5 Z: ]; O& j( x' g/ Y( L5 }3 E: U
C.        java.io.FileWriter/ c' J8 l+ W0 f, O& b" g" U
D.        java.io.FileWriter9 |* @7 @/ ?! S+ P7 U  T
21.        Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)
$ L9 o" e; l* MA.        java.awt.event.FocusEvent
, V. p7 ^2 L/ r+ m3 l9 U* {2 \. aB.        java.awt.event.KeyEvent2 N& S, {! Y- }+ V# f6 R: K. t
C.        java.awt.event.ItemEvent, w5 H0 ?% l; R) y8 T
D.        java.awt.event.ActionEvent
; T4 M: b+ ^+ |0 H& \22.        Which of the following methods are static methods of the Thread class?(双选)
) w' `* L% i% f  gA.        sleep(ling time)0 p0 Q  v! v6 I+ S3 L8 Y8 Q
B.        yield()2 q) J/ u* o8 T  u) F6 q0 Q$ I/ Y4 k
C.        wait()" e! }2 u, `& Q6 K/ a: K6 M& \  e
D.        notify()9 l% I* F0 }6 J8 {/ _0 t' ]
23.        Which method do you have to define if you implement the Runnable interface?# S& T- t$ i  a- d5 f
A.        run()
4 y* D/ _& M8 W% LB.        runnable()
% \5 G5 a0 j* b! X  HC.        start()
2 d- }' w* Z" w8 uD.        init()
, w$ g6 x4 m2 y$ [. T4 TE.        main()2 I8 ?3 v* m& L7 F: q; h( @
24.        Given:+ {! \; r- f* @; a# a3 C
1. class MyThread extends Thread {
2 ?+ N; k+ T3 M7 x2.
1 q, w" Y$ z  i! t- |7 E! O: B3. public static void main(String [] args) {9 E: |& o" n1 d7 b
4. MyThread t = new MyThread();
( u7 A7 ^3 d' r" r9 P4 W5. t.run();7 r$ m; C9 m: \4 A
6. }
, Z5 f+ V7 g5 W, L) ?: ^+ m, n7 l7.
6 @, Z  i2 y* v0 {8. public void run() {
! j, T* _4 B2 ]9. for(int i=1 ; i<3 ; ++i) {! V0 h% R9 r! M# S
10. System.out.print(i + "..");
+ A9 ?9 ?- t% d' M% O% k4 `11. }
" Z' `. m$ s" J% W" L12. }& ^4 u' A9 u$ y1 z  l8 G- N
13. }
9 Q( q6 X' K  R# mWhat is the result of this code?C; _# K: N. P: N8 ^* L9 K" h! j
A.        This code will not compile due to line 4
; Z4 W5 U. E: J9 [B.        This code will not compile due to line 5
& D" w9 S) ^( V5 |C.        1..2..
6 ^0 Y8 n: r/ k; f9 m4 t* BD.        1..2..3..  H+ K! w& V/ ^) T& t
25.        Given a local inner class defined inside a method code block, which of the following statements is correct?
5 F0 k+ C' k1 ]0 a1 v2 B) b" ?A.        Any variables declared locally within the method can be accessed by the inner class
7 k! t5 v' ^1 p' S* XB.        Only static variables in the enclosing class can be accessed by the inner class
+ R: `$ M$ @6 c+ V1 _7 \$ }: d5 k( ]C.        Only local variables declared final can be accessed by the inner class0 G6 `4 [. ?, b1 b( J
D.        Only static variables declared locally can be accessed by the inner class.$ L! K& i+ E8 ^4 m  h

, t; c1 ^$ ~9 Q: j2 a4 V: Q) S+ |/ T5 N( s  L
$ `; a" d5 |4 G2 P0 {' z
26.public class SychTest{ * l% i, J& L, }! K( N& S) ^$ n
   private int x;
$ P; d( {- O) e. t   private int y;
7 M: a6 Q3 X  I6 Z  B   public void setX(int i){ x=i;}
. R& q, H1 V9 ]. z   public void setY(int i){y=i;}
$ [+ }/ ^  K) {' Y' H" `   public Synchronized void setXY(int i){
* D8 W: {# ]3 t( L3 H     setX(i);
9 f: ^7 r/ V' W     setY(i); , [$ [' s3 S) j; H1 D0 X
   }
" P3 d8 b0 o% T, x4 c   public Synchronized boolean check(){ , q, A: O+ |; d/ y2 j
        return x!=y;   
! y4 B8 z; l; G  j1 o   }
9 s9 L' e) z+ w9 K8 v9 o1 N   } # n' ]9 A, F3 x( J0 s7 m5 D0 l
    Under which conditions will  check() return true when called from a different class?
9 |( z* D; ^4 ]3 r- T! h: I   A.check() can never return true. & ?- g2 _5 x$ q5 n
   B.check() can return true when setXY is callled by multiple threads.   I, p  k8 u& Q# N& V
   C.check() can return true when multiple threads call setX and setY separately.
; \0 U9 C+ X1 r8 x! E: s4 U   D.check() can only return true if SychTest is changed allow x and y to be set separately. + x% B- E( C0 y, l* R' I$ L

0 L) r& ~: q0 s+ d1 ?. |27. 1)public class X implements Runnable{
9 ^' B: @- i5 S8 @; S7 K" ~  t# T  2)private int x; $ v& m9 s. B0 p; {4 P
  3)private int y;
6 \' p$ Y; n( P- P  4)public static void main(String[] args){
( J" j9 q: l  q, }1 Z2 ]+ n: N  5)   X that =new X(); 9 i6 ]: y! E/ ]( B% k
  6) (new Thread(that)).start(); 4 N8 S$ f0 a* o& [
  7) (new Thread(that)).start();
' j* O( h! h" v   }
" g6 Z0 \& M6 k# G$ `$ m/ J2 r- ~% B# X  9) public synchronized void run(){ " V) I, d2 w# J4 o
  10)  for(;;){
( u, _3 o# m4 L( x+ |$ x  11)      x++; 3 e8 R4 ^) @& W+ S8 r7 ^4 z* }! q& w
  12)      Y++;
8 M4 L" s, }9 M& B4 W  13) System.out.println("x="+x+",y="+y); + W% |; q; h5 T
  14)    } , H$ f8 Z& q3 J& \( `
  15)   } ) H: L) K' {$ V2 C
  16)  }   
# M% F9 [- [0 N# O2 b2 {  n  t    what is the result? 0 X0 ?6 s6 L. Y6 c7 i
  A.compile error at line 6
% K+ e1 I( f3 H! ^2 Q# _7 u5 Y  B.the program prints pairs of values for x and y that are ) F  {, V+ L9 H
    always the same on the same time 6 c1 y% A8 \  D5 g8 [* G

7 K0 r3 \! ?- |- B9 J28.class A implements Runnable{   _7 b/ y4 U! z" T% w3 D
  int i; 7 v  i8 B3 K, H/ w
  public void run(){ / o: H; N3 w0 v8 U
   try{
5 `# o2 _) T( A. b" Y& Y7 A2 s       Thread.sleep(5000); ; ]: R' ^# f: D: ?) j* H; @, o& W
        i=10;
, R% U: F* i& a" y       }catch(InterruptException e){} ' P6 a2 p  e( Z( D
       }
" p. g. V# _- o  _" X       } ; j; T# K2 S1 g, J; a! u$ M1 G
   public static void main(String[] args){ 0 F. J- P' D: f. k3 J; y$ Q& I
      try{ & `- q8 u7 q9 q" p% u9 V2 E/ v
         A a=new A(); % U3 ?5 m8 O5 y% V+ N2 Z3 q- m
         Thread t=new Thread(a);
% D& I7 J  t0 W         t.start(); - c/ N& O" A: d" U6 J5 h" T& q
  17) 6 N& o! l- f0 O0 c. Q
      int j=a.i; ; \, s7 w$ I* t+ [9 t& b) ^
  19)
" }* ?) n8 W; P$ W      }catch(Exception e){}
, K, g2 z7 S' c/ p9 n" \1 c3 }2 A      } 1 {" Q7 b" E$ r: y/ S
      } 6 \: |$ m* n* S- J$ W  b! g
  what be added at line line 17, ensure j=10 at line 19? : J2 Z# K8 c' |0 q$ I$ x
  A. a.wait();   B.  t.wait();   C. t.join();   D.t.yield();   
8 R" o6 f$ R) x2 X* J  E.t.notify();  F.  a.notify(); G.t.interrupt(); % ^" T$ C/ m, r' I& f7 i+ S
29.1)public class X{ % V9 N0 b# w& q# p( }
  2)    public static void main(String[] args){
. V$ s4 ~2 G  i& q$ K$ F0 ]  3)     String foo="ABCDE";
% [' p& L, g5 L1 q% m$ _  4)     foo.substring(3);
4 V0 F6 \* m% H' X1 Y) d  5)     foo.concat("XYZ"); 5 s; y$ s! E, O; v2 A/ E/ ]
  6)    }
# ^" P( y; d) f" P# N& Y7 [6 V  7)   }
# E' E( o6 V. r1 e9 ^. [( D  what is the value of foo at line 6? / _7 C. }- B( ~9 v2 O! z5 [
30.  public class Test{
. N& L& b4 x7 l# G+ }' ~3 f: j   public static void main(String[] args){
3 E$ k! y. w" X   StringBuffer a=new StringBuffer("A"); 9 [/ J  G. h( e$ R7 y
   StringBuffer b=new StringBuffer("B"); & k  B$ p+ B+ k8 Y% x- G. s
   operate(a,b);
  o6 d" U0 ?- o" O6 R   System.out.pintln(a+","+b);
+ _4 g' M6 g; Y* U6 P    }
) Y$ v+ y# n- w5 \3 q7 L+ x/ {0 ~   public static void operate(StringBuffer x, StringBuffer y){ 5 P7 e& A( D! b8 u  J: C- ]0 q
    x.append(y);
1 }4 F- w7 _9 {' E0 {/ S, e    y=x; . p& b) U$ l8 c6 g
   } # M) }3 _% y5 U% y  e1 S0 E
   }
4 Z  l  l- u( e% g" p  I- J   what is the output?
yangyu638 该用户已被删除
发表于 2005-3-20 12:49:05 | 显示全部楼层
感谢楼主分享* \3 n! ~3 ]/ r5 Q6 m2 `$ ~
我已下载。。。。。
" F) Q: P" p) E特感谢您的大公无私
weichl 该用户已被删除
发表于 2005-9-29 21:51:28 | 显示全部楼层
感谢楼主分享
发表于 2006-6-24 19:00:04 | 显示全部楼层
JAVA还没学呢,先下载来再说。。。
发表于 2008-5-23 21:00:52 | 显示全部楼层
支持楼主。
发表于 2008-12-15 08:22:28 | 显示全部楼层
谢谢楼主发这么好的东东!!
发表于 2009-7-7 16:26:16 | 显示全部楼层
您需要登录后才可以回帖 登录 | 建立账号

本版积分规则

 
QQ在线咨询

QQ|小黑屋|手机版|Archiver|美河学习在线 ( 浙网备33020302000026号 )

GMT+8, 2025-8-12 14:58

Powered by Discuz!

© 2001-2025 eimhe.com.

快速回复 返回顶部 返回列表
最新资料站