Sample TextJava培训考题
& y6 [, M9 ?) U+ a1. 给出以下程序:4 A" i& w! i3 P/ v" E; ~
1. public class Colors {
( w" W& F$ F5 m' L7 p7 T2. public static void main(String args[]) {0 P' e% M$ L- B$ p+ }6 V! @
3. int n = 1;
: ]. ?! r1 G; M( f5 z, k4. System.out.println("The Color is " + args[n]);( \4 i' g L1 K
5. }8 W9 Q% B8 M* {+ m0 H! H1 I" T* e5 {
6. }7 T: C2 V5 Z* q7 m0 F3 A
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”
, K/ @ a( F- @8 _. gA. Colors red green blue yellow: s( j: H2 k- O
B. java Colors blue green red yellow# O& E) E# U$ s6 Z& |
C. java Colors green blue red yellow) j/ ?+ W7 J' g
D. java Colors.class blue green red yellow
) q4 _, U( n1 G/ `1 e" A6 L5 pE. java Colors.class green blue red yellow
! u4 Y0 K3 z Z+ v0 F y. v! K2 n' Y2. 读程序:7 C0 k) u: _/ [2 t: j5 b8 h
1. public class Test{
# ^$ r; ?5 n' B# G5 A! w4 s% F _2. public static void main(String args[]){8 _/ T1 y- ~: J- R0 M
3. char c = '\u0009';
- g }% ], [, `* k, M3 p4. if( c == 0x0009L)
4 ]* S, v2 J% E0 e5. System.out.println("Equal");* _% `1 B, k; \. J' Y# I
6. else System.out.println("Not Equal");/ M2 U/ o2 a' N2 ]) ]2 h1 O
7. }
% \5 |, n% K& [) u0 T8. }
& W z& ^$ |1 a. R5 O5 ~5 u0 v当编译和运行该类时,将会发生什么:: a# Q* x8 x4 g9 p$ H, O
A. 程序因为第4行将出现一个编译错误
, I, s/ v5 E; K2 K Q6 s" }' tB. 程序因为第4行将出现一个运行错误) w. f. t" ?$ x, b! y4 `% Y: ^
C. 程序因为第3行将出现一个编译错
# R9 Y7 x/ ^% M9 b cD. 程序输出”Equal"' o2 A0 r% V5 [; n: c1 ~
E. 程序输出"Not Equal”
0 L) k: D; _% z9 m( i3. 读程序:; y+ \' [7 {% w G4 b" |
1. public class StrEq{
$ a! C5 h2 }- @2. private StrEq(){
1 H) g, U9 \# {& C: W; c3. String s = "Bob";
, T8 i7 V6 H9 J9 h4. String s2 = new String("Bob");
& W1 k. E; R6 \' R$ O9 x. Z/ B5. if(s == s2){
2 S @" g$ ?3 r6. System.out.println("Equal");1 ~$ { @% m! x+ p7 N! z
7. }
8 ] T" G( d3 p2 a: x# j8. else{
" j- y" f. c r: O7 [( ^- g% E5 h. N9. System.out.println("Not equal"); v7 C- t# k5 [( M) o
10. }9 ~* z4 M5 Q6 u
11. }
$ R7 e( u k' O, s/ G8 n0 J12. public static void main(String args[]){8 l u1 b' n- {7 p) r
13. StrEq s = new StrEq();1 @2 \* {8 g* a+ \2 @' {
14. }
! B s( U1 V7 V2 i% ~15. }5 g! J D& g% R. {, \
当编译和运行该类时,将会发生什么:
1 z( l4 X3 b$ J. qA. 程序能够编译,并且输出“Equal”5 Y3 L! J! h3 l. y; n
B. 程序能够编译,并且输出“Not equal”5 z' W/ }; u* Y1 V
C. 程序因为第2行导致一个运行时错误
$ b% z6 P6 U" ?0 eD. 程序因为第2行导致一个编译错误
. n5 h$ G: f z4. 以下那个赋值是非法的:2 a3 H. B! C1 P; Y J4 O; G
A. long test = 012;4 B5 |5 m5 b9 x6 u
B. float b = -412;7 r; X, g8 Q! r$ j, a
C. int other = (int )true;
! w! C/ r) i1 b1 | YD. double d = 0x12345678;/ R# |) y8 Q" {2 b
E. short s = 10;- V5 S( R( ?7 |: v" T9 c
5. 读程序:& n. x' K' w& @1 C! b
1. class CompareString{
# Q, Y' g3 t! k$ c! B: I/ N7 ^2. public static void main(String[] args) {9 J1 t( h9 j. G8 T# v0 r! J. C
3. Integer x = new Integer(5);( S5 z' P3 V5 A; X- H4 v8 C
4. Integer y = new Integer(5);
9 b' P$ W: G9 o$ i9 ~7 c5. Integer z = x;+ r4 U9 s% t% O# }& I. s$ a
6. if ( ______ )
( Z/ ]# V" E0 H8 q7. System.out.println("true");
n+ G: l2 B# H7 T8. }
9 _1 k% R, O' K9. }
8 i2 p1 b+ g( u$ ]以下哪个选项填入第6行后,不能输出true:
" A& x; P) ?$ Y4 }; vA. x==z
4 Q, ?. c/ a' r/ k' }B. x==y$ V- A, m$ k& a) x! Y
C. y.equals(z)
- u3 w; n, G. w: r) _5 [- OD. x.equals(y)8 F$ C" r. t4 _/ r/ T8 f
6. 读程序(双选):
% i! B" z( N& C& ^. W& a& @1 s+ l, B1. public class Declare {
" J# k) h8 D+ P) F4 s2.( F* |$ ?* M% M! z% x. z; |7 h! n
3. public static void main(String [] args) {
( P5 X+ J- t: I# D4.' v, A( S$ m- W! Y5 K
5. System.out.println("The variable is " + x);
2 }5 a% v& b( K0 v6. }4 l$ O. J8 O4 a$ R
7. }" I; e6 o$ L2 X
以下哪个选项中的代码可以使程序编译通过
9 o, f9 z) E o0 V- W5 i& F2 QA. 把“int x;”放在程序的第2行6 ?- W7 g& c% q& I5 I, I4 k- R
B. 把“int x;”放在程序的第4行
j6 n* ^. o2 Z4 YC. 把“int x=5;”放在程序的第2行
5 \. ?: L, c% |4 DD. 把“int x=5;”放在程序的第4行
! C0 l* |' r0 G" T% P0 q. ?" VE. 把“static int x;”放在程序的第2行
8 B1 ^& x1 i# F) k# q
0 `8 g+ q, n0 U& |2 b4 K
2 n1 k: T1 p7 D1 X( [
2 Z1 r* C( ~0 C/ q1 X& E5 O" m, i& C7. 读程序(双选):
; ?0 A Z/ _! I8 }: R1. public class Child extends Parent{1 Y; H* N8 m9 H0 q$ n8 W+ `1 n
2. int a, b, c;3 x$ a+ ~7 D7 L5 V" ]2 e
3. public Child(int x, int y) { p w2 F' A3 I
4. a = x; b = y;5 S& O# e* u& Z* A, C
5. }- \9 f/ X5 K: M- F5 Q0 [6 t, `3 _$ i* H7 t
6. public Child(int x) {
0 ~7 d4 ^& O, q# N; I7. super(x);4 c% q4 f0 e0 J8 Q
8. } U$ I" w1 }/ S r0 O
9. }/ x, L+ b' `0 m8 y, \ i: A
为了能使该类编译通过,下面那个构建器必须出现在父类中:
$ I) n/ W9 a5 ?" K6 p, Y1 _5 [ xA. public Parent(int m,int n), `* Y) x/ ?0 R
B.public Parent(int m)
: g( y/ s) Y6 I6 B- NC.public Parent()3 ~9 y( L9 C4 V/ ^
D. public Parent(int m,int n, int o)% ?" B# }& F" r
8. 读程序:
. p: s+ {/ M1 g1 h, ?8 L( \9 w1 z; N1. public class Derive extends Base{! m( X! x" I1 L
2. public static void main(String argv[]){
: C4 K4 w! u! _. J2 \! r3. Derive a = new Derive();
* C. ?/ Z4 U& W7 U' }& ^. d4. a.method2(); u) j% a- | `
5. }
0 I4 B u9 i9 i9 y5 q# B3 i' ?# o+ _ K6. public void method1(){4 G# L4 @0 M- ]) g
7. System.out.println("method 1");3 }. J: ~/ L5 Q: F$ I9 X$ `( T
8. } - @6 B \) x7 T
9. public void method2(){
1 p: Y! t" P# q2 V2 `+ c# E10. method1();
' Z( c; M1 G/ r, W+ K1 H11. }; D/ o# R. o9 `3 U* B* n5 R
12. }
; w$ U0 E" [6 v- o) H+ a% U13. abstract class Base{7 H% z4 I' b& P
14. abstract public void method1();
) _4 _8 c2 Y! ~& B2 ]& S8 Z7 i15. public void method3(){
% t! f9 r6 |; e# W- \5 [# \/ n16. System.out.println("method 3");( @! W; t2 L- ]0 `" C
17. }. M+ V( c# X; l9 U2 @
}0 R+ X# O1 r7 X4 t0 n( ]2 W
当编译和运行该类时,将出现什么:* z% ?8 Y1 j4 [1 }( V: B
A. 程序输出“method 1”
8 g4 e* h$ ~% m( L4 N. Z& AB. 程序输出“method 3”% O* U B8 z0 O+ F3 w
C. 程序因为第15行而出现编译错误! T' M j& |) U8 R
D. 程序因为第15行而出现运行错误 O2 b3 R% x6 x# K
9. 以下哪个接口的定义是合法的:
[6 m6 Y5 e2 \( _, zA. public interface A {int a();}
" b9 B& _3 a( R( y( G. `9 i! JB. public interface B implements A {}
. b+ q8 |1 M% o% _1 Q5 m$ eC. interface C {int a;}2 m, ^8 G4 Y6 u. ^; q4 B5 d
D. private interface D {}
5 Y/ b% V% ]8 h$ r9 z% p: m& V10. 读程序: H4 V0 X7 n$ c
1. //File SuperClass.java
' m/ u. z. }# [' s! _2. package MyPackage;) V6 d, R& C. E+ {+ N4 O2 V
3. class SuperClass{
3 n3 u# w5 i/ s2 O2 I4. void myMethod(){4 E v. F p: J7 e! W' f/ A
5. System.out.println("SuperClass");3 f% ]) S. n8 G8 B; V4 y' C
6. }2 D# _# o$ F: @/ q( l, S( ~: H$ w
7. }
7 a' ^% [/ T! M- t7 C* s- e8. //File SubClass.java
) C! I! e: A6 N5 @) W9. public class SubClass extends SuperClass{ Z# R- L$ b+ F" `8 i
10. public static void main(String[] args) {
! w! J4 e# ?# J$ n8 `# t0 d11. myMethod();
' Z9 |& U7 y& Q12. }
}. ~$ f4 S. Y. U3 J! I9 l13. }9 Y6 }! \( x7 E) g: ~9 F% ^
当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:/ j( Q/ x( ?' h: r% h9 ]: L; x K1 Q
A. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”
. B, l* v% G; D/ A- o8 W f. D; [# \B. 两个类都回出现编译时错误( N5 K' K" o2 u: U$ w
C. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误
+ t# j5 |/ C" S4 o: rD. SuperClass.java可以编译通过,但SunClass会出现一个编译错误' A" ^/ k, O& I! L) X& X2 m
11. 读程序:" X+ i( }# e: C
1. public class Test{1 a; G% J; z. I g' c
2. public static void main(String args[]){
T) \/ i9 e! V) y* r. D3. int[] i = new int[5];2 y1 B( }! }! i) [) l+ o6 M) S
4. System.out.println(i[5]);
- _$ Z$ v; _8 J! J: s. W; M5. }
- z- K9 {) o1 S7 s& o. A1 Z) k; ^6. }# Q* m X% O: l. j# v$ l4 j* r, w5 L
当编译和运行该类时,会:
- U9 H/ K5 u8 {* z; G* C$ i& u5 CA. 出现编译时错误' O4 W/ l" b7 P0 K
B. 出现运行时错误2 @8 C; B r Z( z5 H1 F
C. 输出08 o# [ O$ N( b8 w
D. 输出null4 \- I. C1 y. V9 N( I
12. 以下哪个选项是正确的数组声明或初始化:
# H. c1 M4 ^& K$ FA. Array sizes = new Array(4);
9 {/ u7 h2 z+ H$ ~# Y+ a4 QB. double [] sizes = {1, 2, 3, 4};: A$ h; S* \2 U2 G A) N: ?. P
C. int [4] sizes;
' c4 ?0 c% Y; Z# i4 ^D. long sizes [] = new [4] long;
6 q1 I9 k7 i7 t2 O& F2 F9 z13. 读程序:" a' R) d1 Z: U. e1 p3 T8 w
1. public class Initialize{
* ~5 Y6 q/ T' ^" B2. int x = 200;" _1 `1 E/ M$ }4 z' P V- f
3. static {
" w+ h: B7 L1 C* y9 x4. int x = 100;- m: R+ X# Y/ Z* S
5. }
# G) w2 T: ~9 M5 _6. public static void main(String[] args){) o# p* X" h% }0 ^ x
7. Initialize init = new Initialize();
5 z( X4 @5 u6 P c( `: m8. System.out.println(init.x);# ], N: U1 l( \
9. }( y* n, _! G! _0 R6 T
10. }
0 B# i1 [- l# Y- q当编译和运行该类时:
0 W6 ?0 J4 l+ W$ TA. 程序因为第3行产生一个编译错误1 G+ n& ^5 j% c7 P0 o" ]
B. 程序因为第4行产生一个运行时错误" W- f& ?( m- K9 n, c! m# I$ M" R
C. 程序输出100
/ h% x2 n. I9 a' d! ]! aD. 程序输出200
: Q" r) Z6 T* |14. 读程序:
. I! D% c+ d" G( P( Z5 ^1. public int abcd( char x) {
% x4 A" o0 z. d% g7 ~/ \2. if ( x <= 'M' ) {
. g# u; C) ^/ T8 K( L: C8 E8 T3. if ( x == 'D') ' B. ?* y* u' [, H6 L5 s- m) {$ r, L
4. return 2;( S0 M$ g b; S. t$ C p
5. return 1;
' K% f! _' M% b f6. }
9 I1 S0 B- D9 u* C3 T# ]9 l7. else if( x == 'S') return 3;! f9 I o& B, v; J
8. else if( x == 'U') return 4;
) ^7 @; U' b6 K6 v% ^9. return 0;
# w3 d- P3 Q- [/ P( `10. }4 ] ~! \; C0 n+ l
在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1
& d! |3 k( C( a3 t8 s2 q& I0 {8 OA.‘X’$ Q6 }1 X' ^# U5 ]: M
B. ‘A’
" B' \3 j/ Y6 D4 i3 Z9 i- uC.‘D’
! @! ^9 Q0 M: q4 g6 L" DD.‘Z’8 y! c$ ?. K) f9 q* g
15. 读程序:' }& J: l l# O, P5 V' B o h' c$ Y
1. class TryTest {
5 O6 J; S9 O j4 S' j2 f2. public static void main(String[] args) {; g+ @/ z: S! R% ?% @$ _4 Z. N
3. try{ W9 |/ n7 g6 i0 \ q5 p
4. System.exit(0);% v1 m" b! e9 [2 w$ i
5. }" b9 c6 g' Q8 A# F5 |4 `
6. finally {! s* c$ [3 j% V1 c, K( [( m, ~
7. System.out.println("Finally");) Q3 k0 j. o/ K% X- {6 N
8. }7 E i% o4 p- w, s! Y+ J! Y0 A8 f' B
9. }: u: Y) j' B) n" u
10. }( ^: G ^+ M6 I, M5 i8 }' F) e
当编译和运行该类时,将:0 [, y0 k: l0 ~5 E8 r) _
A.程序因为第4行出现编译错误8 L3 L2 e7 t) R
B.程序因为第4行出现运行时错误
; U6 B1 e) |) G8 ]2 e6 P9 _C.程序因为第6行出现编译错误& y# \8 g( Z. W8 A) ?4 m
D.程序因为第6行出现运行时错误
" X/ q1 g' t8 l% a, q+ nE.程序输出“Finally”
" } x% w3 j8 d0 v Z) Y, [F.程序可以运行,但什么也不打印2 O, `4 R; e& Y: J" f. A2 `: j; f% i, d
8 S, k' f0 o0 }% O' n2 I
16. Given the following variable declarations:0 s: |8 \0 q. O5 z0 S% y# o1 z8 ?8 [
Boolean b1 = new Boolean(true);
" Q- b, K' i) PBoolean b2 = new Boolean(true);/ d z' @/ w2 o0 u* U7 G
Which expression is a legal Java expression that returns true? ( i& U: \$ K1 M8 ~9 C+ k
A. b1==b2
7 f# W* |! z( {% q. w- SB. b1.equals(b2)
% t8 ~/ J8 H4 ]/ }) \( S- bC. b1&&b2# {6 w, v+ Y+ W' E2 a: C+ ^
D. b1||b2- w: w( D/ }. i+ S9 x: ?
E. b1&b24 d- P% W3 L* T: q/ S
F. b1|b2
+ n' `% x3 ^/ R) P" i: ?4 E2 S( v17. Given:
; ^: Q8 y; A g# F1. public class Employee {
7 U& [9 K' ~9 X6 I. |) l2. " z6 a9 Q+ w, G7 a
3. }
1 Y0 v# A* I8 v( ~6 u* X4.
2 g/ ^* }& l1 V5 F! X5. class Manager extends Employee{
3 u- c/ T9 ?" d1 Q, L7 ~& G: S6. }
5 ~# q) d0 g8 k! n+ ^. jWhat is the relationship between Employee and the Manager?
8 c$ b; R2 r6 q& }A. has a
2 L# }. H y I0 TB. is a
/ ]' M# _# A; g: aC. both has a and is a
0 g! O' V6 [7 }. ]D. neither has a nor is a
. M- b+ p; x3 ^+ `1 }! J18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?
`6 y. L2 N: J+ @. T8 l) L9 z4 WA. BorderLayout
( o8 W/ J P+ ]' o* j* }B. FlowLayout7 {. k1 {) P5 e- y
C. CardLayout1 x( v. S0 V y# U
D. GridLayout
7 ^ p2 _( [ H P% H. f19. Which of the following AWT components can generate an ActionEvent?; F1 d, V i0 V& @4 q7 w! ^7 z5 k; S
A. java.awt.Button
7 M: X$ c# K$ G, h* EB. java.awt.Panel* [1 e( N) n2 Q* j8 T2 [; Q: I: M: j
C. java.awt.Canvas
' x. ^# _6 r/ m$ {D. java.awt.Scrollbar
" E+ Q+ G9 x0 v* z: n4 g20. What kind of Stream is the System.out object?5 {- V+ N. l6 J1 j" u2 g
A. java.io.PrintStream
: |- r2 o3 N/ u7 a$ d- I0 O8 yB. java.io.TerminalStream
" x) b6 k5 l' c8 w; U7 e# @% ?C. java.io.FileWriter, i0 }8 A) g# |$ {
D. java.io.FileWriter
" h* `3 B: J/ {6 b- w6 ^3 s21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)
$ p5 F1 w( M' q/ E# L% LA. java.awt.event.FocusEvent/ Y8 l- U. H7 i9 p- I. r1 w
B. java.awt.event.KeyEvent- T) j0 |* d% i1 v. T5 T5 v T
C. java.awt.event.ItemEvent" v9 O# q8 P" L* S
D. java.awt.event.ActionEvent$ D& ]0 p) B7 S4 z- `
22. Which of the following methods are static methods of the Thread class?(双选)6 M* t" q# j1 l- q" Y% z" g* ?
A. sleep(ling time)) x2 x2 l0 W w
B. yield()7 c- ~; z+ @7 e( }* Y. I( l: _
C. wait()
+ [. ?9 J4 O$ o" D; iD. notify()
, O) `$ S" W. O$ ^2 S I9 t6 a23. Which method do you have to define if you implement the Runnable interface?- c) n6 D* G% x4 s: s0 \
A. run()/ N: ?2 i; d3 I7 v' o: H7 P
B. runnable()
4 i4 G- a' s- R' W {" }6 i7 P, yC. start()
4 v5 m5 d/ ?' E0 Q! ]4 _5 T. ID. init()# L% v6 A' Y7 ]* v3 r& U$ L
E. main()
- I$ D$ B) y8 c! Z1 g24. Given:
% [- e5 O! C8 {4 |# G1. class MyThread extends Thread {
6 M! ~( W* }* Q8 J" F2. ! ^1 X) H9 s7 [
3. public static void main(String [] args) {
9 X0 W" Q! X& k5 _4. MyThread t = new MyThread();
) k% {1 F' ]5 B# T6 [8 s5. t.run();
% C6 Y( Y. D) i& U( n, T/ R6. }. L+ B1 b( c/ c+ v
7. 6 b9 B, n% C: z5 I5 k
8. public void run() {
/ F4 Q& m; b% h. q4 `9. for(int i=1 ; i<3 ; ++i) {
1 p7 Q0 B* l7 G9 D9 B) R+ O6 \10. System.out.print(i + "..");
3 Y1 T/ v3 {4 r0 D/ C0 a11. }
! S. |. G5 E$ e, \12. }8 K( p( M. r! M& q% \$ A
13. }
% ^* E7 P4 G8 t+ O! h9 z1 L8 i7 RWhat is the result of this code?C
! H. A9 z/ X5 d" lA. This code will not compile due to line 48 A* u5 f2 G" i. j" a; W
B. This code will not compile due to line 5/ \; @* q; o8 Z9 x4 L
C. 1..2..! \: ^* c! h7 y( M1 Q) I( R# X6 U
D. 1..2..3..
. v' q4 |1 F) D8 M2 Z' n25. Given a local inner class defined inside a method code block, which of the following statements is correct?; d8 _ x5 d- Y6 ~7 g8 y1 A2 |
A. Any variables declared locally within the method can be accessed by the inner class! W: Y/ U( [" |" _( [' z3 z
B. Only static variables in the enclosing class can be accessed by the inner class/ T) Y( u, o( u
C. Only local variables declared final can be accessed by the inner class; U- s: K) M5 A$ Y
D. Only static variables declared locally can be accessed by the inner class.
; A0 n$ y: }6 @, o$ N2 b
( a& q9 U/ R2 u% h8 k* J% u8 U$ n# V8 o0 m1 O
+ z3 h( S4 w( C26.public class SychTest{ 9 \! o/ K) A2 \( J0 c; q
private int x; ! @3 e/ R9 R5 P- \ R+ p
private int y; 3 g0 ]* j' A7 n# M m7 s
public void setX(int i){ x=i;}
0 X1 s9 T+ S% R& }0 l public void setY(int i){y=i;} * W% D+ C- v! z
public Synchronized void setXY(int i){
4 a |1 c4 y( b; g% S! Z8 \ setX(i);
+ Z3 x! ?$ }( ^! ?' z2 Q1 e setY(i); ' T, F9 n& E1 \) i( x+ N
} ) D8 s4 a5 x# {
public Synchronized boolean check(){
3 S0 K) M( ^+ j S6 ^ return x!=y; 2 i* X' b2 @( y0 n/ D1 M& d
}
/ n" V# }% d; \5 r$ y6 D }
1 X9 K0 N+ S5 V7 U4 G9 i! A Under which conditions will check() return true when called from a different class? 1 n9 ?/ o8 r+ s+ S* ~( g
A.check() can never return true. 7 ~& j1 e- \- ~3 P& ?
B.check() can return true when setXY is callled by multiple threads. / n0 M' C' s/ t1 Y8 _5 i9 V
C.check() can return true when multiple threads call setX and setY separately. 9 W, W1 `: Y$ ]
D.check() can only return true if SychTest is changed allow x and y to be set separately.
- h) T( y7 J- \% [* H& s S% f1 F/ n- e0 y) L
27. 1)public class X implements Runnable{
2 a5 y! L4 n: t0 \9 l9 C7 n6 Z0 W. J 2)private int x;
. j5 l/ s+ n! h, K# b 3)private int y;
7 a% b# B6 J: \- T+ A7 \% M2 f) {6 O; } 4)public static void main(String[] args){ / O% {& Q7 U) ^8 N5 O) n- Y+ x/ c% x
5) X that =new X(); : ^7 Y: C! _/ U& s. A
6) (new Thread(that)).start();
9 M9 M+ t! b1 G. W D 7) (new Thread(that)).start();
0 D7 ]: w/ z: @ } * H) r; `' v; D' L
9) public synchronized void run(){ $ p; V% A D2 ~
10) for(;;){
+ \4 j N/ r) E4 V- u7 I9 [ 11) x++; ) }: ]# y, c6 {. L& P7 ]5 {# [
12) Y++; 8 V: G. b% V* k+ {
13) System.out.println("x="+x+",y="+y); . e% V0 E# q0 Q6 s6 A
14) } 1 \+ T% M8 c$ [2 E# ?' Z$ `$ [
15) } % y$ L& K! j- Q5 |
16) } * B* p) a3 {3 }/ ~
what is the result?
# D# h7 u& R" h6 x0 i6 P* V A.compile error at line 6 % N, o2 m" ?5 M8 h- h: Z& t
B.the program prints pairs of values for x and y that are
' Y; _; a, }" a% S. ]( _" D always the same on the same time
$ w; s) E% u+ J! Q4 n1 q) V# P
3 L: S7 U; Z4 K: P ~0 |+ |28.class A implements Runnable{
+ \$ @- ^8 w4 G int i; . d0 B1 ]6 [$ O9 B( a
public void run(){
9 @' y' @* P; e& E) x' U0 H try{
3 L! P4 b( k |5 q' m8 c. Y" d1 H Thread.sleep(5000); w2 ?* i/ f0 u9 m
i=10;
* n3 w& R/ {4 V& h! | }catch(InterruptException e){} ! s: K) i& u: s. _5 j- f) X1 G
}
) G0 _: }# c" U7 o } ( ^4 J0 E; l& P( S8 D' C7 \
public static void main(String[] args){
/ J! m% ^2 I8 u' o- l5 O/ o0 Y5 I try{
2 U7 H) r! ^+ s0 y& S A a=new A();
9 U1 |% J+ K- e, G/ D Thread t=new Thread(a); ) e0 }+ N9 l' K) U1 v! O
t.start(); + L- Z; |4 t1 [5 I! @( T; z
17) 7 v) W' J% z' f3 L/ _2 l
int j=a.i; + |7 i$ ?2 l/ z/ p+ x- f# f
19) 1 f" R3 \# |9 _. j2 l! i
}catch(Exception e){} ! R }. H. t7 U6 \1 [; [+ B
}
- P% D$ F2 \4 U0 h' m }
1 W7 r1 t8 b* [$ F what be added at line line 17, ensure j=10 at line 19? 6 c1 p" c! J4 \4 l
A. a.wait(); B. t.wait(); C. t.join(); D.t.yield();
8 \# x& T8 S8 N E.t.notify(); F. a.notify(); G.t.interrupt(); . F' A& R# ?( t/ N/ N1 H8 K
29.1)public class X{ 1 B7 b! c8 R7 n& w) a
2) public static void main(String[] args){ & y; `7 J, K' x g; ^. A
3) String foo="ABCDE";
* d4 J* |6 i7 n4 R9 f 4) foo.substring(3); / o ?$ H5 F+ i1 y0 r
5) foo.concat("XYZ");
3 O# G5 f* s/ q' K1 b0 M 6) } ! J& j' ~. n0 e4 V# c9 u7 o: C# C
7) }
, X& J2 x% ]' `: r what is the value of foo at line 6?
) Y* [ E6 h; n30. public class Test{
3 ^" v/ \& ^, @1 L# X3 C public static void main(String[] args){ % {$ K9 a% v- x, P% i* L3 s$ F
StringBuffer a=new StringBuffer("A");
+ w0 j: K; u/ {- {4 T! g StringBuffer b=new StringBuffer("B");
* h! {9 W2 l6 x' d3 t8 a: O ~ operate(a,b);
, C& |! |# ^: t( W System.out.pintln(a+","+b); / `. M* t& }+ ^
} # B+ l1 ^+ V) F6 U+ j9 i( S. f$ b. y
public static void operate(StringBuffer x, StringBuffer y){ 9 p4 {7 c$ B. \3 G- u' L$ _
x.append(y);
4 s( `0 ]8 {0 v% b# [ y=x; P/ S$ w0 [7 b1 _
} " f9 v0 k# h3 l9 |+ P( C3 J
}
3 D! Z, }$ Z' B6 }6 c what is the output? |