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

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

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

方联scjp权威考题

[复制链接]
javalea 该用户已被删除
发表于 2005-3-19 18:54:17 | 显示全部楼层 |阅读模式
Sample TextJava培训考题. [+ a: ]- [6 g/ ?
1.        给出以下程序:& I0 l( [5 [5 Z( M5 D* J7 A* K, ^
1. public class Colors {
) d5 Z9 A1 d# l: o  ^2. public static void main(String args[]) {
. d+ E+ K2 d  ?8 J3 B3. int n = 1;& m# @1 e! f# F6 E5 e- J+ N
4. System.out.println("The Color is " + args[n]);3 W. V5 W- {5 p: N* u8 I# c$ s
5. }
8 G; H5 h; F$ V9 [6. }: k7 u. ]9 F" _+ c# H" C# F2 A
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”$ t: j: X; Q7 Q. X8 C3 I
A.        Colors red green blue yellow
; c+ ^# C, W8 M1 cB.        java Colors blue green red yellow
; ?  _4 J! h3 m7 _C.        java Colors green blue red yellow
; |  L7 P8 o3 H! F# p- fD.        java Colors.class blue green red yellow& M1 H3 x/ L5 f" }3 ^: {1 Y
E.        java Colors.class green blue red yellow
* F3 c3 j6 s7 s' }% H' F2.        读程序:
3 N1 p5 K. l, l- V$ o" E1. public class Test{  o4 B, y7 B; T; U3 w
2. public static void main(String args[]){
: q/ M0 p1 U7 w3 g3. char c = '\u0009';
$ j5 n% D0 j6 _  A7 E" L4. if( c == 0x0009L)& z2 X7 l! M9 k) f$ z. c" M# w; ]
5. System.out.println("Equal");
9 }" W( x& T9 t: j( F; N6. else System.out.println("Not Equal");
9 G) h9 F; P6 q7. }
, a$ T0 e7 B) J$ W8. }
0 O: m2 ]3 I2 a% P# R7 ?当编译和运行该类时,将会发生什么:
% y* Z; T! K6 nA.        程序因为第4行将出现一个编译错误1 g! R! x7 N# V( X4 E- L1 V
B.        程序因为第4行将出现一个运行错误
2 M8 A" V8 ~0 y1 u+ k  j; lC.        程序因为第3行将出现一个编译错
/ W; N1 ^# r; O+ pD.        程序输出”Equal"
6 {, U+ E( G( l8 i$ ~$ kE.        程序输出"Not Equal”
5 |& p" k& I' w2 E+ [7 D3.        读程序:
! ]" j; N7 p- p! v3 y1. public class StrEq{
1 o- I" }' v+ [3 ?' l9 q2. private StrEq(){
- W7 A, h5 L1 v% Q7 X3. String s = "Bob";
; m& ]$ E7 t7 |4. String s2 = new String("Bob");
* [; ]3 c: y9 P* I; g- b4 a5. if(s == s2){
5 f2 }) e. P: }3 O* F) ~2 p* u; ?  m6. System.out.println("Equal");
! G3 H/ ?3 t& L7. }$ b& N* X9 O: D) ^7 ]+ L! S# Z# s
8. else{
/ e' z) C7 h6 Z9 Z9. System.out.println("Not equal");
# x. }8 s/ f! _, _7 x2 c' \5 Y+ L% P10. }$ G2 J- ^/ n+ v0 O/ J  _* |
11. }
; W) r! G0 j: A4 c6 n: f- p: f' C12. public static void main(String args[]){
' [3 k3 k  J, q! V  P/ x- l! W13. StrEq s = new StrEq();
# L- p% ?. \; \6 P/ Q/ h9 O14. }
7 ]* X2 v5 Z' G  E15. }
5 F/ z9 s4 V# ~当编译和运行该类时,将会发生什么:
7 i5 y  M! G% P% s1 {; tA.        程序能够编译,并且输出“Equal”
& j: q: }( \/ o0 U+ m% v# cB.        程序能够编译,并且输出“Not equal”! Q9 y: l( r* j* e
C.        程序因为第2行导致一个运行时错误
3 e! Y* l) [" c: ?4 a. |; iD.        程序因为第2行导致一个编译错误1 }1 e# r' \: q+ f
4.        以下那个赋值是非法的:
7 n4 @, A5 j2 F0 u* h+ }! B4 IA.        long  test = 012;8 I6 S0 D( y( a' z9 z5 f: z  u
B.        float  b = -412;+ Y+ i7 M8 d. r7 z- l
C.        int other = (int )true;
: t3 B7 R) ], Z! p, J# wD.        double d = 0x12345678;
3 N9 x/ `- Y1 y! P) l; r, ]2 O/ I* j: VE.        short   s = 10;+ ]2 C% K+ h* _( d+ _3 e4 g+ F
5.        读程序:3 g' Y0 w2 W2 f$ b
1. class CompareString{: F2 \" |+ D, S' @& R1 |
2. public static void main(String[] args) {
4 ]& T  ^8 e9 n0 K* @2 M3. Integer x = new Integer(5);: N- {; A: I# Z, F( }/ x) ]
4. Integer y = new Integer(5);
" H3 v: l% Q8 w4 D# Y- i' U8 t- c5. Integer z = x;$ @! x8 G% \' f6 Z
6. if ( ______ )  O. o0 Q1 o* V8 S) `
7. System.out.println("true");; z5 X! ?" a! @# ~
8. }
" n9 ?+ e7 k, d  n1 X7 x" V- h, D4 q9. }
7 g6 i' c) P$ b/ x3 u' }以下哪个选项填入第6行后,不能输出true:
3 }: k9 c' g7 N6 L0 @A.        x==z5 j- e7 M$ g" y: N
B.        x==y* p+ S7 Y3 \- h! {% |6 p; i. x8 {
C.        y.equals(z)$ k" w: U4 u) ~6 x! T
D.        x.equals(y)* }  [# A4 r2 W1 w* p
6.        读程序(双选):
/ j! a* C- L; ^" R0 [1. public class Declare {4 d' Z# r( V+ _% L& d: N
2.6 E3 u0 {& P# |$ M. w9 I
3. public static void main(String [] args) {
, ]: g  `( _' d5 O' Z% K- V4.
' k' w3 D! P$ r$ A. U$ B1 }5. System.out.println("The variable is " + x);
' k% a$ ~* q( L" d$ j$ K6. }
( T9 t  J2 e# C8 O7. }. {# B" b# q' b
以下哪个选项中的代码可以使程序编译通过$ @2 B2 w: A5 i8 k, o
A.        把“int x;”放在程序的第2行
8 J) S) [9 P* IB.        把“int x;”放在程序的第4行0 H2 X* t) r1 C
C.        把“int x=5;”放在程序的第2行
7 W0 c& {8 X$ bD.        把“int x=5;”放在程序的第4行
; D0 A$ @3 {. e" `. \1 z, dE.        把“static int x;”放在程序的第2行
- c3 j0 `& J2 }) S" O5 P, c6 I  \6 k6 t3 d4 L  {$ G7 o# @3 y

+ g! z% Y4 v- H" k6 Z0 S& M1 F2 q/ C4 M2 j2 M1 E2 o
7.        读程序(双选):( g. c4 [6 G: V2 U% e6 l0 I
1. public class Child extends Parent{6 S: c1 |, W( i5 K: G
2. int a, b, c;1 j, J1 m- L  A& h! J8 r
3. public Child(int x, int y) {
% ^: k) q- z# W& l4 Q+ x1 b4. a = x; b = y;
9 L7 r4 Q; O4 g) W( q& o' K& K5. }& C( p5 G0 j/ w) {7 I. k* z
6. public Child(int x) {
3 O0 a! ]+ q9 b- Q; H7. super(x);
* `/ ~. P5 B9 ?4 l9 }8. }
; g. Q  `3 E  s* p- h/ X9. }* v# w. e$ K% ?# K2 b( t
为了能使该类编译通过,下面那个构建器必须出现在父类中:$ w5 q) a  |" t6 x3 I2 Q  v
A. public Parent(int m,int n)
! W! `) D' ~8 `( r1 G0 m( pB.public Parent(int m)3 N8 {- b. O  q. O
C.public Parent()
7 |. f: H, z1 ~4 I+ KD. public Parent(int m,int n, int o)9 s9 E( J; b& e1 @
8.        读程序:
6 K/ M0 P* a3 ]5 K5 M- i1. public class Derive extends Base{3 q& ]8 L  k4 T0 t9 b7 N( J2 M/ E
2. public static void main(String argv[]){
# G/ `3 L4 ^1 |- {" a8 A9 r3 b3. Derive a = new Derive();" K7 z7 E. f1 h
4. a.method2();* E% @3 S* P& {& p. q9 x3 ~+ w$ F) h
5. }1 K* C4 [+ M; p# F
6. public void method1(){/ v+ t1 o" Z5 `; Y
7. System.out.println("method 1");. k# b! N" ^6 w3 l5 e
8. } & j7 C; ]* t& Z) O% f9 B9 U/ R
9. public void method2(){& U" I( h4 Y  j4 w; g3 T6 k8 h2 b& k
10. method1(); : F- `2 `3 y' [7 D# Z! \; W/ C
11. }
* W0 y" E; i8 o& Y5 @12. }3 s+ Y- M& t. j3 `6 R( t
13. abstract class Base{
- j) W4 D) o) \, S- U) K14. abstract public void method1();0 c. v0 L1 A( B# }* D* M1 w
15. public void method3(){: T2 T# y- @( e# J( E
16. System.out.println("method 3");( m- y8 G9 _. s0 a5 p5 i% w
17. }* L+ R( K6 d6 d3 r4 U, h8 M1 ^$ W
}; T5 d+ F2 R# u& ?
当编译和运行该类时,将出现什么:- E; U3 v3 M3 b( _' P( @# u6 w. A
A.        程序输出“method 1”
' w6 \3 g  H' l- `* E9 RB.        程序输出“method 3”$ n9 i' v; d% w) |- g& l8 X0 b
C.        程序因为第15行而出现编译错误
4 A7 q" H5 T0 m* k( ~, Q4 M5 v* vD.        程序因为第15行而出现运行错误% @2 E& I/ b4 P' L. n( ~# W: m
9.        以下哪个接口的定义是合法的:4 y/ v' |% F# ?. i2 j' l6 Z
A.        public interface A {int a();}& ^8 B4 [! C/ a9 {8 s  ?
B.        public interface B implements A {}7 q. Q! K7 _, S: E' V7 J$ P& U* Q; u
C.        interface C {int a;}
! [' N4 q) [1 x% ~8 xD.        private interface D {}
. U5 E- }$ {3 U# @; |" p10.        读程序:4 z) I( H8 X7 r: n* l. H
1. //File SuperClass.java
- g) r- _4 P/ h4 x/ T2. package MyPackage;3 X3 w( c9 X4 V: \. e/ J$ v# f
3. class SuperClass{2 @9 f- A' S' ?1 ^
4. void myMethod(){
4 j5 e" f8 H  Z* Q2 ]5. System.out.println("SuperClass");% F7 c3 {4 u4 u+ N& g8 R
6. }, q" Y  _3 L; z+ x( @( x  A
7. }: d2 a0 H( @; Z" V* ~* [5 A
8. //File SubClass.java1 t) Q" Y8 f- A8 ~3 k* p
9. public class SubClass extends SuperClass{8 I* p- c; }$ \
10. public static void main(String[] args) {
  z( |7 Q; ^5 Q8 B, _7 c! X0 R11. myMethod();
( W, T1 `+ A: ]( D$ l# _12. }! w( a$ u( G  A) z9 a
13. }
' v# X) z) r: n: S) ?4 [当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:% S+ {* ^: S1 p- ]# r" `
A.        两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”' Y6 k- L  I! z* d0 o2 @" O: f' _+ ]
B.        两个类都回出现编译时错误
# S9 m" J! v; V) J' AC.        两个类都可以编译通过,但SubClass会在运行时出现运行时错误$ B1 C2 a) y8 ^% J
D.        SuperClass.java可以编译通过,但SunClass会出现一个编译错误
( \3 A/ o# n( J5 O5 R0 \9 ]8 p11.        读程序:
. J; Y1 Y% U+ x- l; Z( T% C- f1. public class Test{' n: e% F# P2 w
2. public static void main(String args[]){. k+ O$ B7 H! b* |
3. int[] i = new int[5];+ f3 C# t2 A. J8 V; V/ J
4. System.out.println(i[5]);# a: ]$ q; r0 K! }; ]
5. }& I9 n4 L5 B6 H& i$ B
6. }  Q9 K2 l! d+ J7 a2 l6 J3 Z
当编译和运行该类时,会:! J7 @( x) l8 C
A.        出现编译时错误
! x9 d" `8 n1 x9 X9 e  ^B.        出现运行时错误  i2 @5 N2 Y4 ]; S8 V2 H% r/ S! [
C.        输出0
# e% U' v) v0 {+ JD.        输出null. C; @$ ~$ ]% J8 N0 X
12.        以下哪个选项是正确的数组声明或初始化:. J5 m$ C7 z' z( h
A.        Array sizes = new Array(4);
" _* O% t& v) B' I& s0 M* tB.        double [] sizes = {1, 2, 3, 4};6 A! e* {6 J: C4 X/ a) A& X
C.        int [4] sizes;
  R. n5 E6 p9 N) z. G7 WD.        long sizes [] = new [4] long;
; x: Y* r1 R  l3 _# \+ e. Q6 a13.        读程序:8 O3 q% t  k" k
1. public class Initialize{
. P8 P% v: I. t9 F6 _7 ]! m2. int x = 200;3 E& R4 O6 L; O$ Y# p7 x6 C$ X% t
3. static {
/ d/ k/ [; x  H& _( B6 J1 q+ p4. int x = 100;' P6 H0 R( l: l2 V3 E  ^
5. }8 H  g& l7 Y5 v" s& N
6. public static void main(String[] args){
+ w: k5 J0 K. h7. Initialize init = new Initialize();: h- h) t! |# i. K
8. System.out.println(init.x);
: N( ]& m8 w. R# K- t7 O! _9. }
  c: e  L* v  T& B4 b; H10. }  r1 o+ J4 M% X& P% r
当编译和运行该类时:
& p3 @# b% Y3 g) G, e" b8 BA.        程序因为第3行产生一个编译错误+ i' V* {; c; r0 u, y+ y5 O! [0 e
B.        程序因为第4行产生一个运行时错误
" D/ T# O( ]" @0 l, F0 C( aC.        程序输出100% ^$ B& ?. r! c4 J1 X
D.        程序输出200
3 d0 e& C6 G2 k$ O& D* v# `) f14.        读程序:% u+ }" H3 f) b7 ]
1. public int abcd( char x) {
* U) @$ I9 @& U2. if ( x <= 'M' ) {6 c  S6 a' l# r
3. if ( x == 'D') 2 v) s: H! M! P9 K# ^6 O! K9 E
4. return 2;# e' M$ D: M1 u# |( p+ Q% b/ h* @6 }
5. return 1;
) i1 g4 b$ _. U8 _& H$ O8 w. B4 Y0 o6. }1 k5 A: R8 J' \4 i
7. else if( x == 'S') return 3;6 e, B4 N& O7 q/ \
8. else if( x == 'U') return 4;
" y. E$ p) y0 ]9. return 0;3 E8 g( m# X! ~" n' w) q6 h+ Y
10. }
9 P9 |5 t: ~$ R* t9 S7 j% c在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1
. b* X( {9 k1 B+ t/ {' hA.‘X’
% S( `- I5 ]3 o+ k4 E! CB. ‘A’
. _  i* x# L, X( ~. UC.‘D’
/ m7 R: E/ z& t7 T8 a. ~8 @. G" hD.‘Z’; X# ]# D' D  r
15.        读程序:
  y( d, C9 C2 N  y# o4 U& V1. class TryTest {" }9 s! v! b' `- w' M" f# J
2. public static void main(String[] args) {# s# O, j/ @0 Y  a7 I$ K0 l( k
3. try{
8 a; s% E  A* h2 {$ {' L4. System.exit(0);# F  i5 h% r9 S) m) x/ j
5. }
, l& g* X4 e6 w8 z$ l6. finally {  H/ a& }+ l4 E# F
7. System.out.println("Finally");
, h" O+ g  ]  h. n2 T* F8. }; n% t/ M' {2 z: v, Y& j, J$ |
9. }( b$ W0 B: Z/ a. V
10. }" w& F+ u! Q7 P4 X. \$ Q, t
当编译和运行该类时,将:4 N  l/ S! F3 O# m1 c/ W, K. R( S; p
A.程序因为第4行出现编译错误6 P9 h5 g4 ~6 T; _& Z+ E$ a
B.程序因为第4行出现运行时错误0 C8 ]+ j( Y: q, w* L+ t  j4 ^) ]
C.程序因为第6行出现编译错误
  g% h3 F2 r3 C2 i$ A% }D.程序因为第6行出现运行时错误8 Y" g) y+ {/ _' _3 r
E.程序输出“Finally”$ T. o# T/ E3 {/ u! [0 l3 e2 W
F.程序可以运行,但什么也不打印
. t1 Y- p6 C7 y( [/ P$ B5 d& z7 J# c" |0 X, s
16.        Given the following variable declarations:
, ?$ P( X  h2 n# _Boolean b1 = new Boolean(true);
2 K5 P) _0 L0 C( u) U; a9 m( pBoolean b2 = new Boolean(true);" ~$ G# d" i# U; x# \1 r" x
Which expression is a legal Java expression that returns true?
! ~$ I5 m* t; o. @( v6 |$ QA.        b1==b2
) M+ S8 A' u+ |2 _B.        b1.equals(b2)) O" K  G/ r0 R; S6 X3 A1 w0 N: E
C.        b1&&b2
5 P. O9 k5 k3 @; z  aD.        b1||b2
  J" r9 _' Q4 I$ QE.        b1&b2
+ R: H$ B6 M  F: V0 oF.        b1|b2
8 d: B5 V" s+ T% a5 |- f17.        Given: 9 k2 G0 T6 t9 A- g
1. public class Employee {1 q" n! }. ~2 X% E4 u
2.
: u- i5 j' Y1 [3. }# Q- s$ [; _2 P
4.
7 _9 r8 c. V/ V9 z5. class Manager  extends Employee{8 b3 @7 w7 W8 d0 o; J1 a% J2 B
6. }
# G3 I6 e* L/ B% X( \1 f9 x2 e( SWhat is the relationship between Employee and the Manager?* q2 \% y, e6 j" e9 F( X0 p2 V
A.        has a5 w: ~4 M6 A' n! b" r
B.        is a
2 k, K% |! t1 l& Q- QC.        both has a and is a
7 h3 t0 g$ t( a2 x' z6 `9 u& aD.        neither has a nor is a9 V5 c: F( ]: s$ v5 P# [
18.        Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?4 M" L2 R& Y7 Q9 x$ w' |3 N
A.        BorderLayout8 |5 L8 Z, Q1 I4 H& Q0 F
B.        FlowLayout
! [9 F/ O. \) B( r7 Z" nC.        CardLayout
/ `% W0 y+ f3 C7 {: {  X- e5 d& hD.        GridLayout. X, M4 Y: p. y  \
19.        Which of the following AWT components can generate an ActionEvent?: f0 T) p+ L7 w9 E/ ?. Q
A.        java.awt.Button
2 P1 Y  S# M* Z. ~% h0 W" hB.        java.awt.Panel- ^( V8 r" p$ v- o
C.        java.awt.Canvas2 h, n; A/ y2 ]9 p7 o
D.        java.awt.Scrollbar
. Y( s$ [) Z. u+ ]4 w1 y20.        What kind of Stream is the System.out object?4 W/ F8 s, X( e8 L6 m
A.        java.io.PrintStream
0 f+ s2 K0 M5 {. t$ P/ p% RB.        java.io.TerminalStream
. Y- `& Z( {* B8 r% CC.        java.io.FileWriter
+ Y8 l: @3 N" J9 b: F  W% A* _D.        java.io.FileWriter
8 E8 j  _& }% z3 e; Y! y21.        Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)
2 |/ m! c: Y) P7 L; ~' IA.        java.awt.event.FocusEvent- Z, i* z0 V1 e$ h
B.        java.awt.event.KeyEvent
& ~  B) l  B$ o" O" m/ |  KC.        java.awt.event.ItemEvent1 ]7 d) F/ ]1 D+ b
D.        java.awt.event.ActionEvent
  r$ h* [& M% c6 I& V! U+ G8 j4 \22.        Which of the following methods are static methods of the Thread class?(双选)" X4 P) y  c2 i3 [# h* m
A.        sleep(ling time)5 }: b, p( Y, _+ F& K
B.        yield()
7 V% a/ O3 q9 b: v- oC.        wait()
! N+ h. W! F" T0 ?( A8 vD.        notify()( `/ J4 w- l$ _# |  i( s1 W
23.        Which method do you have to define if you implement the Runnable interface?
# \% q# f! i5 ~0 N/ DA.        run()7 j. I, t* g$ p2 h* j% {
B.        runnable(). `& W- B: {3 F+ o& [* u/ y; d1 E
C.        start()
' s3 J* s  i' X) x! ^D.        init()
& c5 h3 J, T; r2 I" u- QE.        main()8 v7 f$ ?6 @' ]7 ^1 t$ e( w" T% i
24.        Given:- b) k& Z) [& b" u
1. class MyThread extends Thread {
7 G% @" [$ g! y2.
$ t1 U% Q0 X9 i3 q3. public static void main(String [] args) {% U. h0 i" _; o4 g, t' E$ J
4. MyThread t = new MyThread();6 c( Y6 c5 F2 A8 z
5. t.run();
% e/ d1 B2 O# Y- s* j8 {! ]5 ]6. }- J. u& P8 G" F7 ~5 c) ^
7. 9 h, O2 p7 y; p. K
8. public void run() {
: N6 f& T' J3 m. y6 Y: J. B9. for(int i=1 ; i<3 ; ++i) {3 T3 U' Y  R  h5 _
10. System.out.print(i + "..");
! X6 c9 [' I7 Y: B5 |7 Y3 @11. }
4 k4 H2 O) f3 S! c12. }
+ Z% @/ p+ l# T0 O13. }2 I) A2 b( K& }# o
What is the result of this code?C
) `" F- Q7 b6 {' G: hA.        This code will not compile due to line 4+ X1 I3 c+ M- g( a, ]
B.        This code will not compile due to line 5" `, K! ~$ L( |# a9 ]
C.        1..2..
% N" N3 c8 a) ]. ?9 V4 KD.        1..2..3..
: J) s6 Z% W1 t8 P0 ?4 d7 C25.        Given a local inner class defined inside a method code block, which of the following statements is correct?
- D" z$ J1 V; O9 i2 xA.        Any variables declared locally within the method can be accessed by the inner class# Q) E$ \0 N0 r5 a
B.        Only static variables in the enclosing class can be accessed by the inner class
4 j& N/ W; p. X4 z1 c1 I$ v7 PC.        Only local variables declared final can be accessed by the inner class
* G" e5 N! {/ F* W( m. F, QD.        Only static variables declared locally can be accessed by the inner class.; N; Z  [! P* C3 g: M+ N8 n7 H; j

. f( p" c" s/ X* s6 [/ c4 e4 m
3 f; Z/ F. q0 U& n$ ?% C) `0 N( c/ s, I6 D+ D
26.public class SychTest{ 6 l6 }+ g) r  _, }% ?8 W& j
   private int x;
4 U* s" i2 h1 c, h- {7 s   private int y;
$ r; y* }, T$ {6 e7 k   public void setX(int i){ x=i;}
# C+ t1 i" }& v   public void setY(int i){y=i;}
1 b3 Q- @0 r2 z   public Synchronized void setXY(int i){ 3 G5 U8 e9 a- d* s1 `. [6 K
     setX(i);
8 |1 m7 n# J" E% f0 G" T" h     setY(i);
% v7 P9 `5 X9 U) x3 X5 w   }
( I" `* r8 h0 R' f   public Synchronized boolean check(){
0 S$ u4 D; h# [  A        return x!=y;   / r; C; H* _4 a; F- G
   } 0 ?9 m3 @$ i" D1 {! j& {
   } 3 p3 m4 l& B* F8 e; b
    Under which conditions will  check() return true when called from a different class?
$ [! b; z6 o* M2 h1 c/ P   A.check() can never return true.   |$ P0 H4 B5 ?/ b/ K
   B.check() can return true when setXY is callled by multiple threads.
0 d) {% Y! M5 v: z8 I+ m0 F( B   C.check() can return true when multiple threads call setX and setY separately. : b8 ~7 e7 b5 p) H9 w+ W3 _/ H2 S! k7 |
   D.check() can only return true if SychTest is changed allow x and y to be set separately. 2 Z# s5 D  c, a' i1 G
2 x3 n' }  J! f1 }, c2 o
27. 1)public class X implements Runnable{ * b* m( [  ~  P4 P( h
  2)private int x; ' ^, d9 O+ I) v& T2 _% f7 ]
  3)private int y; $ W; D% \: O0 s; b. s
  4)public static void main(String[] args){ 9 u- w$ a& w# f* c3 R  @. z
  5)   X that =new X();
! H3 c% A  e- @4 v  d) B& D  6) (new Thread(that)).start();
! G7 Q2 m* U4 W8 C# F9 ?2 y  7) (new Thread(that)).start(); 8 F" l2 O. T# J
   }   i! e/ B( {) c2 F) u3 ]( a7 `9 r- {
  9) public synchronized void run(){
/ f3 u% \- `9 i% F  10)  for(;;){
* n3 m9 A5 Y4 Z: z, \7 y0 v" F  11)      x++; 6 S+ k  D0 D8 |. U: c( p
  12)      Y++;   O6 n$ r: I5 L+ G7 B
  13) System.out.println("x="+x+",y="+y);
3 `+ o. |2 B+ E3 h5 c  14)    }
. Q' F2 V! p- i6 A  15)   }
) M5 T  ^$ w2 i- L8 S# h0 o* U! l  16)  }   0 \4 h7 j5 L1 V5 E4 T6 g
    what is the result?
& G2 h6 f4 a+ B: b- ^  A.compile error at line 6 7 j! o  @3 f# ~* I2 g) V
  B.the program prints pairs of values for x and y that are ' V  n2 W# k3 u/ h# ^7 y: n
    always the same on the same time
9 ^8 h  f' O9 o; p* ]- Y( w  a% Z2 C1 E+ W
28.class A implements Runnable{
  v3 C/ H" A3 \* O; x9 J# u  int i; ) B- F5 d; M* R1 `! a0 `
  public void run(){
+ V" n: p4 n4 a  Z8 c1 ~$ m   try{
  w/ ~* R5 w) ?' b, P0 Z: [  ~. N       Thread.sleep(5000); 7 {$ E- |6 u3 l& W3 V
        i=10;
3 r; |$ Y; Q. p+ ^% L       }catch(InterruptException e){} + \- b; g# C; Q( b
       }
; h, \3 G: J! k3 ~) V: L. U       }
) T/ J2 g' t. p& w6 Q5 f   public static void main(String[] args){
  r1 h5 i, f! g) D0 c1 u0 D      try{ 2 B5 m* ?/ V! C. r  `
         A a=new A();
) ?3 G# O* w' h         Thread t=new Thread(a); 6 g* d. N9 ~) I+ I
         t.start();
: P& J5 \9 E7 }0 Z; a1 ^  17)
/ @5 Y. R3 p; K5 A/ x) Y2 ^      int j=a.i; 8 j. ]" R9 S; ?* P. m0 r
  19) # d0 q, r! I2 J9 B: y
      }catch(Exception e){}
* F! J2 ^' j; ]$ y      } + u' _" s' k: E4 ?$ {6 C
      } 0 ]7 L$ t8 V3 i6 t8 s7 v
  what be added at line line 17, ensure j=10 at line 19?
/ u; \, E) g" g' i1 A# X  A. a.wait();   B.  t.wait();   C. t.join();   D.t.yield();   
9 E- x& q1 K3 I" f4 |/ h  E.t.notify();  F.  a.notify(); G.t.interrupt();
& J" k% \) M4 D29.1)public class X{ % v2 a5 w. z: F1 B# u, m
  2)    public static void main(String[] args){
* y6 h! B# o" g# l0 U3 W; y2 J  3)     String foo="ABCDE";   |) M$ A1 ?3 {3 _3 [$ m
  4)     foo.substring(3);
6 N' V$ m! P: n  5)     foo.concat("XYZ"); * e# x5 ]; F% b
  6)    } ! h/ d3 f/ w) N
  7)   } : U" C4 H. c3 U- D
  what is the value of foo at line 6?
, o' J* s' {& `( _' N' ~/ s30.  public class Test{ 8 [/ M# o7 _  L+ ~+ K
   public static void main(String[] args){
! W# [5 K$ v$ }$ L" B; S   StringBuffer a=new StringBuffer("A");
! S: ~5 l! z, p$ g' q6 s- n0 L0 H8 i   StringBuffer b=new StringBuffer("B"); / B2 e5 d' O  m; c/ {, ?
   operate(a,b); ) U% g1 L- \, M: f2 I$ j: y3 f
   System.out.pintln(a+","+b);
9 D5 }! k0 {( ^, D, U    }
2 I7 h/ M9 F: i' h: K( U$ C   public static void operate(StringBuffer x, StringBuffer y){ 1 i# p( [7 A6 |; l+ [; H+ K" U
    x.append(y);
5 c3 _: W* @# }/ H    y=x;
5 s$ ~' l( y) p' k   }
3 ]2 P+ f$ Z) J, J8 ]   }
* o% P! u1 `* B: ^+ x   what is the output?
yangyu638 该用户已被删除
发表于 2005-3-20 12:49:05 | 显示全部楼层
感谢楼主分享
8 q( K% n/ i: [1 U8 [我已下载。。。。。) |0 c. y* b7 B; O
特感谢您的大公无私
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, 2026-2-17 13:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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