Sample TextJava培训考题
: ^, u: s1 d& a- t) L+ L* E0 _1. 给出以下程序:4 d1 R) N4 H9 E7 t ~* `3 D2 ^
1. public class Colors {
6 g' Q Q5 C: R, u( d) k$ \3 R2. public static void main(String args[]) {6 R/ N1 f3 e4 _* y) E; C6 o
3. int n = 1;
9 T4 L. e2 I- }* z* O( W4. System.out.println("The Color is " + args[n]);8 V" F, r" z7 O5 {+ ]& h
5. }; O. e, G4 A; p, D; f
6. }* E# d, e- U4 C; o9 q
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”* l# |4 p! D0 V5 E! o% ~2 X
A. Colors red green blue yellow
' Y% {) {/ a) F* b* |9 O4 ]7 a* ]B. java Colors blue green red yellow2 ^0 E! T: H0 T* O$ M' j4 Y
C. java Colors green blue red yellow
% [: f; m4 K6 {D. java Colors.class blue green red yellow
. T4 T+ @4 U4 ]% s3 Q' {3 vE. java Colors.class green blue red yellow* I& U" f, B3 E K8 R' m y
2. 读程序:
+ D7 W+ P5 R0 c- e( H% S1. public class Test{) i) ^ y$ g3 X
2. public static void main(String args[]){$ d7 y+ c4 S+ f/ q7 b1 ?
3. char c = '\u0009';
% ?0 o! ]; v& S$ M, z7 `% ?, x6 X4. if( c == 0x0009L)0 M& ^! `8 g4 g m* N! m1 ^
5. System.out.println("Equal");! l, @+ h8 |: }4 K
6. else System.out.println("Not Equal");& x2 O5 s+ \- C* b
7. }
N" i* F: e5 ]9 }5 L# ?8. }- E# r- S* Q4 Y* m# n* c* S/ v
当编译和运行该类时,将会发生什么:
! K/ D# p4 i& M" CA. 程序因为第4行将出现一个编译错误
* C2 m k+ p+ q: |B. 程序因为第4行将出现一个运行错误4 i8 L5 ~0 k( ~" P3 d; m
C. 程序因为第3行将出现一个编译错; g. |4 r/ ?( P3 q
D. 程序输出”Equal"3 i$ V: [8 L) a
E. 程序输出"Not Equal”/ {6 J) Y5 ?3 g: y
3. 读程序:2 q7 I2 i: Z" W/ W& X" W; } T
1. public class StrEq{
1 k" A! } B) d1 Q# d2. private StrEq(){7 k' s3 w$ Z' H1 l+ T1 }
3. String s = "Bob";
& j% O6 |) W5 G7 n4. String s2 = new String("Bob");
7 x- ]3 o) W$ U/ N5. if(s == s2){; }# v( e# J) }! ^4 k
6. System.out.println("Equal");
' s+ p4 i3 G7 J' p: |! Z$ d7. }) ]' w% _+ o/ h7 o }. N
8. else{. O) \4 U7 D! d: E9 x/ Z$ v$ r
9. System.out.println("Not equal");
. y2 y& W8 t8 `+ |! a Q10. }
& r) K9 E' X, ~! U11. }
1 K$ z# A9 m( i( Q+ G12. public static void main(String args[]){
+ F- Z0 I& ^1 C13. StrEq s = new StrEq();2 \$ `: E) Z% }% l" d
14. }
7 b* ?5 S: c4 Z7 } [9 W( Z5 l15. }5 [' {/ E. x; w
当编译和运行该类时,将会发生什么:) |1 w6 |3 `" J
A. 程序能够编译,并且输出“Equal”5 o! K2 o# r8 F
B. 程序能够编译,并且输出“Not equal”
: M9 H) w& a& fC. 程序因为第2行导致一个运行时错误
9 f7 o5 n; D* E1 S VD. 程序因为第2行导致一个编译错误! y. H1 y! d" R9 C2 L& V# S2 q! C8 K
4. 以下那个赋值是非法的:
/ w+ ^" n/ ~% G2 i% m; c# c0 RA. long test = 012;1 \: _ E. ^1 ~) O o* q' ^
B. float b = -412;! d; D+ L6 s! M8 x- R6 e
C. int other = (int )true;: j1 u7 v% O' _5 F3 T
D. double d = 0x12345678;5 Q: ?. c* x1 {: G; w6 }# p; V
E. short s = 10;2 q7 `7 y% L8 F" T! [8 [
5. 读程序:# h. p+ V4 m) [
1. class CompareString{% P2 {( L j$ y {9 m" A
2. public static void main(String[] args) {( j& A# _+ e0 t) E
3. Integer x = new Integer(5);
+ Z4 |; _' {3 r. D* G: J4. Integer y = new Integer(5);; z" L" T& H. n7 N
5. Integer z = x;$ e' p, Q* p) R
6. if ( ______ )8 W$ {) R$ Z, i9 b6 Q
7. System.out.println("true");+ l6 c6 O6 B/ {: X- g7 j
8. }( E$ W4 @, r M/ t5 F; p4 H" d) W) c
9. }; ~& I" N7 a: F: y$ ~
以下哪个选项填入第6行后,不能输出true:
0 R0 k& R1 i8 A, FA. x==z
4 \2 b* ~* |* X- M' ZB. x==y
/ N! M+ ^5 m7 | [0 n9 NC. y.equals(z)9 s/ U3 n4 b+ \) R7 \7 K" M9 H: F' G5 T
D. x.equals(y)3 u1 R0 r8 |, e- D! G! w# t: t9 M
6. 读程序(双选):
/ E6 M& b9 m8 m6 I+ }7 r1. public class Declare {
" p j# z& }5 N3 v9 G2.- h, |; w: B- F! O
3. public static void main(String [] args) {0 E) g, {5 y; T5 [
4.5 t& F* I6 H6 n$ q1 ] u
5. System.out.println("The variable is " + x);
5 |. d8 B$ O; R6. }
( K! C, C9 X+ K, L7. }
6 g, v8 t6 P% v+ G" m以下哪个选项中的代码可以使程序编译通过9 D b3 s p5 A+ h$ w2 F( K7 }
A. 把“int x;”放在程序的第2行
0 k5 J; f/ ~8 G: e5 D" KB. 把“int x;”放在程序的第4行; u: I9 b; S0 T$ v) G
C. 把“int x=5;”放在程序的第2行
- r) ]3 Z& R7 b/ z- }* }D. 把“int x=5;”放在程序的第4行2 G. l: i4 y) ?/ H
E. 把“static int x;”放在程序的第2行
( a4 k/ H* X7 ~ F, m
* |. V1 O; r0 o& V, O
! A' ^ o! F, ]! Q7 U8 [4 m1 q1 J6 W3 a9 F6 Z5 ~5 F; F9 t
7. 读程序(双选):
3 p& ~4 n* q4 {) m# j' S) ]; B6 V1. public class Child extends Parent{
" o+ g6 m% W1 j3 `* F2 w R2. int a, b, c;
: `- G! Q# b+ }3. public Child(int x, int y) {
5 P- o9 j1 F# G& v, R) T/ r4. a = x; b = y;
$ |: L7 R- k! b7 q5. }
4 @# ]: S. g% R+ P' ~7 ?1 C! O6. public Child(int x) {
8 N9 H W) Y9 v, v7. super(x);
X- ~% H, k# v/ E* |4 J8. }
$ Z/ k" m# k# U& O' d9. }1 a0 M7 U% P) [- Q5 e
为了能使该类编译通过,下面那个构建器必须出现在父类中:! ?9 o. N9 M5 B8 r% _) _8 e
A. public Parent(int m,int n)! V$ ~/ s) _& o5 Z
B.public Parent(int m)8 I3 S- E2 t2 x5 H8 m
C.public Parent(), f/ X6 r1 c; O4 m# E/ h0 n
D. public Parent(int m,int n, int o)2 p! F! A+ M* K/ a
8. 读程序:
6 I5 D' c& w% f2 v1. public class Derive extends Base{: V7 C, k2 V6 @+ W. d
2. public static void main(String argv[]){4 C1 A9 @1 q2 e4 j( }$ O' b
3. Derive a = new Derive();
% q! E3 V7 U( `$ b: {4. a.method2();! I$ z6 i* T# t5 I v* B% b3 k
5. }
1 I( W, f/ k" U3 [* P3 `0 V) C6. public void method1(){
7 Q6 r5 d- z5 ]) l7. System.out.println("method 1");
6 c2 e7 T, b# }# w8. } }& g# y ]! v' k G
9. public void method2(){
Z+ S. \" @ k$ F( }& D, A/ a! j) r$ [7 @10. method1();
( ]: {5 k! O8 O6 }4 c2 a, X11. }4 L& l1 |+ f( ?. N g0 p
12. }/ }- Q7 Z: H/ e
13. abstract class Base{% T6 ?) P9 k( x) D; Y
14. abstract public void method1();; O5 h5 i4 l8 ?; O* }! Z0 X1 e
15. public void method3(){9 v* H) {0 Q& g& s! D1 [9 m
16. System.out.println("method 3");6 O v: d$ E# U3 M- P
17. }
1 }/ D/ t, K/ B6 ]9 H$ s$ h6 v* N}, a- N3 ] D) v* V
当编译和运行该类时,将出现什么:* F$ K" v5 Z: q
A. 程序输出“method 1”/ I7 u. Y* N7 E B
B. 程序输出“method 3”* e( }; F- O. X: C- u! Y+ n. e
C. 程序因为第15行而出现编译错误
) @( N3 }. C( q) ?D. 程序因为第15行而出现运行错误
0 k( H c# T+ M) H% ~7 ]: E8 |/ s9. 以下哪个接口的定义是合法的:
6 a/ v/ i# _, z3 [9 _, r aA. public interface A {int a();}
{6 s$ S! R6 p1 t7 V% rB. public interface B implements A {}
7 l c" ?3 Y0 oC. interface C {int a;}( n1 x# J% s( K: }% @
D. private interface D {}
( i q' L, G; }1 I! C4 ?10. 读程序:$ ~2 E3 c) l, K. a4 n: x4 `* v
1. //File SuperClass.java
8 Q& t" M. o- J% T8 x2. package MyPackage;
6 w+ y Y; f! d. _! n& M5 q: `3. class SuperClass{
7 i/ h0 }6 N$ S) L' K/ z4. void myMethod(){3 C" o- d* @) {0 {1 A
5. System.out.println("SuperClass");; o( y" s' I G# F2 k9 c9 }' x
6. }
5 [1 J7 K; X% o1 S/ g# T+ W7. }
+ s+ c# V3 N1 |3 ^9 H8. //File SubClass.java* s. X- B! Y0 k) l( A% Z
9. public class SubClass extends SuperClass{, ~! m' P! D/ k9 X, H/ _
10. public static void main(String[] args) {
% Z0 f+ T# B0 z# d11. myMethod();
U P% z. J/ T" ~1 y0 q( F! Z12. }
8 i' q' p- b! a) j* e# [13. }
- f- Z$ g- {1 G6 |3 N当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:
) b7 p. l! `" \! ~A. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”
/ K1 c. U( w' o& k& ^B. 两个类都回出现编译时错误, `' Y! b: b3 u! b6 Q' [
C. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误
, c& k! U, V% u; ?; d8 fD. SuperClass.java可以编译通过,但SunClass会出现一个编译错误
g& }! A' [& Q' _) o8 Y& G: w0 }11. 读程序:
. l k4 D, b/ ? z' ]1 Q! i1. public class Test{! S0 ?1 F2 N, }9 i9 V, o+ z( Q% ^
2. public static void main(String args[]){3 H: k/ }9 E* x) J
3. int[] i = new int[5];
, G0 [( G0 f. e; ~4. System.out.println(i[5]);" V$ p* u7 S2 [: V) [
5. }: [) g3 N- ] e! Q0 z
6. }
) @- x k5 L# E当编译和运行该类时,会:
2 q" x5 ~" w; _4 ?! G) ]+ V+ }! |A. 出现编译时错误
7 [: B4 _# b9 p2 A# CB. 出现运行时错误
, O1 p' J2 Q9 ~. U7 y/ oC. 输出0
% r% `0 B5 ^5 d. p$ ]- G$ jD. 输出null
/ C- N+ }, z1 o& G* P8 M1 P9 Q12. 以下哪个选项是正确的数组声明或初始化:
' i5 R/ c/ }: I1 q3 wA. Array sizes = new Array(4);
% |( P6 } ]# _; k0 `- HB. double [] sizes = {1, 2, 3, 4};9 s0 D) \) k7 z* {- `; }
C. int [4] sizes;
& m0 y, `% Q% F7 c# |D. long sizes [] = new [4] long;5 r5 f0 z6 z5 [( r
13. 读程序:1 I6 X. n8 l( x) U
1. public class Initialize{
/ u+ l1 T4 \% k; I$ J5 M/ q! p0 ], L2. int x = 200;! U- O( _- G9 C# r, x/ s9 ^; ]" p
3. static {
1 K8 s. m$ `3 \# s2 h% |$ F9 }4. int x = 100;
; l8 ?6 Y8 P. `5. }& c0 L! b/ u% ]. A! Z) T' ?2 T
6. public static void main(String[] args){# y* g9 J. d4 T8 ]7 S
7. Initialize init = new Initialize();
7 N, j; |" Z+ X$ D0 d4 U7 x5 d4 e8. System.out.println(init.x);0 f: U; b( O& ]( [& l |* \
9. }
1 a6 [9 a6 n! F/ D s$ w10. }
. ^5 n" B% y$ E" Z H: l当编译和运行该类时:5 F u7 s3 ^: L
A. 程序因为第3行产生一个编译错误
9 S" Q+ R9 J% U0 YB. 程序因为第4行产生一个运行时错误
& u% P5 I# z' I$ M8 U9 w; \C. 程序输出1006 a3 s) o* w8 K4 @6 }' }; d
D. 程序输出2002 A" m$ A9 R# U2 L* s
14. 读程序:8 L( O x8 Y4 ]7 v5 {" q& ?: j
1. public int abcd( char x) {$ t1 j% V4 n3 @4 D3 G' e( F* Z, `: M
2. if ( x <= 'M' ) {
9 r5 m* N/ b4 I: ~! _$ S3 } B3. if ( x == 'D')
o0 [' B+ L+ r" W8 o: ~( V; V4. return 2;! b+ U# B5 R& V! i* ^
5. return 1;
{4 T! e0 {' {4 Z3 r6. }
+ i' B. |2 m! w G. G7. else if( x == 'S') return 3;3 T4 G' t' d6 N" t" B
8. else if( x == 'U') return 4;
+ \1 z9 U" {- w l! v7 e4 k9. return 0;* x/ F8 g5 C$ @& w" N# b8 ?- A
10. }
6 J: S; a0 F$ t, s在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1
- v0 ^- G+ g. B2 R9 tA.‘X’
5 }# m2 U& e O( e0 R5 k' MB. ‘A’
R! u* ^: T( Z* |+ vC.‘D’
/ v: ?" r( \0 k/ T% D+ u( p& ID.‘Z’. d E* \/ ~8 J9 J- i! X
15. 读程序:- o8 ^+ w6 W9 i
1. class TryTest {
8 b$ F5 _, ^) V7 N, [2 f6 s2. public static void main(String[] args) {
3 _" j/ d) i9 D6 p' \3. try{
1 I4 K2 x" I* q3 G+ D4. System.exit(0);
1 J+ h G9 [/ S" e; p5. }
, {5 E8 V/ d( h/ D5 ]6. finally {
. \1 P# P$ w( d7 o3 y7. System.out.println("Finally");9 J; u) T6 F4 L6 _0 S
8. }- B9 {9 J$ B; j* Y5 B0 l6 @
9. }3 M! `; L* r, x$ Q' \. A9 ~/ \
10. }
! J$ p! |% l7 R当编译和运行该类时,将:% u6 t+ G, b3 U, j' E# r) A$ I
A.程序因为第4行出现编译错误
6 G+ K$ j- r7 n( {; Z( Y. C' ]B.程序因为第4行出现运行时错误3 A; a+ u9 E! i1 |+ N7 @- n
C.程序因为第6行出现编译错误6 G7 j+ r5 r; ^- `
D.程序因为第6行出现运行时错误# l9 r2 M6 D: R
E.程序输出“Finally”
2 u" a' x# P+ GF.程序可以运行,但什么也不打印: h2 G5 S/ S6 Y; B4 I
9 q6 q3 f0 A2 ~/ d16. Given the following variable declarations:9 Q2 v6 q' t- D# a' \; y
Boolean b1 = new Boolean(true);
8 F V* x3 D3 X: r9 Q4 PBoolean b2 = new Boolean(true);' P) K3 {5 q! W9 n; Y3 \
Which expression is a legal Java expression that returns true?
8 M0 n- k# k( uA. b1==b22 d0 u$ |1 _7 f- Z# Z
B. b1.equals(b2)
! d4 C$ W; K/ X4 d4 \% x/ MC. b1&&b2
+ x- d! o1 g! e. X/ l! Y3 }; Z4 Z% |D. b1||b2
6 q+ W0 B' @1 x0 V% vE. b1&b2% N0 _! m* [# S0 X: H# |* d' N, x4 \
F. b1|b2
2 A; m. R0 u+ r$ E, v17. Given: $ n7 o3 ~8 }9 S: c+ _- b/ _7 u
1. public class Employee {
1 ]# y6 ?) Z1 v: j2. + J0 m, p$ w. ]2 k/ v0 X
3. }2 E- K2 ]: d/ d# ~6 D+ A& D
4. & n" c& {4 l2 y2 ^" V w& Q
5. class Manager extends Employee{! H. m0 ]" t3 r8 @! e
6. }
$ R _0 E6 F+ s9 r! JWhat is the relationship between Employee and the Manager?
0 k) E4 y7 @" v5 jA. has a
2 C3 O8 R+ q0 I6 q$ SB. is a
6 F$ M9 {/ z" ?+ Q1 L0 }6 {C. both has a and is a' d8 v2 _4 T `$ @: t) l
D. neither has a nor is a7 g' r) I `- R. x( }, D% f, z
18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?
}1 N/ f+ S" P/ b( KA. BorderLayout" J9 v Y5 L8 ~: S, d& k( ~
B. FlowLayout2 C6 U& Y2 A% [+ m. J R5 h
C. CardLayout) x- \% l4 e6 J w6 o( E, w# D
D. GridLayout
# R- Y: M: N+ g( I! q19. Which of the following AWT components can generate an ActionEvent?) Y2 p+ D2 @8 ?4 [$ ]8 v! t
A. java.awt.Button
% | Q. Z% N( H* c' a2 x/ i$ kB. java.awt.Panel* {, _" f4 `8 y X) Y. T' W/ p
C. java.awt.Canvas8 |5 h( b* V5 h% e% O; D; y' G
D. java.awt.Scrollbar5 @! h% b3 [- q/ v5 E
20. What kind of Stream is the System.out object?
6 l: {2 z" u9 A/ [A. java.io.PrintStream
* V! i2 j) y$ h! M3 R: |3 t, GB. java.io.TerminalStream
* C* K; e5 Z- y5 h8 O' N/ cC. java.io.FileWriter2 z* ^* D& T4 Z f& a1 b
D. java.io.FileWriter1 J5 ^# t! p9 f- [; H, r8 v
21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)2 m( r9 t+ ?+ s( V2 Z" E) F/ G+ ~
A. java.awt.event.FocusEvent! u% v" v) I1 n3 O3 j
B. java.awt.event.KeyEvent# S8 q) r9 n1 m3 }
C. java.awt.event.ItemEvent1 P r: E5 Y; ~
D. java.awt.event.ActionEvent
9 O7 b& W) I, z" r3 K+ V! R22. Which of the following methods are static methods of the Thread class?(双选)
7 m% d: {& U! ^! H9 g4 ZA. sleep(ling time)
! l6 o$ L7 g A/ L& @' a- oB. yield()
8 ]! C4 n: @1 C9 M& s" N# v7 q5 `. ^) Y8 WC. wait()
: F5 p3 N, L/ J0 ^D. notify()7 S6 E9 C! j. [
23. Which method do you have to define if you implement the Runnable interface?
. @ a! ?+ j5 u$ [: j6 uA. run()
) }8 L. o& A8 T8 nB. runnable()6 r& {# g, O; U3 @ B" T
C. start()
~$ o7 U: b/ m% R& lD. init()
6 I: s% l: d4 G* x4 xE. main()" `% m/ y& `+ n' N& S" a
24. Given:! A, ]8 _0 S; Z p
1. class MyThread extends Thread {
3 k* B5 D2 m8 D3 o% u2. " J% @& T! Y* `' I9 }3 [+ f
3. public static void main(String [] args) {5 K; V3 i' B. u) @
4. MyThread t = new MyThread(); }, c: ?5 u3 \6 v; c8 t4 [* f
5. t.run();
6 g5 o- ~$ g4 J- U9 f+ D' V9 T% k: c6. }: H3 e. H7 T& O2 z% r
7.
! G/ R) Q+ C8 b; `: t8. public void run() {; [! T' o, }% _: h* ^5 o9 O F, `1 v
9. for(int i=1 ; i<3 ; ++i) {0 y5 |7 u3 \" i) Y) m
10. System.out.print(i + "..");
8 `# X! o) c' d! i% B7 X$ Y" Q11. }
, R6 `$ A; @+ w* q" h12. }& k4 }( X! T# F. g6 j, ~
13. }5 @8 z! X0 ?. w# c- a; u7 g
What is the result of this code?C6 p$ t9 m; t3 l
A. This code will not compile due to line 4% Z F' O1 L( {/ F
B. This code will not compile due to line 5
; @+ r' |9 |; S/ r# ?& {* Z; mC. 1..2..
3 ]% Q) W5 d+ p! b+ L- l' KD. 1..2..3..
( w7 ~+ ~( k. p& _: D6 ^25. Given a local inner class defined inside a method code block, which of the following statements is correct?- r1 h! L5 ^+ Q
A. Any variables declared locally within the method can be accessed by the inner class
% H; F1 B1 x1 i e' w) Z! u: z) M8 kB. Only static variables in the enclosing class can be accessed by the inner class
. ^$ t# }$ g5 P3 C; X# O. l' jC. Only local variables declared final can be accessed by the inner class
1 l; E( J, l$ ?+ ], s# b( sD. Only static variables declared locally can be accessed by the inner class.
( o/ G* \& n4 u) c2 d
! T6 A/ H/ N. F5 p+ G; E
# ]& R1 V, t. K8 ]! c5 T: }$ {+ `# ]* c
26.public class SychTest{
/ S( M% Z* o$ U9 M" D private int x; . j+ m% o; g R" G7 `9 X6 B1 L7 g
private int y; * u: E/ f; K/ ~5 Q& M& b
public void setX(int i){ x=i;}
5 _# o- k8 U2 d3 H1 o: f! F1 S$ e public void setY(int i){y=i;} 8 v* v+ n I6 C& n7 ~; ~: ~
public Synchronized void setXY(int i){ 6 b. h( i5 r7 `8 Y
setX(i);
3 Z {1 t; l! D F0 f! Y' x$ p5 C8 k setY(i); % C! o, }, Z$ Z/ v+ s5 Z/ @; Q
}
9 K" I2 ?" [- ~0 S) k- b- R' v public Synchronized boolean check(){ w: Z* }- g; x9 |
return x!=y; 2 H; Q) t5 `0 Z5 H1 w7 r
} % E0 J! h9 \- @
}
* f' V' l. D) ?9 Z1 Y" i Under which conditions will check() return true when called from a different class? 2 ?8 [. e1 F U
A.check() can never return true. ! ~4 A- R1 `: \; I
B.check() can return true when setXY is callled by multiple threads.
4 Y6 Q4 _8 K% m# ]7 d C.check() can return true when multiple threads call setX and setY separately.
8 g" ~7 u5 k& L0 H9 F& e+ y D.check() can only return true if SychTest is changed allow x and y to be set separately. : L2 R3 h+ a+ I) e& K3 G6 }( ~
# |; q% ]- U1 q# ~# S& p' S8 O27. 1)public class X implements Runnable{
% Q/ V' K2 P" B4 D$ [ 2)private int x;
% x9 N8 I$ h# W5 p3 x! w 3)private int y;
2 d# m+ U, j7 `4 H# G: A" I& i( \, | 4)public static void main(String[] args){ / U# q3 ]# }' K2 I% G7 g
5) X that =new X();
) G3 ?1 Y; V8 r) y/ W& v5 c+ f 6) (new Thread(that)).start();
1 z4 ~- s/ b: r. A8 u* Y1 Y) u 7) (new Thread(that)).start();
- B2 G: I' ~* E; e8 D7 o6 { } % r- |9 D( U7 A- t
9) public synchronized void run(){ 6 U* L' ^ T8 x0 X& W) L2 G
10) for(;;){ # W7 M" l8 x. ?& ~, ?9 R
11) x++; ' a: |0 i1 ]% N2 D- b9 \) M
12) Y++;
3 n/ \6 B5 T' f- O. J% a& [ 13) System.out.println("x="+x+",y="+y);
9 ~% o2 l# Y( P2 h6 ^ 14) }
1 p K, E3 q+ K 15) } n% `6 H% ?* U
16) }
# ` h& V( P& @5 S o what is the result? - u0 {& X. _& A( \
A.compile error at line 6
' P! u7 w1 H. i% Q/ `* S, f8 A8 V B.the program prints pairs of values for x and y that are 6 f9 G G6 S% E1 U
always the same on the same time & s E# v6 X+ M" h2 G& t/ B3 z
: F: V9 Z- k8 U3 u
28.class A implements Runnable{
3 s$ Z8 e9 C# U( | int i; $ i8 z$ S( _, x& x+ o$ l3 n
public void run(){ # p1 v( e0 R6 g Y: O3 ]
try{
, X! u+ k+ s3 X# X" s" c7 ~ Thread.sleep(5000);
/ E4 o- T V' e* { i=10;
; j' n! Q" O: L1 a8 t; C }catch(InterruptException e){} 2 s" P$ d, q- {* K, D, u4 Q
} 8 n8 W% b. F( T. [1 A
} ! t- U! y! A7 l+ |& J" n5 \
public static void main(String[] args){
+ a- J% t2 y7 a( i/ L/ F; w0 X try{
# u, C, H0 R+ y( h A a=new A(); ( n9 V! d7 p5 ~9 x) q
Thread t=new Thread(a); % l- H3 Z Q8 s# _' m7 u, Z
t.start(); ; k" \6 j" B4 x0 }
17) # C: }. r7 p/ ^" Y/ L2 _, V4 R
int j=a.i; 2 c" T0 h* j! @6 Z# F3 K0 e
19) 7 h7 q# c4 _& B& ^
}catch(Exception e){}
* e( z' Q) v4 k V. c* c3 r }
. K( P2 x$ g8 C8 H/ ^6 h/ [ ` } ) g% k/ \' o' L! ~1 B! u- E, k4 T
what be added at line line 17, ensure j=10 at line 19? 9 u4 X" A4 E0 @6 r8 v- H) y" t6 D% x) p/ k
A. a.wait(); B. t.wait(); C. t.join(); D.t.yield();
! v. I: b& x8 V0 G p E.t.notify(); F. a.notify(); G.t.interrupt();
* v9 t3 a* T0 a7 O4 K7 |: J29.1)public class X{
' p! X. B. m. ?; r1 M! |) f, b4 J8 j 2) public static void main(String[] args){
/ x7 G$ U: @. \, j" X1 V 3) String foo="ABCDE"; - \5 ]4 z+ p3 ~' ]0 _5 }
4) foo.substring(3); 2 g# P/ c+ r5 f+ u1 L
5) foo.concat("XYZ");
8 R6 ~( y! `+ B$ p r( f 6) } - ]- }: S! d- ~. o3 \- r1 d
7) }
: S$ M+ E' F9 k* R what is the value of foo at line 6? - Y" C3 Q4 i D% \0 J
30. public class Test{
2 q) G1 L% ?% j public static void main(String[] args){ 0 P" N S# u* B, H8 t; A
StringBuffer a=new StringBuffer("A"); $ X* T/ w/ m( O
StringBuffer b=new StringBuffer("B");
# p. F# a4 c3 a1 f8 } operate(a,b);
" b: [! t2 d- w' R& q! f9 k System.out.pintln(a+","+b); ; y6 ~0 S& N( D
}
- z/ K( Z7 F3 n! c: e( p4 X public static void operate(StringBuffer x, StringBuffer y){ 3 r8 | A' W9 h
x.append(y);
/ u) d, F2 b% R/ U y=x;
D6 p" L6 x; | F; H7 X } . s; X% b& x4 z# h* O) T% o
}
. z" O: O8 i' M& {: e& m what is the output? |