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

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

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

方联scjp权威考题

[复制链接]
javalea 该用户已被删除
发表于 2005-3-19 18:54:17 | 显示全部楼层 |阅读模式
Sample TextJava培训考题
0 W, L/ D3 R$ b' C3 B! e# {. g1.        给出以下程序:( g5 U! G: J+ K( G. g
1. public class Colors {
  y/ g6 Y  C4 I2 z* S& x; c2. public static void main(String args[]) {
0 C. V, Q% q, C" X! G2 @3. int n = 1;& @) Z& z0 L8 l. h7 ^) N
4. System.out.println("The Color is " + args[n]);1 J1 e  w% l! B5 f9 Z8 M; e
5. }9 T% P$ ]( }( `
6. }2 b# v; f" i4 T9 s+ O
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”) v/ w5 ]0 E9 i+ R. r2 a
A.        Colors red green blue yellow
5 A* }; R) ^% u& a  ^- }/ QB.        java Colors blue green red yellow
! R2 F- I7 b$ M$ ?4 ]; aC.        java Colors green blue red yellow3 g; U' b  O& a  N* F  H7 z
D.        java Colors.class blue green red yellow( ^1 M  Z6 S- G& x* L
E.        java Colors.class green blue red yellow1 F: ]( j7 c$ D$ a" F6 Q
2.        读程序:
6 y) ~) U- U8 h" T, e2 B0 x$ |  M/ O1. public class Test{8 y2 o8 }" j) x# K' A0 l: W7 `& }
2. public static void main(String args[]){
  t, e! d' I% G. c- ]: F# ?$ Q9 a3. char c = '\u0009';& n" j5 }6 r; e- {, [+ h% B
4. if( c == 0x0009L)# O" z: `2 O8 R7 i9 n
5. System.out.println("Equal");* g  d4 P/ N5 ^  k6 b9 t& g% K* d
6. else System.out.println("Not Equal");
1 _& o: C. b3 @( E7 I% H* ]% X1 ^7. }5 B6 a, [$ [+ B' G2 D3 |
8. }5 ]8 B. L3 T$ y0 q; X" i2 E+ f
当编译和运行该类时,将会发生什么:5 I" \# Y. ]7 B
A.        程序因为第4行将出现一个编译错误& U5 ?" g7 ?1 y7 _3 P5 f
B.        程序因为第4行将出现一个运行错误$ L" ~3 Q+ [) ?. L4 R! D% l4 k
C.        程序因为第3行将出现一个编译错
4 E, m2 Y- g+ P) n7 gD.        程序输出”Equal"- i# c8 u; z5 }5 w% x
E.        程序输出"Not Equal”
: O( m" D) h5 f; p3.        读程序:
3 L8 o/ D6 ]+ q! B/ `" v5 x6 P0 F1. public class StrEq{9 Y0 \! t' I% H) {# T+ l: I6 K
2. private StrEq(){# {7 f: ]2 t4 ^6 ~; h
3. String s = "Bob";
" x0 J4 t- r, o5 T4. String s2 = new String("Bob");3 Z2 k; ?1 z0 X* S
5. if(s == s2){/ w4 b- I4 q6 b$ G0 q. v/ D0 P
6. System.out.println("Equal");
+ P' @! D8 J4 r* P7. }; n; o. J' j9 r/ O! j# A
8. else{' s* K, U, g$ L( A" m
9. System.out.println("Not equal");: x$ A' D# Y" {% f+ c0 ?. }
10. }
- r: w3 [3 [# H& e) m( G  s! q11. }
$ u$ g6 H+ \( q0 w12. public static void main(String args[]){
+ P$ t! i* a/ G13. StrEq s = new StrEq();& |( q! P- x/ C$ ~7 S4 x9 j8 S
14. }- o& D5 e0 ]1 @
15. }
. A* G1 K- k  E# v2 M当编译和运行该类时,将会发生什么:) L: O) h3 S* W8 F$ W* Y4 `
A.        程序能够编译,并且输出“Equal”
1 e, t- l7 u! z8 j6 o( c( pB.        程序能够编译,并且输出“Not equal”5 X! ^2 [# C7 J/ p* ?; G8 u5 y
C.        程序因为第2行导致一个运行时错误" v3 l7 ^# r5 h/ b9 P* D1 X, l
D.        程序因为第2行导致一个编译错误/ O4 ~8 I6 n: L* Q) h9 {
4.        以下那个赋值是非法的:
: \8 i+ \/ O$ S) IA.        long  test = 012;
% t1 \& E1 e- @5 B! ~. e+ J0 c& RB.        float  b = -412;
+ D2 J: d+ k, t$ }2 Y1 YC.        int other = (int )true;3 x7 }0 C. ]$ R% g+ q: Y  ]; k5 w- O
D.        double d = 0x12345678;3 x" ]% O) ~0 Q. |+ g
E.        short   s = 10;
) ^% Q) X. c: }. f9 }5.        读程序:
/ F: b$ F8 ~" f1 h4 c" ^7 I1. class CompareString{
7 `' ]2 ^1 |. W2. public static void main(String[] args) {8 E( N3 Q# ?- r1 g6 F) ^2 B
3. Integer x = new Integer(5);& x( Y2 D! i1 Q7 Y1 |6 ?+ |
4. Integer y = new Integer(5);
3 D; z1 H" V4 q( \4 e7 c5. Integer z = x;5 B, z8 F( H- l7 F6 {
6. if ( ______ )
4 B# ^  ]2 Q" d" O1 l- K, F, ]7. System.out.println("true");
7 L; z8 y. ?# N8. }% e$ v1 R" c/ _$ R2 C: W& ^8 |
9. }- W  p6 ?6 j7 M  A0 d  X7 B
以下哪个选项填入第6行后,不能输出true:$ R; ^3 E8 R2 s, |
A.        x==z1 R3 F- t6 p5 ]! r% b) [( v
B.        x==y
0 T+ _' P3 F& e; dC.        y.equals(z)
' P+ X& G  e. k+ u" U2 s: H9 A/ FD.        x.equals(y)4 h3 {8 F* W8 N7 h+ D9 A: K
6.        读程序(双选):0 B( n( v7 V2 B0 n- J4 Y
1. public class Declare {5 n# X6 G! H# @+ y, _8 Z
2.9 p# K* ~4 ], D
3. public static void main(String [] args) {
8 Q" C5 s, w3 F: T) R4.
0 o: t+ F, [- M5. System.out.println("The variable is " + x);9 F4 d) b5 p  _8 M1 n; t1 V) p
6. }
# K: E4 l+ B% P( M& _7. }
- D1 ^8 D& m, j8 q& t9 w以下哪个选项中的代码可以使程序编译通过, g6 D# R. z  \  r+ Y  e: ~# [& x
A.        把“int x;”放在程序的第2行6 h( U3 u+ Q: f1 ?( T
B.        把“int x;”放在程序的第4行
  T' t0 o& y9 [8 v8 n1 a6 bC.        把“int x=5;”放在程序的第2行" e) P" X+ l  `2 V+ ~
D.        把“int x=5;”放在程序的第4行  B: o# G4 y' }5 `. P
E.        把“static int x;”放在程序的第2行: h5 K5 E; O4 A8 @# \+ w+ S
+ f) y' E- h, s$ K
8 S( \# L! [( u: W' X& E) S

1 {8 Q! G! k: r* j2 g' U7.        读程序(双选):
3 P8 ~% S6 i) t3 J1. public class Child extends Parent{
: w" r  z) @. E' W) j" o5 ^, p% _5 ]2. int a, b, c;
5 _  Z. U" t. o0 s4 }3. public Child(int x, int y) {
" L$ L, P- f) {* w* ?4. a = x; b = y;
, m" i6 t3 L! Y  d5. }2 \, v0 X) |2 N( D/ Q' ~
6. public Child(int x) {
7 b' V) G* i: Z) ^  }* [7. super(x);4 h: k# O8 _  z: k  s- ?* Z
8. }
% I- n- z" O# j" {/ ^9. }
7 M' Y/ D9 I& o1 m7 z  i为了能使该类编译通过,下面那个构建器必须出现在父类中:
& J( E$ O; S: |8 JA. public Parent(int m,int n)
  Q' V, P, h  M+ R0 U8 B6 }B.public Parent(int m)
3 ^  Y3 x6 k$ z# g; v# [, W0 ~C.public Parent(): ^) `7 s/ u/ X# u
D. public Parent(int m,int n, int o)5 v8 M* e  w9 v! a, d# |0 E& C
8.        读程序:
* h( R# n0 w) }( F: g. \+ ]. y$ g/ W9 R1. public class Derive extends Base{
5 S( t& n# F5 v" {2. public static void main(String argv[]){% x$ n3 c! `  u5 M( y5 h0 [* m
3. Derive a = new Derive();3 A3 @* d. \% L. K5 c$ f4 O. p% i
4. a.method2();* K  Y& G; g; ?7 H8 [
5. }$ t9 t# ?8 ^# X
6. public void method1(){
2 x* k# r; a, p2 j% w7. System.out.println("method 1");( G% ]9 \9 o/ N+ F! P
8. }
& v& b( x4 U5 S4 A- w, Z; N0 U9. public void method2(){/ @% V  G8 ~  p! e
10. method1();
6 f7 }0 _" M- a; a11. }* I, Y3 h! t) q% |4 T  w- N
12. }
9 j+ t, L& P& A: h0 ^9 S13. abstract class Base{* r) V- a1 s4 o, e# s
14. abstract public void method1();$ a8 U; a: K4 M% x2 l' U
15. public void method3(){9 J# f7 ^" B) k
16. System.out.println("method 3");2 G7 m; s2 ^& b% ]. O
17. }+ b& M% J! J" Z$ ?
}# O- p8 a& H% {( y; d& J' K* B. ~- P% W% \. M
当编译和运行该类时,将出现什么:# D) [0 E( K4 L6 i
A.        程序输出“method 1”/ C4 @9 [& i2 c; y* C; i3 \$ e
B.        程序输出“method 3”* F( L" X  Z, C$ y+ l
C.        程序因为第15行而出现编译错误+ U/ S9 Q$ T+ C% c
D.        程序因为第15行而出现运行错误
' e3 r( j" K) D: G3 H& l9.        以下哪个接口的定义是合法的:( M  }5 ^7 N6 N% h. _9 I- j2 }
A.        public interface A {int a();}7 [. N& h) `8 B' H& H
B.        public interface B implements A {}5 u4 p. K- G' x& j
C.        interface C {int a;}& w; V: `6 y/ A" L& |6 J
D.        private interface D {}( ]- T2 Z3 l( C
10.        读程序:& ^/ H. K9 s5 a" J3 d- U
1. //File SuperClass.java. G% |+ B* v5 i% D( o0 u; r/ `# o
2. package MyPackage;- H8 U8 B0 D( P$ i/ l: Y
3. class SuperClass{
3 I) F( k8 c9 l4. void myMethod(){5 Y. v( k' c, p- w9 A2 o. B
5. System.out.println("SuperClass");
5 {# Q4 G6 [& ~: ]7 `$ b6. }
" t$ y% W7 A2 X7. }
! {- B) b7 m, J1 {0 U" {1 t* [' k8. //File SubClass.java
. F9 f# ?- y! }- O" S# a  Y9. public class SubClass extends SuperClass{
8 L, v3 y5 Y- k- R10. public static void main(String[] args) {5 \6 s$ `# w- k" L6 ~
11. myMethod();4 m2 @8 x6 P2 x6 S3 ], }) t% D
12. }
+ s8 I( }( N: t7 c" Q: I13. }
: B3 F7 L5 u" B7 i' h! p4 Z4 x# E当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:+ {5 e8 g* u6 X
A.        两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”0 F: \3 I1 B: ]) V7 w8 B1 r
B.        两个类都回出现编译时错误
7 V! f. ^) E- UC.        两个类都可以编译通过,但SubClass会在运行时出现运行时错误* S( Y+ v4 n: `3 S$ t
D.        SuperClass.java可以编译通过,但SunClass会出现一个编译错误, U- j! ]+ K. M; q# P# @) A/ [% v& P0 g
11.        读程序:
  V  A3 y2 E# m6 O# S  a% G) b1. public class Test{
4 y/ B2 A# A* q) M/ x0 g5 F$ u& H, o; D2. public static void main(String args[]){2 ?. F* P( s. M3 L) ]
3. int[] i = new int[5];6 H6 ?( `- k- f" Q
4. System.out.println(i[5]);
( V& N2 V. E# J4 C1 ~, F  _5. }5 ^1 i0 O1 I9 J: k' k- q
6. }
) x( k$ Y# F3 k4 N: J当编译和运行该类时,会:
( [. a7 _1 T# Q6 z3 YA.        出现编译时错误
& W7 h5 n. {3 f4 ?$ r/ N! w3 XB.        出现运行时错误2 z. e- z. r7 M2 r6 m0 e8 z% q
C.        输出09 N8 O4 [, s5 K5 b6 ^
D.        输出null
# K' T& k- s6 u% A, d) d9 B12.        以下哪个选项是正确的数组声明或初始化:
" g$ V$ Q8 j2 q( }8 @A.        Array sizes = new Array(4);: [. W' n" x+ a9 {6 [% s
B.        double [] sizes = {1, 2, 3, 4};7 _, K4 o6 W0 ^" Y3 j0 u
C.        int [4] sizes;
, d6 r) \2 P! f. z& KD.        long sizes [] = new [4] long;
7 F: x% b% L5 Y2 k9 ^) f13.        读程序:' o- V) k% ?+ U3 N( x4 C7 f' v
1. public class Initialize{* }! r# ?# Y2 y) F7 Q$ d# q
2. int x = 200;% s6 s; u3 h, B; g/ [( i- o3 c
3. static {4 V, i7 L' V# f* |& @
4. int x = 100;
. q, w6 ^# V2 o* m) Y" V5. }
& }. W; g  F9 p# X# T6. public static void main(String[] args){5 @" [* I# P1 f% @6 p1 e1 u1 s
7. Initialize init = new Initialize();
( `: h8 a, v8 C  A/ |8. System.out.println(init.x);
2 b2 a0 c+ ?: Y9. }  @* c* ~, _6 B9 Y
10. }
. R1 b  T- f/ H3 Y当编译和运行该类时:2 a+ Q) |5 d7 u
A.        程序因为第3行产生一个编译错误& T' `, z4 ?! z# ^8 T" T* u
B.        程序因为第4行产生一个运行时错误" H# |% F2 U; q1 y0 m' Q
C.        程序输出100
( A) h% O  r! Z2 l. q* {& q7 ^D.        程序输出2001 ^+ t8 y4 L  h% A+ C& F& W
14.        读程序:+ e$ J, s0 q1 {' [1 F1 ^9 e4 n( ^5 c7 t$ U
1. public int abcd( char x) {. `( G+ `9 i' i; Q
2. if ( x <= 'M' ) {
* ~% q6 I: \/ x8 {9 t# {- f6 W3. if ( x == 'D') : g/ k! f' R; x5 U
4. return 2;
) T4 C6 q. C$ n' m/ B5. return 1;
* [! i5 c8 e8 \6. }0 X. n! Z3 e: m
7. else if( x == 'S') return 3;: \  G' A1 |' |. t$ o3 ~$ H
8. else if( x == 'U') return 4;; p6 r7 N( P! W0 S
9. return 0;
, b! D# S& r7 d: k10. }
2 a& R( O4 E9 A9 A5 l在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1! \/ r5 H! F% k2 w" [
A.‘X’
; c. j+ d6 u3 L6 j7 V- hB. ‘A’
; b) t5 @- C7 t+ UC.‘D’- W; w! `  X- k' m3 J; Y$ J
D.‘Z’
! E$ Q% K2 J* w$ R+ j* W15.        读程序:
5 `  X7 O; M  W4 y" _: B- A. J! j1. class TryTest {
9 u- L& W" D8 w9 `8 j5 @2. public static void main(String[] args) {6 y5 P) k, f- B& t( d. h% |5 t
3. try{2 {& @% n( a; J( I3 W
4. System.exit(0);( B. @% R% D/ [8 y
5. }
8 [  `$ U: G, A- A3 ?+ H6. finally {
8 @! f7 T  {% q* ]% }  x! i! `; i7. System.out.println("Finally");
. M; Y3 n4 O' S% ~3 h8. }
. U2 q! I8 S) _) D6 [" P9. }) D! E. ]' V* Y/ Z3 J
10. }& D, u3 [; C+ l0 G) ]
当编译和运行该类时,将:
) B9 k3 C. G' I9 C" U8 f8 TA.程序因为第4行出现编译错误" [. C; s% J' U6 d
B.程序因为第4行出现运行时错误
* ^6 @# F" \) b& p9 L- RC.程序因为第6行出现编译错误
; z& b( `4 [' w% o6 q  y  n( uD.程序因为第6行出现运行时错误4 k, g, }: V- e9 a3 Y
E.程序输出“Finally”. i- R6 R( i2 a' f6 {. a6 C
F.程序可以运行,但什么也不打印7 T- h0 q& _9 w/ T5 h9 j

) D) n7 v; I# K5 D2 B16.        Given the following variable declarations:! X+ e' `1 E; r5 I" z
Boolean b1 = new Boolean(true);( p. @  J2 t' f( O
Boolean b2 = new Boolean(true);
( C- V$ T9 X, S2 w6 BWhich expression is a legal Java expression that returns true? , x. k; R4 u  I! l) m
A.        b1==b2
9 N6 R' _( C# |+ K* R! YB.        b1.equals(b2)4 O0 j! x! N8 r* V# [0 I
C.        b1&&b2
- h% w9 p, R+ Y4 U* f. O2 s1 s. \D.        b1||b2
5 d5 _" F5 r! h  r' [E.        b1&b2! ?; H* G3 C. m( I1 P0 D; R, E
F.        b1|b2
5 \( F. P0 x: J; U3 \* }& E9 e17.        Given: : F% Y2 ], \. o2 i3 Q# B, g
1. public class Employee {
* R- i; {+ H- O2. ) K" j( b# {% Y# d( O
3. }
  V  R' t, C: @. W6 i  G0 m4 A4.   x. ~$ l, V6 i3 `0 {
5. class Manager  extends Employee{
0 D( j! p, a# {" K/ F- t6. }, Z+ j2 K6 \, W  d
What is the relationship between Employee and the Manager?  B' e' y- C$ F/ q' a
A.        has a+ O& X; m: X! F9 b% P4 J
B.        is a* C6 v5 _9 l1 G8 O, f
C.        both has a and is a
/ t0 \, w0 `1 v% }* ?& \D.        neither has a nor is a: [# K. v$ Z" X! |/ L7 D
18.        Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?
, P) v7 K! L! m  L  c4 sA.        BorderLayout
0 `& _/ J/ y" L9 A! F% ]B.        FlowLayout
, O$ J+ f3 Y0 s. V8 [C.        CardLayout' d! J& K+ Q, t. |1 z% l* h
D.        GridLayout
) j% d/ _3 w- j3 S19.        Which of the following AWT components can generate an ActionEvent?9 L1 u- ~) U( C* ]9 A4 n2 I
A.        java.awt.Button
0 ~/ }# T2 Q7 H- O! e" ]5 s' t9 bB.        java.awt.Panel) a* E$ [* ~. t% W! i7 I: m) ?) X' @/ }
C.        java.awt.Canvas
" P. b7 X; r/ aD.        java.awt.Scrollbar1 q( n- v# h* f3 @8 P1 v& X( D
20.        What kind of Stream is the System.out object?
- o$ i1 {1 R$ a7 d( X% MA.        java.io.PrintStream
  \* f8 z! ^1 g7 K6 I, lB.        java.io.TerminalStream( Q7 y4 G+ a2 x* i/ j
C.        java.io.FileWriter, J$ J" a/ S& V- M' i) M7 a
D.        java.io.FileWriter
% [2 t  b5 |* b1 p21.        Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)
+ u: q/ F5 s/ i9 c; L, E% }' N+ DA.        java.awt.event.FocusEvent
! K1 q7 U+ O3 v6 \1 M% r+ i! r* g9 H0 uB.        java.awt.event.KeyEvent1 K7 k& ]# M) T5 L8 A* K
C.        java.awt.event.ItemEvent
" D/ a0 b7 ?) A* b2 f$ W9 gD.        java.awt.event.ActionEvent
7 `' d: w' |8 U4 j22.        Which of the following methods are static methods of the Thread class?(双选)2 a) E6 p+ n6 C7 z4 q( a
A.        sleep(ling time)
1 v. X# ]. C- i' s8 I7 r6 @B.        yield(); r( E8 }+ m. j9 H0 S7 U1 n0 {" L
C.        wait()
2 ?* {8 z( O4 }1 a4 OD.        notify()! m3 |. w8 c: F0 p
23.        Which method do you have to define if you implement the Runnable interface?1 W5 G  x; L+ D" {* X: M. e
A.        run()
* n8 y+ O% s* \% ]& W1 ~# EB.        runnable()
# v! ~( P; C; Q7 n( rC.        start()/ a8 @7 r7 _# F2 b" |& w% L! l
D.        init()
" [- L& r0 K* _  K  w- I7 R8 ]: iE.        main()7 d) m7 |& X2 h2 U7 I& S
24.        Given:
  a2 P) T9 f/ Z1. class MyThread extends Thread {: H1 H9 w; a( S7 a0 @
2.
' E( e1 n: G* x. {% A) g4 S& H3 C3. public static void main(String [] args) {
) u& ~' s' d/ ^, I) f, J+ U4. MyThread t = new MyThread();
3 O! o2 E! ^& @6 m* d* G- |5. t.run();
% G' @, z. }4 i6. }
) c8 o0 t( h  i7.
- I6 g" I0 c; o  m  [. C$ g/ {8. public void run() {
. m* ?, o9 c1 [9. for(int i=1 ; i<3 ; ++i) {  P9 z- Y" k) S. M! k
10. System.out.print(i + "..");3 ^) ]- w# n% E8 }* r6 u
11. }
# T. c, \2 V4 x9 E12. }
; J) x& C" v: X$ _3 I13. }% v  h' O+ f& z8 e1 v
What is the result of this code?C
6 m; D8 n4 y* w( S1 ~A.        This code will not compile due to line 4
# l; U+ n8 Q: ^& x: N8 pB.        This code will not compile due to line 52 d( a! K; h7 M$ O4 ?6 M
C.        1..2..
8 N: G7 X4 C% L+ o9 ND.        1..2..3..
" [9 J1 s8 q9 A* P/ s25.        Given a local inner class defined inside a method code block, which of the following statements is correct?
, K* N/ `" [9 r$ `2 B1 S; w' FA.        Any variables declared locally within the method can be accessed by the inner class
; R: D2 k$ i. k& D5 oB.        Only static variables in the enclosing class can be accessed by the inner class8 q% w% ^$ f9 G* u& Y
C.        Only local variables declared final can be accessed by the inner class& Q( A5 C3 R2 {' A! y7 K: |/ V
D.        Only static variables declared locally can be accessed by the inner class.8 K, d/ T2 ^" z3 \6 m

' S! h3 f/ O! b8 X6 m& `1 r
8 R# s6 y+ q6 I4 ^. N5 g: v! r- C& x
26.public class SychTest{
/ v2 M! l& l: ~5 D, l0 E. L8 Z: E   private int x; * x; q/ ^4 F2 S8 t
   private int y;
2 U# G8 O' o) D. J" g- ]   public void setX(int i){ x=i;} " U  d! l' r4 ?  Z0 [
   public void setY(int i){y=i;} ) @) {, T( Y9 h% L
   public Synchronized void setXY(int i){ - o2 |, S: [; e" F3 l) d% @/ q
     setX(i);
7 @1 }  r* u5 \; G" z: x* Q1 P     setY(i);
) e! C4 t9 a' V7 b: a* `7 t   }
; z0 f: X# G/ S- Y- `   public Synchronized boolean check(){
7 J- }0 a3 S* c7 l' Z3 y2 j. Z4 q  o        return x!=y;   
9 B) i% W# y1 p* r: d6 @+ s0 D. Y$ Y   } 4 I$ X/ V3 e$ [9 x
   }
' t+ W% Y0 w, n9 j! h1 x7 D    Under which conditions will  check() return true when called from a different class?
  @  e/ ?$ q8 J$ c! \& ?   A.check() can never return true.
3 ]: E2 w6 Q: V. P- `   B.check() can return true when setXY is callled by multiple threads. 2 y3 [( z7 m0 d% l8 p# I# M* z: D
   C.check() can return true when multiple threads call setX and setY separately.
- n: G5 l" A; a; ^. j: }0 x9 R( v, W   D.check() can only return true if SychTest is changed allow x and y to be set separately.
6 Q/ @# m' d2 D; h# n+ m
4 F, M+ E6 m/ q& y8 d, ~+ A0 E27. 1)public class X implements Runnable{ $ _0 W" S0 ]6 {+ c" O
  2)private int x;
7 Q' L; D3 G* e$ C  3)private int y; 2 N5 w$ n8 T& H7 ~1 }5 T- p# H
  4)public static void main(String[] args){ * L4 _  t6 y' d/ P
  5)   X that =new X(); 5 H% K0 C9 J6 v2 P' W- V
  6) (new Thread(that)).start();
4 r3 K( a2 \: r& s) }. b. e  7) (new Thread(that)).start(); 4 M# w$ p' {  Q6 g, B% R
   }
0 @" Q% @8 j4 I6 Q, S  9) public synchronized void run(){
3 ?$ m. J4 ]. Z8 Q: k6 V  10)  for(;;){ . Z3 p7 g) E8 O5 Z9 i
  11)      x++; # g/ ^; y9 g0 K/ D' T
  12)      Y++; 2 t% o; T9 L- ?' U- b; x2 {/ Z) s
  13) System.out.println("x="+x+",y="+y); * @. k+ u0 `, c1 ~: t
  14)    }
6 Y+ u: Q9 A/ F" a/ \. P  15)   }
9 b6 z  L, S0 x/ |: Q- ~5 j: r8 E  16)  }   / w8 `# r; h1 z+ \; s1 C
    what is the result? 8 S( M6 X0 q$ J. c( v
  A.compile error at line 6 ' d: _1 I) W# \2 T; }( `0 ?
  B.the program prints pairs of values for x and y that are
. m2 x0 c$ W" s7 o5 f! u. [+ R    always the same on the same time
/ ~# j* b& w# J9 y/ i
0 n1 S+ B4 Y# K# `( k7 ?$ }; C28.class A implements Runnable{ % A- K3 L. C4 i! e1 T
  int i; % x3 F: b3 [9 V
  public void run(){ 5 B( I% T- U( y) r6 S5 O: Q+ u
   try{
3 H; W" O$ x" K0 _6 _0 x       Thread.sleep(5000);
1 }5 f' u' d4 C5 t        i=10; / U% x: O2 H& Y& ~# f0 Z* \. g
       }catch(InterruptException e){}
7 h& P/ ^) f) m: z0 _       }
* F; a3 U7 O" e! F/ N) [9 y7 B       }
+ x& m# ?, M. f  E$ }   public static void main(String[] args){ . @/ h/ o, Y+ D% D' T
      try{
: U/ I2 E0 Y$ k$ R: E         A a=new A();
. G% Z( m% b( k/ p         Thread t=new Thread(a); & P7 q& X: z9 G1 Q/ L1 ~& i: Z# U
         t.start(); / ?& Z: o% n0 ^- j* h; W3 t$ P
  17) & s- Y- l& T8 K" z! A- H
      int j=a.i; : T! f- B7 R) q+ {
  19)
7 E# C3 O9 b, P( |      }catch(Exception e){} / z, Z4 P( P- G9 i
      } $ G7 W* [, b; \3 q
      } 3 _( L" x5 X, x& i  A
  what be added at line line 17, ensure j=10 at line 19? $ ]$ ~6 x5 S% V+ t( U
  A. a.wait();   B.  t.wait();   C. t.join();   D.t.yield();   8 Q# e) F4 ?$ W) B1 s" I2 p
  E.t.notify();  F.  a.notify(); G.t.interrupt();
4 i, ^4 |5 F/ J7 x, }29.1)public class X{ 7 E$ e  Y. w6 e
  2)    public static void main(String[] args){
9 [; |# Y8 S" Q1 q8 _3 y  3)     String foo="ABCDE"; " t* G! q6 S5 G8 t# Z
  4)     foo.substring(3);
; t. p2 O7 w6 H6 l  5)     foo.concat("XYZ"); . o# r+ O: _' d
  6)    } 9 W8 s1 h' P2 f8 z1 }4 y
  7)   }
) \2 v$ L9 V% `+ X2 e; Q  what is the value of foo at line 6? , t* [# v: P% ]1 `2 `# I8 s; Z
30.  public class Test{
! t$ C# V; O  b* B& F   public static void main(String[] args){ 6 B. \. @3 p( }( E- Z
   StringBuffer a=new StringBuffer("A"); : ?7 A' W5 J6 x6 }& V; E
   StringBuffer b=new StringBuffer("B"); . b( Q0 D( V8 ]; i; ^/ I
   operate(a,b);
6 c3 N  l0 S. M$ H0 \: ?   System.out.pintln(a+","+b);   U. X; p& }* A) _" Q3 M" ^
    }   H3 @. `1 {9 ]% Q5 b: H
   public static void operate(StringBuffer x, StringBuffer y){
0 g/ h) W3 l+ N6 ]4 E8 U( r. h$ Q    x.append(y); 7 M0 |6 I4 {& ]; ]
    y=x; $ e, p5 h2 K( l" m6 K* B
   } 8 |; D2 T) u" s$ P! t
   } / V2 k# y) T* O  q. z/ M
   what is the output?
yangyu638 该用户已被删除
发表于 2005-3-20 12:49:05 | 显示全部楼层
感谢楼主分享
5 u. ?# B3 O' C% ?我已下载。。。。。
. P% Q% _6 i8 n6 s/ v' h* u特感谢您的大公无私
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-10-3 07:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表