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

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

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

方联scjp权威考题

[复制链接]
javalea 该用户已被删除
发表于 2005-3-19 18:54:17 | 显示全部楼层 |阅读模式
Sample TextJava培训考题
* D' J- U0 j) c# f1.        给出以下程序:6 Q9 g8 V) E' h; J6 X0 G7 ^  {
1. public class Colors {
+ V, o$ V9 z$ y" Q( x) @1 G6 w2. public static void main(String args[]) {
( H: Z8 g& Q: Z5 K4 e3. int n = 1;* i6 y6 L) V- U% Q) C  r
4. System.out.println("The Color is " + args[n]);
- K  y9 y+ k" \' X5. }
: O# v' I; A% x0 m/ L) t: R6. }, u- k( I! z* J/ V* Z
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”
  _* X& f" g2 eA.        Colors red green blue yellow
" \; I. c9 O9 P. k; M/ zB.        java Colors blue green red yellow
. Y4 L/ k! D8 ?: i* i9 aC.        java Colors green blue red yellow( v) \" j+ n' A' k' ^% y. |5 ?# e
D.        java Colors.class blue green red yellow6 w$ i. S! |3 F) z1 _  r7 H
E.        java Colors.class green blue red yellow- ~1 u; E* Q) ]$ k
2.        读程序:
. k2 q; z3 s2 }* S" ^$ Q: Y! `' g1. public class Test{8 o3 ^6 G" C' \
2. public static void main(String args[]){
/ r$ w3 O, X+ K7 D0 d, r3. char c = '\u0009';7 `* F/ \" |4 [- g1 ^9 V
4. if( c == 0x0009L)4 D, k$ g( \' w6 I' y
5. System.out.println("Equal");5 j2 Z  u) F1 M4 t
6. else System.out.println("Not Equal");, n9 m! a: Y2 d9 ]3 O# o0 n
7. }3 {3 T7 U- K4 ]8 e9 `. `$ `4 B
8. }
5 x. \1 `4 T) ^" m/ g当编译和运行该类时,将会发生什么:5 H4 v# e# s' K$ O
A.        程序因为第4行将出现一个编译错误( O& e7 e+ H3 f& V+ v# c: a
B.        程序因为第4行将出现一个运行错误
, s) Q; w/ W5 [' \, R( |8 IC.        程序因为第3行将出现一个编译错+ |( C( B1 E  {
D.        程序输出”Equal". Y5 J5 W0 r  M' E
E.        程序输出"Not Equal”
# O; L4 `% L& L( Y2 w9 n, P/ i3.        读程序:. o. z2 Z2 f6 i! U
1. public class StrEq{- V6 F9 R" [9 |, z5 v! d
2. private StrEq(){
+ \* x. ?6 T9 V) f3. String s = "Bob";
+ g' K; f+ H9 X! ^6 a+ G3 C/ g4. String s2 = new String("Bob");
5 M9 b2 j( ~# d! s! P5. if(s == s2){
( w; `0 }- m; e- ~5 w5 }3 r6. System.out.println("Equal");& Y: j8 w- S  B! w, o
7. }4 J, P2 m1 s: j
8. else{3 \4 o* o8 M  n' Y' G% `) U7 u
9. System.out.println("Not equal");
. P( q6 s0 u" y$ l10. }
4 `$ M4 G+ E) q2 v11. }
. S% R* s" Q' @' }' i, a8 P. O# T+ F% ]12. public static void main(String args[]){
9 Z+ a9 y$ T6 P( e7 D1 s13. StrEq s = new StrEq();0 _5 a8 k" t4 L, e& T
14. }. D; U9 D% Q9 A
15. }
& x  y( x3 ?, e) T5 \/ `6 w* F" O4 L* y9 R当编译和运行该类时,将会发生什么:
. l8 R7 g4 s" N) AA.        程序能够编译,并且输出“Equal”* ?: ~8 h4 l3 W4 Q& ]
B.        程序能够编译,并且输出“Not equal”) F5 m( M# U# B& L
C.        程序因为第2行导致一个运行时错误; Q9 L; N/ H- ^6 N/ ]  u" w
D.        程序因为第2行导致一个编译错误- v. z' a# {1 j2 N* J
4.        以下那个赋值是非法的:5 |* m8 h. }- v- t
A.        long  test = 012;
! A" H; Q6 [  ^1 [% g/ NB.        float  b = -412;
6 n& e! q. z/ D/ [) WC.        int other = (int )true;- j+ m/ [' t& e* A7 G  B
D.        double d = 0x12345678;
# w% f& f+ ?) L* cE.        short   s = 10;' m+ I+ n/ k  }0 b+ C3 M
5.        读程序:
( }% s- \# c& F. m, h/ H: `1. class CompareString{/ N9 O! _$ k+ i# ^+ I% p
2. public static void main(String[] args) {
, u, {6 n$ f( x7 u  J0 t& M" N$ }0 \3. Integer x = new Integer(5);
+ z. e' i1 J* `! J  F4. Integer y = new Integer(5);% R) |8 b2 F( u6 L
5. Integer z = x;
; r' K2 s4 c& s6. if ( ______ )
3 u9 r# `+ E! r. V) @/ M2 `  V7. System.out.println("true");
2 [  L6 j' F* ?7 a# a1 Z" G* \8. }6 r5 u9 S/ ~4 m+ W7 Y! s
9. }2 _5 D, w/ t9 h" h3 X
以下哪个选项填入第6行后,不能输出true:4 B& Y' p) a7 ?4 e1 V& N5 r& O) [  n1 o
A.        x==z) r2 e/ `* a! k3 t/ n( h1 {5 i$ w' Y2 z
B.        x==y
% A6 Z+ U0 ?* G+ g* iC.        y.equals(z)
: _8 H1 f8 Y+ j- _0 i: lD.        x.equals(y)" |5 E# N5 B; U  K* M0 e* A, l% L  L
6.        读程序(双选):' m: b: ~1 `2 J+ B! o
1. public class Declare {
& {, E3 a8 ]! t; u1 z: C2.
, D9 H3 s; ^$ N' ]9 ]5 I3. public static void main(String [] args) {7 h* Q% K, f' d; E; C
4.* R+ G) y6 W7 Y
5. System.out.println("The variable is " + x);3 `# ]0 r* l. j7 M) ~6 O( g
6. }+ X. v9 R; _5 ^3 f3 F; j8 A& P& q  ~0 W
7. }
& T3 y5 t  r- X& P以下哪个选项中的代码可以使程序编译通过
# F- E% I! O- G8 P/ y" LA.        把“int x;”放在程序的第2行
2 B4 U$ L+ S6 h) P4 T% \+ i9 NB.        把“int x;”放在程序的第4行8 f1 M& Y) k: h( y, _. I% h3 J. D
C.        把“int x=5;”放在程序的第2行, ?  a  z8 }: e6 k
D.        把“int x=5;”放在程序的第4行6 U7 ^0 p1 t$ B  T% p
E.        把“static int x;”放在程序的第2行
, ^; F3 _) w8 h4 |7 t. t5 }
, G, l4 y- ]3 B8 \; a& T" H7 i- N/ W8 m* N8 S+ j) R# S
; ~6 X" G5 R; n
7.        读程序(双选):
/ U2 R" P) d: n  s; h) B1. public class Child extends Parent{
: `0 D0 u" v" T5 S. G$ i' c2 H2. int a, b, c;
, c5 z9 M% X. O7 a% K3. public Child(int x, int y) {" ]# ]0 k% q7 [  \9 J
4. a = x; b = y;
9 X5 v  c# V3 C5. }% v2 u( C- p* H' T
6. public Child(int x) {
8 P& J& g5 `4 i* k" w( o4 A7. super(x);
$ S* s. {. L; }# c7 C8. }, \" S1 x! }4 {  `
9. }/ Q3 L; c: q8 Z0 {
为了能使该类编译通过,下面那个构建器必须出现在父类中:
0 T% k3 b# x- H( b' P4 F. EA. public Parent(int m,int n)' }0 O' _3 n. L" @; a
B.public Parent(int m)
4 g( Q! C( N5 G# a' N# W6 dC.public Parent()& R+ H' L9 R0 v( y; l$ C/ ?
D. public Parent(int m,int n, int o)
3 Z: `8 Z, D# |% O. h# l% q8.        读程序:" h6 j% Y6 D1 L: ^& P; Q8 o6 a$ h
1. public class Derive extends Base{
- p5 h9 O! ]* ~2. public static void main(String argv[]){
3 _( X2 _/ T# n6 v. {! e' t3. Derive a = new Derive();
- h% T& r3 k0 L3 d) t6 K4. a.method2();) X6 P( z. G7 I% M# B: a$ P9 Y
5. }8 \8 C5 V9 q$ `+ S! ?, i
6. public void method1(){* W) e' V  |# F+ W/ q/ P. E
7. System.out.println("method 1");
! a, x* L# L# ]# g; J1 U/ c& o8. } 0 A' m0 Q9 S$ `3 J1 Z6 o
9. public void method2(){
# C5 t3 R& o- G3 s' d. ]10. method1(); 6 N. C: e! i) b3 O& M9 E( ~
11. }
) h4 l1 i) }* ~& M12. }
2 F5 d3 T$ S: H2 y6 Y* `13. abstract class Base{
( H  p, p4 q/ t$ Y1 y6 R* }% ?- F* P14. abstract public void method1();
. T; t9 F8 x7 W7 e15. public void method3(){2 F4 |" W# d9 E
16. System.out.println("method 3");
( `$ I1 x3 |) M- f7 s% M17. }
$ B' X  T$ w7 v0 w  j}
. |( w1 M/ f7 w当编译和运行该类时,将出现什么:
7 L; p1 ]+ f- B6 w4 yA.        程序输出“method 1”0 b4 h! ?; u5 z/ y4 `- c% P- |
B.        程序输出“method 3”
  X2 E8 p& A1 V: nC.        程序因为第15行而出现编译错误
) V1 d& i6 r( A# G( zD.        程序因为第15行而出现运行错误1 e# ?. p, l0 @) `3 D5 [( D- ~
9.        以下哪个接口的定义是合法的:, m0 ^3 J* \' |# Z
A.        public interface A {int a();}( o! ?' H- X' Q0 T+ m; B
B.        public interface B implements A {}
' l" H4 E/ X6 pC.        interface C {int a;}1 w; c6 Y, J9 }1 @2 Q. U1 w! U2 Y& P
D.        private interface D {}4 J" r. ]! W% K- j: y+ G7 }
10.        读程序:* ?2 i; [3 D3 z. `8 b5 F
1. //File SuperClass.java2 F3 S' m( V( |. Y/ H
2. package MyPackage;) q: w0 X4 j) p
3. class SuperClass{
: S4 y  a, b+ C  ]8 Z/ ^4. void myMethod(){
9 J- ^- G3 H: P1 a" ^5 a: f5. System.out.println("SuperClass");
' u8 W' E$ n6 w6. }' y) ^3 e2 a* ]; c2 z9 S
7. }
; `# o/ Z- p* C4 P$ \% o; F# h8. //File SubClass.java
5 T. }% |. I# ^2 v2 J  u& N5 t9. public class SubClass extends SuperClass{
/ d' Q& n7 I3 B) l. R- {10. public static void main(String[] args) {
2 o& A2 w" |0 ]# w11. myMethod();
1 h# \* |/ f1 v" _2 @. w  ]12. }
3 d2 Y$ a* J) w- _2 [13. }
: C& v* H6 G, K4 ^5 o$ W当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:6 ]9 S" i; m0 y. g8 N/ u
A.        两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”
" o* L& H+ E+ o7 Y* H- f' uB.        两个类都回出现编译时错误
1 @* L0 u  b. K# m% w; ^4 X" bC.        两个类都可以编译通过,但SubClass会在运行时出现运行时错误  _8 V5 R- r$ ~1 o: R6 n, r( n% P/ N
D.        SuperClass.java可以编译通过,但SunClass会出现一个编译错误7 ?' Q. r" `5 |8 S$ |
11.        读程序:
( l3 ?) D; N  N5 T/ V# F1. public class Test{+ N* `5 ]% l2 p5 v
2. public static void main(String args[]){
7 t, @3 R3 f' l3 \" X5 f3. int[] i = new int[5];
# z. J4 b: P2 z+ m9 n5 T4. System.out.println(i[5]);
8 L5 H9 w* t' T, W+ D. ]5. }5 u+ {5 ~0 O4 L% m! `& q
6. }
3 \! a: b" G& S1 S5 m# L0 b当编译和运行该类时,会:
' N) L5 k7 e& h! JA.        出现编译时错误
3 r4 V* i/ [" v6 M  H+ }  c3 aB.        出现运行时错误
2 ~$ K6 y1 `( C- ]/ HC.        输出0' T6 |; K& G5 l1 N2 A0 P8 R
D.        输出null# F& U- n1 V" }0 m
12.        以下哪个选项是正确的数组声明或初始化:
- L3 N! n  H3 l# z+ q7 q' R& ]) ?A.        Array sizes = new Array(4);
  q4 ~& s" J: s( I/ d: d) k& Y) o6 CB.        double [] sizes = {1, 2, 3, 4};
# l0 C: U, \" B7 Z3 WC.        int [4] sizes;
& S0 _( q- C: x$ M$ T) ?* `D.        long sizes [] = new [4] long;% w* L' \1 ^5 H' a9 O
13.        读程序:$ i7 T. ?+ W: N3 c
1. public class Initialize{
  m9 p8 s5 ^) {0 z/ }) J2 ?7 Z, M2. int x = 200;
# k$ O) l( h8 i5 d3. static {
1 e- l# q( D: W5 C8 @& h- T* ~4. int x = 100;) P& {" K3 C; z  j; R( S" K/ r
5. }3 C* C0 M, o" _
6. public static void main(String[] args){
  v( M" F+ T6 J8 n/ Y8 y7. Initialize init = new Initialize();
9 b: s4 }. y5 I  r8. System.out.println(init.x);
% p5 Y/ S5 ?0 U& K0 {7 k9. }5 b: W  L. m+ ?2 `& m- [
10. }
1 J* A/ a/ {6 s当编译和运行该类时:. t; p3 p' D- ^/ k2 [
A.        程序因为第3行产生一个编译错误
8 P4 e3 x3 q! c" i. ^; rB.        程序因为第4行产生一个运行时错误% ~' U" K; Z- U! W/ B/ ?& A: W
C.        程序输出100
4 |2 m2 k; `7 M  OD.        程序输出200
7 r2 m, P6 c) [/ N& Q3 i14.        读程序:
- E! c& p% X& j9 O2 Y5 J: C6 }1. public int abcd( char x) {# [; a* ]' c, U
2. if ( x <= 'M' ) {
% }/ h$ b5 j+ l9 n# n2 }7 h3. if ( x == 'D') 6 L# v; q  _4 X" O
4. return 2;$ U, E2 ~/ c$ n) r: m, t
5. return 1;
6 P1 D  `# F" L2 J8 @6 [6. }
) m- s6 S6 d0 `3 }4 M7. else if( x == 'S') return 3;
$ N9 \: c* n: K, f0 T. W. e# \8. else if( x == 'U') return 4;, h( T6 d/ l7 L# p
9. return 0;+ [( a# T  h" z9 U8 d# w
10. }5 X5 N2 R0 \: S( J# w% W
在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1, }3 N5 A, F8 z6 t+ y1 C$ r
A.‘X’( ?; y4 H7 T2 N( X
B. ‘A’7 w" X$ T$ a& M; N! Z
C.‘D’
4 A1 \: R& o' {# {D.‘Z’
4 h* f! o) `% U' J( v15.        读程序:: `2 r0 U/ e" b/ ?' w
1. class TryTest {; X) c+ Z- p# s3 K: f9 M
2. public static void main(String[] args) {
' V( F' ^6 c% t  M9 T# g% ~" h3. try{
' O5 ~, [# U9 W. Q4. System.exit(0);
1 w, H7 F5 l  R5. }& O& l, J. C* A# x, l
6. finally {
2 X% E7 r3 h% u/ C7. System.out.println("Finally");
# f6 U5 W2 ~8 X8. }
  V$ A# C' y: y% N  H! c- z  g9. }
3 \3 L4 i3 R: l" B* F6 F, r10. }
% b" S' e$ A$ u  w# C3 b4 c当编译和运行该类时,将:
1 Q- j0 c2 \9 ?6 _& TA.程序因为第4行出现编译错误2 Q% Z. y: k9 M7 N8 D2 d1 r5 ]; E$ O
B.程序因为第4行出现运行时错误  P% P* s! A8 h- T. {
C.程序因为第6行出现编译错误0 Q, \9 g7 d& T3 y) ]
D.程序因为第6行出现运行时错误
6 G# a6 w& K" Z0 A- P% N2 LE.程序输出“Finally”/ r* V+ q0 {" G
F.程序可以运行,但什么也不打印
: s5 s  o: B9 j; f  z* K, x2 K  o3 j
7 J1 F% `' I) [: e( u16.        Given the following variable declarations:: v0 F/ P! y0 U; [8 c" k- q
Boolean b1 = new Boolean(true);
3 _; W0 _+ p, {! W+ q; C1 F# l8 _Boolean b2 = new Boolean(true);
, p5 A4 B' ^+ L( aWhich expression is a legal Java expression that returns true?
9 F0 v, L$ c% p2 e( B8 S7 RA.        b1==b2
0 x, f( n$ T: z- T( w: [, h# @! tB.        b1.equals(b2)/ D# f, O* x! Z0 O5 T
C.        b1&&b2+ k+ ]3 S! g/ q& D2 r( P( g- j
D.        b1||b2  b* r% }0 }7 X) a
E.        b1&b2
, ?6 T7 i; e$ u& c9 JF.        b1|b2
: o* [% q2 V7 T8 l0 W6 W17.        Given:
" u( ]" D# ^! j0 [1. public class Employee {1 v/ f$ j8 o, \; G! j! A* t8 C, L( c
2. ; U9 N) w4 O  d' @
3. }
( n2 R  }; Q: W7 A8 e) Q! [4. ) c. L, K# S+ O- o6 @$ E
5. class Manager  extends Employee{' \7 [( S& A# f  l, C* Z
6. }
9 A5 W/ d, e' D1 |7 D: ~* q$ v: XWhat is the relationship between Employee and the Manager?
: a8 ~. c% c5 Y% d) d- `A.        has a
; W: P9 z2 y* P% g1 TB.        is a
* x( f* k+ J# f& ]4 ~. B* RC.        both has a and is a
4 `( t: P' G1 \3 @D.        neither has a nor is a
& B' {& A2 L& n4 T$ i+ A18.        Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?; X- J! i" r! G. J$ K7 Z; O
A.        BorderLayout
3 z: O9 S# f" QB.        FlowLayout
% D4 K0 v1 O! N  s! ?C.        CardLayout" s( s- `# t: G8 b
D.        GridLayout* ?4 D7 v7 f1 n  V" g1 X! j
19.        Which of the following AWT components can generate an ActionEvent?' G' K3 H( a4 W9 a! K
A.        java.awt.Button4 Y0 h) [* g0 P- E5 h9 G$ J
B.        java.awt.Panel
4 K! l* h- l- j0 g( dC.        java.awt.Canvas
* @# ~, {" }6 eD.        java.awt.Scrollbar
% A' }0 n: b, Z( \) B1 y5 A20.        What kind of Stream is the System.out object?
" ^  l" [% ]" ZA.        java.io.PrintStream
- z. e7 b6 m; dB.        java.io.TerminalStream: m! {! q, W: C6 t2 W6 {: V" h
C.        java.io.FileWriter. ?4 D3 ?7 {5 Q8 x
D.        java.io.FileWriter
: i1 C/ [8 [4 C& S21.        Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)* N1 W1 i2 J+ C5 l- z% u8 b, e
A.        java.awt.event.FocusEvent. b' d, v: U5 W7 H# t8 i  D
B.        java.awt.event.KeyEvent+ ]0 ~. c/ k6 ^
C.        java.awt.event.ItemEvent
: p- z# B; m* L4 s" U0 e- ND.        java.awt.event.ActionEvent2 n) J' \/ v. C8 F
22.        Which of the following methods are static methods of the Thread class?(双选)
% Z# g* p, Z6 R* o% z  D- {A.        sleep(ling time)
9 K$ ^4 u$ \, s/ |& I1 eB.        yield()
& W/ f% w0 ~# x7 }; ~. _7 vC.        wait()' r* c8 Q( V/ V
D.        notify()' X' d. ~8 ^4 c2 J" X
23.        Which method do you have to define if you implement the Runnable interface?3 t- O* j, w% [
A.        run()
" v( E1 l/ k. S2 j  I" e' z, }B.        runnable()
; c" s+ _9 f0 ~2 Z2 f* A, S( w) @C.        start()7 H0 _  y1 M# b3 U- m
D.        init()  o8 r# {9 ?9 `- T. _: i
E.        main()2 ~. I# D9 Z& X4 n3 \4 [  a8 c
24.        Given:
, p; Q0 F7 d1 t6 O( ~1. class MyThread extends Thread {
- C7 w6 T  F7 |+ q4 O5 ~2.
% I0 ~/ d% v) f4 v9 }3. public static void main(String [] args) {. }" S, {( K6 M$ f
4. MyThread t = new MyThread();  S5 J5 a7 [2 e. t4 E' t
5. t.run();
9 E0 j% A& |5 B1 _) z6. }
" q$ l! U6 [9 _$ y2 q3 a9 s0 t7. 8 h' p6 g& `8 L& p$ }4 n+ L+ Z
8. public void run() {
+ r# O9 Y: Y* y- Z) W" n9. for(int i=1 ; i<3 ; ++i) {. j" |, |- ^; d. c# |9 U. d8 z0 e
10. System.out.print(i + "..");
4 m5 R/ h7 f) `2 {  o* ~% L11. }
- o2 C: R& X# |# E- ?12. }$ G8 C( ^6 l. f
13. }( b$ Q% [3 ]1 e# F; q% D  f) _
What is the result of this code?C. B3 J( S4 e6 w, I$ O: v  ], N
A.        This code will not compile due to line 4
8 ~  a9 n; b( ~) f! ]7 Q" |B.        This code will not compile due to line 56 D, W1 [. u) c7 ]
C.        1..2..
, h& D! L% d7 SD.        1..2..3..
0 X7 W( J6 I  a8 y! N25.        Given a local inner class defined inside a method code block, which of the following statements is correct?
0 ?# }* _. H" C3 f$ D$ CA.        Any variables declared locally within the method can be accessed by the inner class: s9 S1 d' \3 `0 V) K
B.        Only static variables in the enclosing class can be accessed by the inner class7 u9 [5 \8 ?; f; Z+ p* m
C.        Only local variables declared final can be accessed by the inner class) a9 y% v1 j- v4 W; d1 p$ ]
D.        Only static variables declared locally can be accessed by the inner class.4 C; q3 Q2 a. [( D9 ~0 t
" t) i7 ]5 c$ V

0 V: N" W+ g% v! @9 O: e' O, f; L2 M$ x  s: K' s/ |) O* q
26.public class SychTest{
7 C# U- c; Z4 b0 ?   private int x; + M! v( O& M8 _" d# h0 j: S8 @
   private int y; % f: p% D# h& L- \; T* W: G6 O1 _
   public void setX(int i){ x=i;}
5 E+ o9 }! d3 D+ t# A% Q& _2 r   public void setY(int i){y=i;}
7 c: [8 t' ~$ X  O4 t6 c* h6 ]   public Synchronized void setXY(int i){
+ j( y% P9 M" n; k4 C/ {0 ?9 q: t     setX(i);
1 z/ x, e+ A7 k6 h; f. ?     setY(i); - y  x/ ]+ I; |6 _" F, k# O
   } : c# K# `3 a$ m7 z1 i% F; Q3 p# m
   public Synchronized boolean check(){
8 B  s2 o/ w) _# F3 C        return x!=y;   ) y9 ], b0 V- V# z) d# W
   } 3 o: V" m% }5 g. G2 Y, ]
   }
) Q. w+ u4 |0 N    Under which conditions will  check() return true when called from a different class?   o, t0 G" L% L3 E( T
   A.check() can never return true.
* [, X, t$ d/ `4 R5 l   B.check() can return true when setXY is callled by multiple threads. ) z. g9 O4 W. N: J* i
   C.check() can return true when multiple threads call setX and setY separately. 5 }& }7 W4 s, x6 m& V
   D.check() can only return true if SychTest is changed allow x and y to be set separately. ! p" s4 f! ]. ?8 M  `9 b; R& b

. c; r1 Q0 n, Q/ f) j27. 1)public class X implements Runnable{ 7 ^" h* ?) d! b/ }( M8 D. x
  2)private int x;
! H/ Q. J0 s+ B3 @, v, j- ~  3)private int y; * f, @- c) O# C3 g& A; }
  4)public static void main(String[] args){
# @; K  T( d+ B: \( \. G4 l  5)   X that =new X();
) D" o: Y: D- O! H- W  6) (new Thread(that)).start();
, u+ V5 p( H9 c! P" L% v  7) (new Thread(that)).start();
  M* o1 c8 J# u3 P   }
) a& N5 i; d+ e  9) public synchronized void run(){ & o& u  [$ ^, X2 f8 S
  10)  for(;;){
0 S7 v. ]. w2 g2 J  s! X  11)      x++; 3 n6 ?: K$ X) x1 H- z' {/ l
  12)      Y++; 9 u& B7 n; @. K0 x2 o# M
  13) System.out.println("x="+x+",y="+y);
/ k) }' Z9 y1 ]( Y& V8 N4 o$ F( s  14)    }
) o% c% V& r8 M$ d4 q' I8 X  15)   } 2 D5 ~' q4 }& \* E9 e9 c
  16)  }   
5 W: v) f: |4 u& Z* x3 n7 q+ K% g8 Z    what is the result?
: A. _( n, G+ g7 S  A.compile error at line 6
0 m2 |, Q: ?0 p; ?" }  B.the program prints pairs of values for x and y that are 7 L7 O& T0 @# ?( A( s
    always the same on the same time
1 `& L( A2 {2 T
3 A6 E9 S8 z$ ?) l8 d, S4 a3 Q8 t28.class A implements Runnable{
* c0 K+ L5 w4 a7 }; Q% _  int i;
! F: H* g$ k0 U" L. U, C& |  public void run(){
# g/ Q2 T7 b* ]   try{
4 L/ ^! V9 r" W  m       Thread.sleep(5000);
% [; N" \" D5 J$ A8 N8 p7 g' R- _        i=10; 2 c) E* V* [; J* p9 x
       }catch(InterruptException e){} : P4 H; |3 s: x
       }
* f0 n# I9 H5 g$ o% J, M7 }! j       } ' Z; l. |0 _- C, E) \- Q
   public static void main(String[] args){ * x/ z. q, s8 p. G: |8 Z* X2 N
      try{
/ z8 G# r9 C+ [" m& W7 R1 S         A a=new A();
5 m3 W& Z7 A; |         Thread t=new Thread(a);
: z# m. X; M$ \         t.start();
+ |4 [( M- v$ p, U8 c6 G  17) : N2 e+ j, z# W0 D8 S
      int j=a.i;
2 a, X1 E9 Q1 F; u& Z  19) " G; X* a/ P0 \$ e
      }catch(Exception e){}
- e5 d$ A1 K* U7 B( ~, U      }   ]% }' j/ J" H, Z0 Y: N3 y
      }
3 _4 n' d3 m& u8 V, a1 V$ g( A  what be added at line line 17, ensure j=10 at line 19? / s- ^) f4 j7 Z" z6 m. W0 \( b
  A. a.wait();   B.  t.wait();   C. t.join();   D.t.yield();     b, ]8 `) @: u- Y5 x4 w0 i
  E.t.notify();  F.  a.notify(); G.t.interrupt(); - B+ O: w0 p7 {) q5 `8 z
29.1)public class X{
: t. K( z4 [4 T/ w+ v: y4 d  2)    public static void main(String[] args){ 7 \+ \- q' ^" m
  3)     String foo="ABCDE"; " Z5 r+ [$ s5 ~" |
  4)     foo.substring(3);
" T( G5 W2 U- w( B) e# V3 N  5)     foo.concat("XYZ");
% \# y. C9 o" p9 ~& Z2 ?. j  6)    }
% {- N* p, B6 l) y8 a  7)   }
# ~% W: C4 o; Y' D0 {- e$ Y  what is the value of foo at line 6? % U) P/ `9 S! T. k
30.  public class Test{ , i1 Z& V% Z- k' n) r, J
   public static void main(String[] args){
( {; Q2 `" o; W! N0 }   StringBuffer a=new StringBuffer("A");
! W0 g2 Q6 c0 N! v0 Y+ z/ n   StringBuffer b=new StringBuffer("B");
: m- H5 ~' I2 k   operate(a,b);
2 }) f$ B6 B7 E8 c   System.out.pintln(a+","+b); 0 E& s3 @$ _! D$ k% {& x
    } - M2 ]% ?8 M; R
   public static void operate(StringBuffer x, StringBuffer y){
7 c4 c- {; [+ M6 G* e/ ?; V    x.append(y);
0 ]* v8 {5 C6 q, n    y=x;
: q9 E. r7 S8 _. f* W   }
1 o; `+ B# v* D4 ^   }
: l4 `. C7 l7 c$ A% m   what is the output?
yangyu638 该用户已被删除
发表于 2005-3-20 12:49:05 | 显示全部楼层
感谢楼主分享
0 l, D$ g8 V7 b+ e我已下载。。。。。
; X: L& h" A6 ?- A特感谢您的大公无私
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-1-3 11:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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