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

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

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

方联scjp权威考题

[复制链接]
javalea 该用户已被删除
发表于 2005-3-19 18:54:17 | 显示全部楼层 |阅读模式
Sample TextJava培训考题/ g/ Z2 }1 o: K  u* o' U
1.        给出以下程序:" _' G% ~( \6 c6 m1 \7 Y
1. public class Colors {- v' f2 |4 Z$ _6 R3 X7 j8 V
2. public static void main(String args[]) {
; K5 y& N# w4 X: A3. int n = 1;
/ ~9 I0 n- c# p: w  e$ [" |- m/ v4. System.out.println("The Color is " + args[n]);
8 s) l4 m2 u0 N5 I/ S3 A  Y1 T, j5. }
; s: M2 t. z$ m9 Q' L$ ~6. }* H! G2 t5 V$ T# w1 U
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”  Q& v0 x& y! \3 @  J' _$ N2 H
A.        Colors red green blue yellow8 r; T3 k$ s( S" b# ~+ X% x# q
B.        java Colors blue green red yellow0 _0 g( a( B. }
C.        java Colors green blue red yellow# N! t$ E) d( r, q/ k: |
D.        java Colors.class blue green red yellow. n- ?4 L7 v; x: O/ G. m
E.        java Colors.class green blue red yellow
; ~! s; h& a7 Q. E2.        读程序:% r( o0 L9 K1 j
1. public class Test{
1 K- d% m& h4 f9 r2. public static void main(String args[]){9 T7 J. n% `: X9 N9 W* W6 a
3. char c = '\u0009';
0 h9 V3 }0 d6 D4. if( c == 0x0009L)8 a  S1 _+ m  I
5. System.out.println("Equal");
0 I  Q( n6 N+ k/ I) w6. else System.out.println("Not Equal");/ E2 h0 J2 \% k: e1 H/ l  ~$ S
7. }
, ?+ f3 Z# [/ v9 _. c4 F: }! d8 Y; C8. }/ k3 F$ M' f; Y) z4 E  o
当编译和运行该类时,将会发生什么:9 {; X: j0 j6 o0 r% R5 }
A.        程序因为第4行将出现一个编译错误
! X* Q( `. c* ]. d$ ZB.        程序因为第4行将出现一个运行错误
* Y( T/ B- i: n- \7 F6 GC.        程序因为第3行将出现一个编译错! U4 l7 B% L9 O: @0 s
D.        程序输出”Equal"
! g9 f, k- Q* b% D2 qE.        程序输出"Not Equal”
$ y2 f  z- j6 }' Q7 T) Y3.        读程序:
; S! A1 M* A7 \) }5 s: A1. public class StrEq{: K* p$ R8 a, v2 Y' F
2. private StrEq(){
& t# S6 J+ n; W* r/ ]3. String s = "Bob";
# i+ N, G- S; ?4. String s2 = new String("Bob");
5 ~# l$ v8 E$ E- F4 F9 n5. if(s == s2){+ L! M' O* L4 i
6. System.out.println("Equal");, c$ k/ Y. B1 J  o# I- S6 g0 a) p2 i
7. }
" x  ^) G6 r/ F9 d8. else{
8 \+ ?' z' ]  q* e9. System.out.println("Not equal");/ {' Z% P  K( [& W
10. }
& T* K' T3 k5 A5 N9 j; |+ T11. }4 S1 _4 M7 z+ [/ |0 g) t
12. public static void main(String args[]){9 w8 d9 @0 A+ s( F7 i7 S  u$ U
13. StrEq s = new StrEq();4 h# u8 @0 A8 x2 C" l
14. }
. V% o; U8 p( n5 d% [, T& Z* L+ L15. }
8 ]/ P7 [$ n' p2 r; |" @当编译和运行该类时,将会发生什么:( {  D4 j% c3 S* z( {! _  N4 j' j# ?
A.        程序能够编译,并且输出“Equal”  _& P" c" [6 R% }# z$ R( F
B.        程序能够编译,并且输出“Not equal”9 j  F( `/ T3 Q, r3 B  q% U
C.        程序因为第2行导致一个运行时错误5 C  E( d5 C. f( X6 i. l
D.        程序因为第2行导致一个编译错误
* I2 H7 @# Z& M# r% o' @2 [4.        以下那个赋值是非法的:
3 R2 c2 ~: O% `' S! OA.        long  test = 012;" n, G! s5 _3 |- P* \( v- v
B.        float  b = -412;. {$ p; K/ i  l* G
C.        int other = (int )true;, v. R' Z. `- a; r; J, @' [5 c2 w
D.        double d = 0x12345678;! C5 R; C8 H8 F$ H' ?+ E) C# V4 x
E.        short   s = 10;
& {/ C2 L, T* E4 B3 e5.        读程序:2 r% }# F* d: M) l7 h
1. class CompareString{% A3 j0 O0 E" d+ D
2. public static void main(String[] args) {
( x7 e' f5 K; q) b  R$ r8 Z3. Integer x = new Integer(5);
2 q5 [9 h2 N0 p  d) U  H4. Integer y = new Integer(5);2 r& j$ Y- h" D/ I; K# \
5. Integer z = x;; W# @) n! z5 J( D5 `
6. if ( ______ )
2 I" h$ {/ r4 ?4 r. w! }2 O7. System.out.println("true");9 _  t2 w" W7 A, q3 c0 Y
8. }
" U$ {6 p. _( E2 t/ H, V' ^1 J1 f9. }; `3 N( V: P3 D+ z& i+ F( N
以下哪个选项填入第6行后,不能输出true:
! ^( P4 N1 x% g6 x+ zA.        x==z
9 |$ d* U9 c( O* r: P. HB.        x==y
9 H, Q; N! u9 CC.        y.equals(z)
+ j! S8 Y5 k& E2 G- ~' wD.        x.equals(y)
1 [0 K5 l9 B/ c/ J! R6.        读程序(双选):: ^, U6 X- H2 D) V8 y, v1 `- |
1. public class Declare {( u2 {$ Y1 \' {, I  [' ?6 D  d! }* p
2.+ W) `! o. f" s8 g+ G8 `
3. public static void main(String [] args) {
( i. ]3 e) ^7 L* X9 ?4.; ]% i7 G1 d: }/ z' K8 D' m
5. System.out.println("The variable is " + x);8 F! I4 C1 w+ d2 B  q. `
6. }. M. L5 d$ r& K: ]; z5 h
7. }4 ^3 o1 @$ ~1 L7 F5 Q- m' |
以下哪个选项中的代码可以使程序编译通过1 ]' Z3 j* d0 ?) ^  V
A.        把“int x;”放在程序的第2行
/ z; m% K/ I. V  |B.        把“int x;”放在程序的第4行9 Y, Z+ G. V3 d7 {" X0 W
C.        把“int x=5;”放在程序的第2行
4 ]' m0 w# Y# i$ p% nD.        把“int x=5;”放在程序的第4行
; ]- B- Q0 r/ k( hE.        把“static int x;”放在程序的第2行3 T7 w* l3 V3 s6 V, P

4 A/ ?2 ~$ A7 _8 u; v- Y$ q* P- I, t+ j. C9 }
  x5 l2 Y. Q* @% @$ A! E9 T+ F
7.        读程序(双选):
+ c) i: Y# f9 t0 u1. public class Child extends Parent{% G  K$ J9 Q. h, y8 @( ]0 h" O  u! [
2. int a, b, c;' c# D% D2 A8 V, A1 ~
3. public Child(int x, int y) {
* _5 Q  V- v0 [" }+ E# A4 m$ x* e, |4. a = x; b = y;& E* a4 d3 s9 f. F& P
5. }
; z) m- I, W4 j6. public Child(int x) {1 ^3 D: e5 ^, o! q! j7 A- P6 f
7. super(x);
, A% c9 s4 N# \  I3 v) D+ a8. }4 M% A  \) K/ ]1 w$ Z6 A. H1 s
9. }7 ]4 D& }4 x; c9 D8 B* a
为了能使该类编译通过,下面那个构建器必须出现在父类中:9 z" Q- ?( F1 D9 \
A. public Parent(int m,int n)! K- U8 y( d% E! M3 z$ z0 I5 g
B.public Parent(int m)0 ~5 I# [) w5 G' x( S
C.public Parent()4 B1 I( M6 v- ^, f( A* o
D. public Parent(int m,int n, int o)  p! C0 ?+ M3 S: n) P- Y5 n9 F2 D4 m
8.        读程序:
- ]) M( `. U9 Q" J; c  ~1. public class Derive extends Base{1 x2 I( W" G3 A) v* D' I! F. [
2. public static void main(String argv[]){
  p7 q+ c! N" }& A) J2 I2 I, Y- O) `7 t3. Derive a = new Derive();5 A8 P7 `6 U+ y) m- b
4. a.method2();8 v% S% ?) l7 @$ W7 h5 X) J
5. }
; N) T, ?' d; P- S. r& N' Y6. public void method1(){
* J+ a( k6 a) A6 q) |0 X7. System.out.println("method 1");
! K2 j- s" E$ {8. }
! C; r' c5 U; }1 @- q* }9. public void method2(){
& M) H5 f* v7 U# N  t10. method1();
: I: V. h7 _% J3 j, i# E11. }
1 K" O  x  ]" E" d12. }2 m0 ?' T" K) b+ U; K( \! C3 R
13. abstract class Base{. U8 @4 {! D8 ^$ |
14. abstract public void method1();4 @6 Q( L5 P% P+ I( Q6 O1 B' ^
15. public void method3(){
7 x  P9 v* ~( m0 L& D) i  @  W. }% ]16. System.out.println("method 3");- v  M( o# H; x& _: l
17. }: s0 p7 r  L  ]
}) r# Y1 F9 ?6 d4 z; {/ j5 S. U
当编译和运行该类时,将出现什么:
7 Y* R7 L8 c3 R4 C, _! b5 f" ^' `A.        程序输出“method 1”' ~1 L' a2 W( v- u
B.        程序输出“method 3”
- m) z( `* q( E5 VC.        程序因为第15行而出现编译错误4 z" U; [& \$ P. G$ |9 T( K. B
D.        程序因为第15行而出现运行错误1 S' y4 J# Z6 c* m3 d
9.        以下哪个接口的定义是合法的:
5 K) a1 t% J, ?6 ~A.        public interface A {int a();}
+ ^8 C! B# P8 k3 VB.        public interface B implements A {}0 R7 L$ c% @0 I+ p  b4 i
C.        interface C {int a;}
& X- D! V6 V& JD.        private interface D {}% t5 n3 d% A" C# o! u; r/ m2 b* U5 O
10.        读程序:
$ P( m0 S6 k! L7 A; K9 ^2 {1. //File SuperClass.java/ r' V+ N7 ]6 L3 S2 O/ L; q
2. package MyPackage;
: f( k. V8 x% D# s% r! R3. class SuperClass{! h+ i1 ^2 c" h, x( H& B5 E
4. void myMethod(){
: P3 H; n0 A1 l/ B0 x/ n7 p) a5. System.out.println("SuperClass");. |& d' q+ g' n9 @6 W6 |
6. }
  C. g( F* ?6 y  F# \1 O7. }
& S2 O3 N5 a" k* u* z- s8. //File SubClass.java
( _+ [3 _: @0 [' ~9. public class SubClass extends SuperClass{3 k. ?9 u; d" F+ y
10. public static void main(String[] args) {" J* [' O7 c& e! T" R; ^+ h  p. E' v
11. myMethod();" h/ h/ B, H  b+ l1 B- {$ ~1 G
12. }0 e" F3 h* \! z8 O' U1 `6 F/ R
13. }
9 `9 w* R# p3 b8 L$ Q% {当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:
3 M9 v0 F) P8 S7 w! X' [2 ]A.        两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”, ?" O8 Y* P8 `7 ]' K
B.        两个类都回出现编译时错误
& h  O7 U$ b0 ?C.        两个类都可以编译通过,但SubClass会在运行时出现运行时错误
) ]: A8 c# Q) _" A- I# N  PD.        SuperClass.java可以编译通过,但SunClass会出现一个编译错误1 B- [7 `* M7 ~. Y+ i7 C8 Z% r
11.        读程序:
* l/ t4 U5 ^, U; F0 v3 @! H: F+ f' K1. public class Test{" O5 b9 X" m* d" W' Q
2. public static void main(String args[]){2 A6 _3 b1 z" f) |1 [/ n
3. int[] i = new int[5];/ T3 X5 w8 V7 K
4. System.out.println(i[5]);" U* C% Y/ D/ f5 K: S
5. }$ ]5 Y- V$ o9 Z
6. }7 X( n' k$ @* {( C3 Y  G: O: z3 q, a
当编译和运行该类时,会:1 ]4 d  o' L9 ?* q! S3 x* b- C
A.        出现编译时错误
5 `) K1 w2 Y% C+ b5 i- i  _B.        出现运行时错误
# U, f! V) e6 w( E$ ^8 `3 g- XC.        输出0
3 T* a4 l" V4 X7 ?- r7 h, rD.        输出null
  N( Q  \3 ?  f  b8 x; f12.        以下哪个选项是正确的数组声明或初始化:2 k8 F1 t/ T4 \
A.        Array sizes = new Array(4);
0 v- M" m- w2 ?* o- lB.        double [] sizes = {1, 2, 3, 4};
# j3 l* M+ ]* K) r+ m+ `6 BC.        int [4] sizes;1 B1 y8 X! i# b( a2 j
D.        long sizes [] = new [4] long;* S& H3 ]7 {+ ?9 E* z
13.        读程序:
, _) x" f0 J8 z4 E# U1 L% B7 W, x: }0 H1. public class Initialize{
5 w& C! O# X; Q2. int x = 200;
5 k$ O* N2 v7 i* b3. static {
2 G& d' l- b, _( y! |4 I; k6 R4. int x = 100;
6 I, N3 u/ k* x3 N5. }
  s0 ?3 ~1 G7 j+ \8 j  p, r* W3 P6. public static void main(String[] args){
1 m, W; t* O" {4 I2 D7. Initialize init = new Initialize();- u8 G) Z$ T. E$ @4 E6 @1 C
8. System.out.println(init.x);9 J/ Y5 Y4 h% s1 R6 J
9. }. F9 u6 K7 Q: h4 I! F  F* H/ ?
10. }
. J' ?/ `7 V1 F+ G2 ?3 a% A3 E2 f当编译和运行该类时:
' M0 d* e9 z& Q! H, e1 `1 [! hA.        程序因为第3行产生一个编译错误6 S$ W$ s6 e) G/ G+ Y% ^  g
B.        程序因为第4行产生一个运行时错误% Y, K6 W  s; v* j# P# U
C.        程序输出100
1 H) L9 h+ g$ v+ \8 sD.        程序输出200  g* ~2 L1 Z5 ?* N& w, O
14.        读程序:% K" o% s; L7 r! m, f& ?8 o
1. public int abcd( char x) {
+ I. N1 W. k' m0 X) L" F2. if ( x <= 'M' ) {1 k2 d. z- @  [; O
3. if ( x == 'D') ) D3 y* v3 m2 P0 `
4. return 2;
! i+ P" m7 q, j4 c0 N6 X5. return 1;  S; v8 }' U8 n- f
6. }
; M1 c" T! \3 ]$ v# Z. Y7. else if( x == 'S') return 3;8 c) b7 `; o8 U0 Z4 M* z% b; G
8. else if( x == 'U') return 4;
. _+ O0 [* U' j5 M4 T, Q9. return 0;4 h8 ]. \- U8 t! e3 Z  G
10. }
' E5 @* }* F( T5 K2 G9 P在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1
0 S! j8 L. h& OA.‘X’
# e4 d6 ?) O2 d" g4 k# k  C* {B. ‘A’: _, N& [: Z$ O2 Y7 @' F% j; J! R
C.‘D’% g! Y! Y% A% w3 g
D.‘Z’9 O) e" _$ I; L: y' s5 l
15.        读程序:8 k6 e7 ~, P/ L0 ?  p$ \, C& s
1. class TryTest {
  |7 S5 y* l5 G8 D- {# @0 v2. public static void main(String[] args) {+ t2 L4 ^7 Y2 V5 L
3. try{
& M4 W/ s+ C/ V' ?& j4. System.exit(0);; V6 z) f7 x( z( N+ t9 ?
5. }
6 I$ \  I/ ]0 G8 ?6. finally {
1 o4 R8 Y; v) x3 c) K" I7. System.out.println("Finally");4 @2 t/ N0 v, ~1 W
8. }. G% h1 Z7 d' i: n( }' o
9. }
9 J  w6 J5 ]) {6 f10. }
6 v3 T& }7 i+ G$ i& c& U当编译和运行该类时,将:
8 v8 F6 ?: v1 {& a: UA.程序因为第4行出现编译错误
: G6 x$ K$ N$ S9 z4 n8 }B.程序因为第4行出现运行时错误
" z, M( g8 h6 o! W! K" HC.程序因为第6行出现编译错误
; |( n: a2 x- w  e6 P( ?1 HD.程序因为第6行出现运行时错误
0 K2 B" [7 x6 J3 m" |E.程序输出“Finally”
+ w0 B) y5 N0 d; p$ gF.程序可以运行,但什么也不打印9 [5 a  l4 r- m
7 k% Y/ j: I1 O; [
16.        Given the following variable declarations:( t" B4 m) o! ?6 @9 n8 r
Boolean b1 = new Boolean(true);
# I1 k  X7 s" x5 fBoolean b2 = new Boolean(true);
; b3 |! L/ d3 _- s6 q+ {Which expression is a legal Java expression that returns true?
$ Q: U! ~9 O+ L# AA.        b1==b2- Z3 I" n0 T( g  @* K% {
B.        b1.equals(b2)
  h3 o' }" j3 c' h/ h  nC.        b1&&b27 X- N7 e% I  X2 ?; S
D.        b1||b2+ R& p: e  C6 B; I" @  x* J' R
E.        b1&b2
* _4 Y1 C8 E2 i9 t0 IF.        b1|b2/ f) v( F# K! p, P  @: ?
17.        Given: * [* v) ?5 O' j) d
1. public class Employee {3 q" O$ C7 `! I
2.
( T; S: I+ f0 q2 A- }% M3. }# g, P" U$ {/ `2 M0 v" ]
4. 7 |: y. i4 h. w/ `$ a* b2 n/ O
5. class Manager  extends Employee{9 }& @! U! d. ?8 Z1 C, F
6. }' B8 F, \# ~6 T+ ~6 G: Y
What is the relationship between Employee and the Manager?7 l0 T3 N  @  p4 L5 i- e
A.        has a0 u0 p$ v: t& F
B.        is a- P, t- I! T+ ]: @, G! T: K
C.        both has a and is a
$ E3 L+ |9 R/ x4 K  ?" F- A) rD.        neither has a nor is a
; v! v& [3 K" D1 Q5 J8 M+ r6 x" _18.        Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?
* s9 s7 x% g, n2 VA.        BorderLayout; Z9 g* Z. M* [8 {) n
B.        FlowLayout5 p" I+ f; O  G1 o9 U2 E1 R
C.        CardLayout; N3 `# T8 c; H  Q8 G
D.        GridLayout
/ z2 h; F6 ^7 x19.        Which of the following AWT components can generate an ActionEvent?
0 \" _+ T& _2 ?; A, K, \A.        java.awt.Button9 r. c, |' a/ O" p& v
B.        java.awt.Panel
$ ]4 |5 H( Z: c$ \C.        java.awt.Canvas1 _# G- ]; w! c+ ~2 R- o
D.        java.awt.Scrollbar% e" [4 e4 P; Y3 f% A/ O4 J1 C7 I) N
20.        What kind of Stream is the System.out object?
( Z9 o7 G' l# H" `A.        java.io.PrintStream/ m9 C/ p  {8 U4 h: o7 x
B.        java.io.TerminalStream
# E5 G" V  k4 o2 NC.        java.io.FileWriter; m, M; A" R% Z- V1 o: x
D.        java.io.FileWriter0 n8 }8 C7 T: z& R4 `. p8 e- @$ _
21.        Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)
# R" b0 M8 Q2 {; C" g$ fA.        java.awt.event.FocusEvent
" i# e5 e9 i, ]B.        java.awt.event.KeyEvent/ U* r& V' R5 g& U; ^
C.        java.awt.event.ItemEvent  Y; M; G9 ~( E; Z3 \) A: h2 L7 D
D.        java.awt.event.ActionEvent$ a) n2 d  I) B( e8 j* d
22.        Which of the following methods are static methods of the Thread class?(双选): o! ^/ R# ?. x) u8 A4 `
A.        sleep(ling time)- l5 w; I# G4 c
B.        yield()
3 j8 K+ E# o% }, U9 y. G) kC.        wait()- i& }# M- F1 Q3 W  T- F. K
D.        notify()+ ]* y! |* ]; n9 L% S
23.        Which method do you have to define if you implement the Runnable interface?. J1 k  W5 |1 S/ N
A.        run()
  {0 Q3 B  L8 P" z% w# u7 SB.        runnable()6 ~5 R8 w, }! {+ x7 h
C.        start()% E& N" ^, V" D, X, e8 Y
D.        init()) X( j: v' G2 i5 i
E.        main()
5 {3 ]& v/ o  `9 z3 l  s2 L- D24.        Given:  ^; I7 j( n% ?5 `8 W3 w7 e! z( T' e( a
1. class MyThread extends Thread {
! ?( Q' x4 _+ q5 q5 J2. 5 e9 `* u7 q, g! f$ W* ]% C& H  g
3. public static void main(String [] args) {
: E+ b* k' X% h% J* A5 S! ~4. MyThread t = new MyThread();
5 J! u  H/ o$ l* |  L5. t.run();
2 S# ~2 c8 D. ?. @: v6. }
* E' F; k  d3 k8 M4 N' T7.
7 i0 Q- i" G7 s5 D5 ^8. public void run() {
2 m- I5 ~+ j& d! C9. for(int i=1 ; i<3 ; ++i) {
2 e" @4 S1 l0 f  y; B" n10. System.out.print(i + "..");9 Q, T8 l5 g, D, X
11. }4 Y& N6 k, z& Q& d; O' M$ Q
12. }
' c: e7 s: P3 ^, T$ t: r2 Z13. }
$ T  a" M& K) N( g; K" ^5 q8 IWhat is the result of this code?C; ^% n! y, V- N; w; V. E# @4 Y
A.        This code will not compile due to line 4
! P9 H6 |0 c* T7 {+ b8 p; MB.        This code will not compile due to line 5
8 }, j% }9 w  x2 n; G; CC.        1..2..
$ J8 f, [- w. |( TD.        1..2..3..- v8 H  s0 U& r5 F1 s6 ^% x% T
25.        Given a local inner class defined inside a method code block, which of the following statements is correct?
- l9 y* z/ q! R( B7 GA.        Any variables declared locally within the method can be accessed by the inner class
# ]; R1 S: @. O8 C/ F+ @- aB.        Only static variables in the enclosing class can be accessed by the inner class
6 [+ A8 N" W7 ^: T; R- u" L' M3 eC.        Only local variables declared final can be accessed by the inner class8 m) n' Q8 U# }# \4 L8 P+ H
D.        Only static variables declared locally can be accessed by the inner class.) p" g# Z0 o: W  U' D4 N2 ^4 ~

- I- E. M1 s7 _; V2 ~2 B
1 M$ \8 T0 a2 j& l
" ^  u$ e6 B  x& `6 g26.public class SychTest{ $ \& y: A9 P& G  h& i
   private int x;
2 j4 f6 |, N8 C- A% U   private int y;
8 k7 [$ C3 Y5 J, p* ?0 R   public void setX(int i){ x=i;} 5 l$ A2 y7 G9 i; c5 q/ i& V
   public void setY(int i){y=i;} # \# x$ w3 q* B3 g! ~
   public Synchronized void setXY(int i){
0 w* r/ |' L9 [     setX(i); 4 ~# Q# Q% e" D
     setY(i); 3 Q( |4 x/ ~) U+ _% {2 ^) r
   }
, D1 v4 a8 l: u; G   public Synchronized boolean check(){ % @5 m( d; G$ L  }& D+ Y
        return x!=y;   " s/ H9 W& y3 n4 @, s* x8 [0 @! A
   } 9 h) ]- K6 P' l* A5 i( V
   }
+ N! |( E, Y  `* L  C; u* `    Under which conditions will  check() return true when called from a different class?
. O$ y* z5 G9 K, L   A.check() can never return true. 5 A7 ?  z7 y, s9 `3 I
   B.check() can return true when setXY is callled by multiple threads.
( E3 }7 t5 H! I! Z5 F2 L   C.check() can return true when multiple threads call setX and setY separately.
  V( T6 A# t6 h: H3 c   D.check() can only return true if SychTest is changed allow x and y to be set separately.
3 |) W! m7 S* S4 V, [) Q
# B9 v7 l2 }' w27. 1)public class X implements Runnable{
: |# x1 G2 q3 M! [  2)private int x; , U. l" V8 S) A, h
  3)private int y;
3 p# i/ G9 f$ T' @; q  4)public static void main(String[] args){
% K" s, B0 w6 Q/ t  5)   X that =new X(); " \8 g! z+ y1 e& B
  6) (new Thread(that)).start();
# r5 P. X/ A/ Q1 q  7) (new Thread(that)).start(); 6 q' z/ f  J( C
   }
  N7 p. m" H0 ], z  9) public synchronized void run(){ ; ]( ^. B- l) U
  10)  for(;;){ 2 ^% x; c! p5 ]* {( z
  11)      x++;
. H4 f3 X2 [$ b# k# {3 C# t  12)      Y++; ( W  ]0 P2 j9 }8 u
  13) System.out.println("x="+x+",y="+y); 8 y& K2 f) [3 d, A
  14)    } ' Y  ?: V, D: l' _4 w( I& H
  15)   }
. z: k9 d" U$ A0 W  16)  }   
+ U# B6 Z7 a! S: S" e0 `* n$ k    what is the result? & l, b9 n* c2 d1 k
  A.compile error at line 6
' ?$ F4 w% r3 j  B.the program prints pairs of values for x and y that are & O$ Q- ^9 o+ y; \3 |( s" r, S
    always the same on the same time
9 v8 n5 F9 b7 G! ]* f  N) v! ~* z+ w% c% G% O6 k. \
28.class A implements Runnable{ ' y# l' [' Y) _5 P! Q  Y) r5 [/ S
  int i;
( y, U) }9 }4 u: b  p  public void run(){ 1 H6 E6 m3 y5 p' w$ l  s/ z* a/ L
   try{
% u( P! q  R2 q       Thread.sleep(5000); " V3 Z7 @9 s4 l! i6 Y1 ^
        i=10;
# Y: U" Y) H8 _       }catch(InterruptException e){}
2 n7 K' i' k7 E. \0 ~1 n0 Q: W       }
0 G1 s! r2 ~0 g       }
- [$ B& W) _. ]2 O: \; G' M   public static void main(String[] args){
# p. j' N0 Z8 J  {  r& q  a      try{ 2 }7 W& @: ~" \$ q) \
         A a=new A(); ; h/ R8 t% x. y& [4 C
         Thread t=new Thread(a); & T& T3 p% I% K
         t.start(); ) t* q+ Z3 s, [% m) e
  17) ) }4 n/ ]; ~7 `4 f" W. V
      int j=a.i; " o; W9 E* Q) Z1 n4 \9 {3 C5 ^
  19)
4 ^' S. f$ r0 K4 L) K% L8 p4 c. `      }catch(Exception e){}
7 D: \7 R9 L2 x; C      } 7 X6 D  d# s% t. g  K2 O& T4 C$ d
      }
( ^' Y. }9 }$ K  what be added at line line 17, ensure j=10 at line 19?
6 v  n8 S8 Z8 N; j  ~1 a; h( n& A. V  A. a.wait();   B.  t.wait();   C. t.join();   D.t.yield();   
9 e+ I( e6 _3 `9 k  E.t.notify();  F.  a.notify(); G.t.interrupt(); + w1 M+ t" h$ r1 u4 T, I; V
29.1)public class X{ " \7 a! T( [7 @& ?, k. o$ K2 x
  2)    public static void main(String[] args){ ; }) y. X6 M4 U6 n
  3)     String foo="ABCDE";
/ O" [2 F3 ]" v% h  m( ^  4)     foo.substring(3); # B# Q# n! Q2 ~9 b: E0 G( X; C: e9 [1 H
  5)     foo.concat("XYZ");
5 i7 A' G* o  Q- F3 Z0 @  6)    } 4 v/ F' e* I9 Z, ~( G7 k, \9 [7 x
  7)   }
; Q- l' t; j/ a, O* Z" r  what is the value of foo at line 6?
. V# K2 z# w) n! j30.  public class Test{
. Q) i0 z! v4 ?: _9 @1 M* T  W   public static void main(String[] args){ : f) Y) q! q" }! ]5 P
   StringBuffer a=new StringBuffer("A");
2 g2 N5 ]( z& {, w. B   StringBuffer b=new StringBuffer("B");
9 o3 u$ D- F5 K- s   operate(a,b);
" ?5 ?8 Y' u, b: H; n   System.out.pintln(a+","+b);
  G- }0 Z6 {5 _, V) y, t' F5 S3 T# ?    } / r6 P, A9 S! `8 K+ _4 ^7 Z; _' w
   public static void operate(StringBuffer x, StringBuffer y){
1 U2 u9 V* ]2 R) u/ a, V    x.append(y);
# t( x3 Y$ l, t    y=x; 1 ~3 P  {' {8 \
   } " w" S3 {" m+ y% B, D2 T( ]
   } ; y$ `  c$ i- X2 g% |& o7 f
   what is the output?
yangyu638 该用户已被删除
发表于 2005-3-20 12:49:05 | 显示全部楼层
感谢楼主分享3 O1 L: n/ [" V+ l6 I% U! m! u) s; _
我已下载。。。。。
  V2 D0 @, T. F4 l特感谢您的大公无私
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-11-20 05:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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