Sample TextJava培训考题6 L. z: ~% x; O# ^- @9 Q
1. 给出以下程序:7 }1 x' P: g5 T4 V) X8 W9 A
1. public class Colors {0 Z# u* L) d; W. P' k5 z$ [- s
2. public static void main(String args[]) {7 q8 X3 `/ y. v1 K! K3 M7 K
3. int n = 1;( N& w( n: z$ X5 q n) F" J
4. System.out.println("The Color is " + args[n]);+ W4 G& H9 D: h C+ t
5. }( D6 `% I2 s/ i
6. }
0 h# X. E" n& W; t假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”
% U2 F9 ` H% [A. Colors red green blue yellow0 G5 s5 f$ h0 E& I$ y( e0 z
B. java Colors blue green red yellow C% d1 N2 E- @( r7 A; H" J+ h q
C. java Colors green blue red yellow, S$ s+ |0 z4 n, D9 ^* K" ]
D. java Colors.class blue green red yellow, M, u( j! O! u/ {: h3 X) [ e: m1 E
E. java Colors.class green blue red yellow
; d. r" @" ]' F, s+ S( ]* L2. 读程序:+ F1 A# I2 [3 O( V) i3 G
1. public class Test{3 l' g& _1 N2 x' t: g$ d
2. public static void main(String args[]){* o& A% U2 s2 F$ q" Z; M0 s' ]6 K
3. char c = '\u0009';6 n+ f7 I8 T! s) H2 J
4. if( c == 0x0009L)
! @3 H$ ~) Q: n7 U5 W9 H5. System.out.println("Equal");
$ I! _( }9 m7 E) i k8 ?, D6. else System.out.println("Not Equal");
2 p0 \6 Z. J) r; a7. }% }3 ?0 ^) [5 l Z2 u
8. }
: S/ j7 _6 c& d+ N( s/ f; L当编译和运行该类时,将会发生什么:
( k! L$ J- z; x: K; XA. 程序因为第4行将出现一个编译错误+ X# [" @2 J0 Z/ _
B. 程序因为第4行将出现一个运行错误
! r( z. o1 b, h; b1 D0 ~/ I9 }C. 程序因为第3行将出现一个编译错
, `* Z) E1 ~* lD. 程序输出”Equal"6 Z" v4 W9 X% ~& C4 K
E. 程序输出"Not Equal”
9 ~( v; v" S; z6 t0 i8 T( D3. 读程序:7 N: P3 @1 K7 H& U
1. public class StrEq{* w3 S5 z* z- O. q, E2 ?6 f! f
2. private StrEq(){
! L; @2 q& H% }. ^; `/ y3. String s = "Bob";
4 o. I% l( b1 C- p4. String s2 = new String("Bob");, Y4 l% j' z; c2 d2 h' o- }' i
5. if(s == s2){' W7 b+ e& v- [! x5 G
6. System.out.println("Equal");, q3 u! g9 b! h2 G: o
7. }) b9 B$ G! b3 Y4 x
8. else{' Y4 Y& E# A5 \% X4 x
9. System.out.println("Not equal");
* S3 ]! `, q6 D8 g10. }7 |" X1 a1 ^* v# t d- p3 ~
11. }
: L! [1 C. R; U$ y7 O. R12. public static void main(String args[]){7 Z( r ?, g9 b6 z/ `' U# _* q
13. StrEq s = new StrEq();
h' M* m! C3 v6 S14. }
: _3 L6 O( K) i15. }, w+ ]8 ]; y. Z3 J" b2 G9 w
当编译和运行该类时,将会发生什么:
9 J$ ^! G7 N6 T/ r' v, H" sA. 程序能够编译,并且输出“Equal”. J# n, u' W: G+ m) K
B. 程序能够编译,并且输出“Not equal”1 W& v3 x3 y3 ^% ^" y
C. 程序因为第2行导致一个运行时错误
- t; z7 N6 w% ^& J2 c; I; q: `' ~D. 程序因为第2行导致一个编译错误/ t; B* I# w4 {- Y( M @6 ~
4. 以下那个赋值是非法的:
- y) w) g+ S: y! C+ |8 w' mA. long test = 012;! _& L! v" g/ {
B. float b = -412;
% K( `3 `& c0 X# s4 B# h) Y! wC. int other = (int )true;0 S$ f* t- H( P7 A7 l
D. double d = 0x12345678;6 y/ Y5 [3 s+ O: r. _1 Y
E. short s = 10;
8 P+ `- D( i) o0 ~5. 读程序:
6 k: W( F% y8 @/ ]; Y+ u9 f) ^" Q1. class CompareString{4 A8 k2 L8 A" Z# h2 I
2. public static void main(String[] args) {
q* O* W4 u1 E3. Integer x = new Integer(5);
! Q. z7 i3 ]4 ^# X+ M4. Integer y = new Integer(5);
1 ~. m% z# B1 S2 l+ V5. Integer z = x;) W$ o! Q, V. w8 \
6. if ( ______ )
: ?/ Z4 _7 Q* o, N0 T1 G7. System.out.println("true");
) Q+ @4 `! O) g% Y4 L8. }/ w: e8 u4 B# c
9. }+ i7 R) h5 y, N0 |
以下哪个选项填入第6行后,不能输出true:
! q2 x; w( x6 n: i2 ~: H; HA. x==z
) S$ V( `2 U8 X! oB. x==y9 n# E7 U8 [7 d; e, D; x) l' i* {
C. y.equals(z)0 D9 s9 H. L8 [
D. x.equals(y), C( p- M+ C, A( X2 f* P, R" m- G8 A0 Z
6. 读程序(双选):- [5 B6 q4 }$ P; b, p5 B' u
1. public class Declare {
1 g$ k# J/ |0 Z' W4 M/ c/ \* s g2.
. N6 e, H) [! U: ~9 f3. public static void main(String [] args) {
/ h! R# s8 w* G4.
( J5 [ N/ A# }$ v5. System.out.println("The variable is " + x);
2 ]% ?8 u( D S" p# Q9 x. e- k6. }# K+ g+ `8 p5 h: Z/ N
7. }; c6 r% I* r/ v- k2 `* R+ y
以下哪个选项中的代码可以使程序编译通过9 Q# k3 k1 Y0 e) Y z
A. 把“int x;”放在程序的第2行4 Z$ ]3 Y- N2 M- \7 ]
B. 把“int x;”放在程序的第4行! f8 y8 `, _$ H. Y) j
C. 把“int x=5;”放在程序的第2行
4 I! z, @& B8 R; `6 ~" TD. 把“int x=5;”放在程序的第4行
) c& B6 f) V) i) j+ H' GE. 把“static int x;”放在程序的第2行- Z2 u" B( | ]8 R2 k6 v2 @3 m
0 v: |/ q" q+ F; s
4 U- A q1 b- v# |, i1 l
) c$ {$ c) i/ g# A+ ^, i5 b
7. 读程序(双选):4 D* Q1 s8 o9 L3 ]! b# `
1. public class Child extends Parent{
, z) [0 C2 v4 u8 B. O0 K# `2. int a, b, c;
$ W, b+ K& x* W3 s8 L. F# o# Z3. public Child(int x, int y) {" v8 V @& F; i" Y) Z
4. a = x; b = y;
# ]) T }8 V) f- p/ [5. }9 }" X/ b: _+ m/ d& M1 C
6. public Child(int x) {' `6 f3 Z" @# O3 z, U
7. super(x);" J* J- o( ], i( z2 o
8. }
! o: T7 _. K9 C. C+ y9. }# ?; w* ~4 a4 n: L
为了能使该类编译通过,下面那个构建器必须出现在父类中:
" ^ i. x' e$ t7 e }: h8 cA. public Parent(int m,int n)
: i8 Y$ A% M" c$ f% B: y" kB.public Parent(int m)
1 [" K; z3 l' o5 \C.public Parent(). W1 i6 Y# {. P V) n( x
D. public Parent(int m,int n, int o)
0 ^. w0 e4 h& Z8. 读程序:1 @( t9 m/ t& D8 R6 ^
1. public class Derive extends Base{4 x- [7 v/ [% j, p2 Y' j9 X6 q7 b4 O
2. public static void main(String argv[]){6 \* n/ F3 s' D6 m$ j+ P
3. Derive a = new Derive();
2 ?# [8 O- R( ^" ^8 |" Y# O4. a.method2();* n2 k" R$ T; _, K K: V; b* P
5. }- i: [- K! h% `4 ]8 i" Y
6. public void method1(){0 I5 e4 B T9 n- Y
7. System.out.println("method 1");
1 c0 F- u! G( r8. } / [9 v4 N: Y& i+ I
9. public void method2(){$ v! a# i+ V6 e4 h3 U
10. method1(); 7 X7 l$ p% g4 l; `
11. }
% ]( m; O( j7 @2 {: \8 C3 ]3 i12. }$ M9 p+ R A" Q: b! F
13. abstract class Base{
9 m$ V! _3 `' v9 N, [; |' k0 m8 l14. abstract public void method1();
& Q; I) ]2 r J15. public void method3(){2 u* k- Y4 z! u- z
16. System.out.println("method 3");
. z) ^7 x2 U |7 _, h( t17. }
/ W' [$ H1 p! s( x+ L3 f% n4 ~}( a* l1 o: W. l
当编译和运行该类时,将出现什么:
% i% @" | I3 U) BA. 程序输出“method 1”. L6 V5 Y, |+ a, `* K; F2 W% }4 j" d
B. 程序输出“method 3”
( d1 s A: { V/ J% n$ X) v5 nC. 程序因为第15行而出现编译错误
0 |/ z* ?, n6 `+ l7 Z3 T7 kD. 程序因为第15行而出现运行错误
+ L: Q* A" M; @0 Q+ g- T ]5 v9. 以下哪个接口的定义是合法的:0 f7 E/ Z3 z( F3 S2 U C
A. public interface A {int a();}
7 s) `4 g$ q( k/ L6 S1 `B. public interface B implements A {}3 d$ e5 C; ?+ W. d+ N
C. interface C {int a;}+ V% D! F; B( b/ K: `, ]% E
D. private interface D {}
1 Y9 f: N/ a5 e' g* L8 G10. 读程序: r& O* \) L7 M
1. //File SuperClass.java1 m. ]0 p: N2 ~+ r1 N
2. package MyPackage;8 ?5 B% K! @# {, d0 W; [8 R
3. class SuperClass{9 {! G6 @/ C/ p# }( L" k' U/ F
4. void myMethod(){
: E5 B( J+ q* t5 s8 {5. System.out.println("SuperClass");: h, k `+ s1 {! o
6. }
* |' M+ Z3 v# r: X _9 A9 Y7. } J! A1 ^6 E& G- ^, W5 j) u! |" R
8. //File SubClass.java
( Y) P' ~: b4 v8 k: u j8 I- F9. public class SubClass extends SuperClass{
# ~, K( f" E3 j% k1 c0 @10. public static void main(String[] args) {& k2 s" T0 {0 J! Q& w, [
11. myMethod();
3 _$ }1 Q. v" n" E$ I. F0 u! B12. }
9 h! z7 H1 b( K13. }
/ l( V" z/ r8 k. ?0 t当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:' H/ s/ G( t# I9 \7 V" K$ F
A. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”+ [. x! j# `5 p. {
B. 两个类都回出现编译时错误
3 k7 x D( M3 V8 m# [) l# `4 T7 ]C. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误$ t0 P$ w; A7 |& B
D. SuperClass.java可以编译通过,但SunClass会出现一个编译错误+ Y0 J: R" {+ g( \7 V' H- X
11. 读程序:+ g1 X9 m/ n+ h( T( D" }- s
1. public class Test{
" C; A/ C, b% f7 K! y2. public static void main(String args[]){
1 m7 o( w8 _, Z) ^3. int[] i = new int[5];
2 m( W) l7 Z6 x; M4. System.out.println(i[5]);0 u( L5 F$ n3 [
5. }
6 i% z7 J) l6 L) [5 x8 p: E6. }8 F t2 D& w4 a: V
当编译和运行该类时,会:, h) |! ~8 Y! l. J k2 y% `5 s. T3 M
A. 出现编译时错误5 B m; Q. s! [8 q( b" U2 n$ Z' L4 f
B. 出现运行时错误
9 R* c0 g$ y+ F7 w% t. Q( D# V+ _C. 输出0
: k1 T! |, W( F6 vD. 输出null$ E: a" }* x1 e6 k# z
12. 以下哪个选项是正确的数组声明或初始化:
7 Y& I8 ~3 b' P. T3 p) ]/ VA. Array sizes = new Array(4);
6 T* H I( D; ` Y& M z6 MB. double [] sizes = {1, 2, 3, 4};
1 h9 X- P, Z9 b; QC. int [4] sizes;
. J( ?. O6 ~' L; fD. long sizes [] = new [4] long;
/ K% J- O; U/ Y# h- w13. 读程序: u* |9 B6 `6 N& s3 y% B
1. public class Initialize{
& b% t; H1 O+ b' c- L5 P2. int x = 200;
8 y: I! Y/ M; Q+ T: W+ M2 g3. static {
5 {+ ?4 z" ~- ?+ V4 B0 Y* ^6 S. J0 a4. int x = 100;
1 k' E- V5 x4 X) p5. }& C+ z/ m: `3 X# o. I2 O
6. public static void main(String[] args){# h8 h8 h5 s5 b* y+ u' S
7. Initialize init = new Initialize();( t# n) r) }% @; a/ `: E8 `$ i1 m
8. System.out.println(init.x);2 _( _& l7 n0 W+ a
9. }
6 V( m) `0 e7 _9 b Z10. }- d; h2 Z3 I# M9 T" l
当编译和运行该类时:
( B: a3 P' }) ^, x6 ]9 R- p5 `A. 程序因为第3行产生一个编译错误. c1 X3 g7 A4 L+ J5 k
B. 程序因为第4行产生一个运行时错误. w6 |: u, Q5 o7 c" j# a
C. 程序输出100
6 q/ T, U9 W* {1 }0 \8 CD. 程序输出2002 e6 H; D, Y0 O& ?6 d6 t% k! J# {
14. 读程序:3 u" {7 H; d9 B8 c; I8 z9 F- `( S
1. public int abcd( char x) {$ e; \# |2 t2 g* {3 R, t+ D, S
2. if ( x <= 'M' ) {
! i* }' B; O8 \3 A! t3. if ( x == 'D') 8 f+ ]1 H, ^! Q
4. return 2;) W2 v9 Z! H% M3 R# g9 w$ p' S; D
5. return 1;
' b) ]- W* l* X+ Y3 \6. }
* n7 i* y% G# d; p4 V7. else if( x == 'S') return 3; W0 V/ w+ x$ p/ J+ W" s; H2 J) {$ T
8. else if( x == 'U') return 4;
0 G _+ B5 r" Y1 Y! m9. return 0;$ g0 N. X, r% b- t2 y) z
10. }6 ]5 u# ~$ T7 ~
在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回13 \3 N }' U& y8 q
A.‘X’1 v1 j3 z' o K% t. m* H
B. ‘A’
; J5 W5 B! B+ X! t: s' @C.‘D’& X- C) ^' D7 b) I `; K" s. k& Z6 x
D.‘Z’
3 R5 l9 q# \) E- ?4 H, v* V15. 读程序:( F8 [; C7 D# N
1. class TryTest {
4 f5 j: g4 [) i/ r" Y2. public static void main(String[] args) {# h& W' p4 K, J
3. try{0 P/ f7 ^- Y) ]( W% e/ M9 P& A
4. System.exit(0);
1 T7 N& k7 v( W( { g8 _- Y+ o, z5. }
) z( }: Z) ]. {6 i6. finally {% R. u( Y1 M% ?3 L
7. System.out.println("Finally");9 J' X( C q+ g
8. }% P) M. _7 ~# x* w
9. }$ d# e0 s5 K. W4 d
10. }* @5 C4 L* s, G* `' T5 O' S) r
当编译和运行该类时,将:5 y; M* x& T6 Y; f% C" K B/ d
A.程序因为第4行出现编译错误
/ E# ]9 \3 P$ \! S8 HB.程序因为第4行出现运行时错误: X- V* b; L) E& d. z
C.程序因为第6行出现编译错误. O/ N7 y6 O$ @& x
D.程序因为第6行出现运行时错误
$ C% k" | x6 p f2 p" X. X9 RE.程序输出“Finally”& Z0 c8 Q' s- b9 h9 T
F.程序可以运行,但什么也不打印 V% s4 k( D2 E' b2 }
_7 K$ v4 m6 G; s' v
16. Given the following variable declarations:
6 i7 ]3 I d. R" [3 R, JBoolean b1 = new Boolean(true);3 Z* m& w$ e1 G4 }
Boolean b2 = new Boolean(true);0 {; J$ X. [* _( c; L
Which expression is a legal Java expression that returns true?
' T' y, p; u8 d/ R/ \+ U, ^( M' E# PA. b1==b2
. V( T# z3 ?: r/ S& ]0 X; j0 I- MB. b1.equals(b2), u$ ]! d1 E! ^1 B, ~/ d
C. b1&&b2$ s! ~& [7 v$ d: s
D. b1||b2& G( H( j1 E" ~1 K' B
E. b1&b2
" ^0 T6 X' s7 o) A6 Y! @: iF. b1|b2
" P# |. `% J6 r$ t* a17. Given:
0 w9 g( w5 b. n* @6 S" s8 \1. public class Employee {) G$ q$ @. _$ g2 O$ L
2.
- p# `. g7 Y- O% P+ e3. }
+ P9 |$ ?4 T* x. E5 Y8 G: ^7 F) A4.
% m2 ~6 B' g' Y: _5. class Manager extends Employee{
! C1 L3 G+ `+ j! X9 |% F6. }0 Q% m! n5 h* |# {/ G
What is the relationship between Employee and the Manager?; I. h6 F) \7 [# I$ g p
A. has a
3 q0 L% h" m, M: [" mB. is a
* C% g" F/ S5 ~: `+ s4 @C. both has a and is a
0 C' Q9 n D% l; ~D. neither has a nor is a/ G% y* H! i$ |' z, C4 U7 a" m
18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?
' O) v" W2 a1 e2 v1 |- ^4 dA. BorderLayout
/ F. i' k/ u( p. R3 aB. FlowLayout5 G& Y* _2 E0 v0 B- c% g
C. CardLayout+ s6 e( g7 R4 m& J' D
D. GridLayout% E& |: W: d5 H; {, l
19. Which of the following AWT components can generate an ActionEvent?
/ A, v0 n. Q' i" T. d' [A. java.awt.Button
8 w1 X, R6 T. i& AB. java.awt.Panel0 Q4 x' X- r4 C9 }. W
C. java.awt.Canvas* c+ C5 D$ r5 ^+ K2 t$ Q, i0 P
D. java.awt.Scrollbar
2 I5 U, V# D" U# a, F# Q20. What kind of Stream is the System.out object?' ~$ p# D& G* R' C$ T
A. java.io.PrintStream
+ `1 K" u C/ M& T) |# DB. java.io.TerminalStream
( L z( H" o: ` T, t+ KC. java.io.FileWriter
4 ]" `! B; G0 g" | r. nD. java.io.FileWriter
& E& N. d4 S) j7 f4 l, G21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选) D5 I# W# m0 B! O; a
A. java.awt.event.FocusEvent5 n C2 O* J% f, i* T/ N- o
B. java.awt.event.KeyEvent/ B# G9 t: j; d' i6 M8 p7 `" U
C. java.awt.event.ItemEvent
; L8 ?' c; ^' Q8 w5 ?1 y6 M `7 ~# ZD. java.awt.event.ActionEvent
: F1 t% c) J# B: [7 |22. Which of the following methods are static methods of the Thread class?(双选)
O; N4 I: `8 M5 j" {A. sleep(ling time)
w% I4 N! u2 q: f7 pB. yield()
: _- C: [: k- t" PC. wait()1 e8 p/ ~/ |6 M& P: S
D. notify()' X+ I% c: `$ }" |1 B2 W. L
23. Which method do you have to define if you implement the Runnable interface?
2 ?: a5 E, c5 @" T- Q' T/ r8 e( kA. run()
7 O% b: _6 \" D s* wB. runnable(); i! [* k& B' B; L7 v% l! n
C. start()
/ g' e, m8 a8 W& X& e' z5 d2 }D. init()
: W3 C9 v1 S- T2 K7 L$ w1 jE. main()
$ t+ X. N* |6 o7 K" R3 a$ B ] W24. Given:( x! i! W8 T/ k- U- s2 t' P. {
1. class MyThread extends Thread {, `9 O5 {. h3 l/ B
2. " f) o+ z! r6 Q5 v$ N5 S' q
3. public static void main(String [] args) {
$ J) J; H) b& g* W4 D- ~/ V4. MyThread t = new MyThread();
& }+ y1 K# g. m5. t.run();
0 _1 |. y; m* e3 {2 \6. }3 @8 _% ~1 y4 c6 q; d7 u- g
7. - Z. K) m0 p. r8 i7 b
8. public void run() {
+ ?; q4 s# ~. P- E7 V0 w$ ~9. for(int i=1 ; i<3 ; ++i) {( S! h' W0 ^ s. l2 E# V- t. A+ q" |
10. System.out.print(i + "..");1 e# @2 L$ x- ^5 h( ]
11. }
0 U7 d" k& {* m12. }" c e& c& Q- O
13. }$ S* Q- [/ J6 Z1 }; V0 w: N
What is the result of this code?C; Y8 |: }; ~; D; V0 |; c
A. This code will not compile due to line 4; V1 a0 K* U. {, _. F
B. This code will not compile due to line 5% _$ D% t" n0 C( B6 S
C. 1..2..
+ B% E. g4 @2 p% ^, G/ E7 ?) H9 lD. 1..2..3..4 G; c% G3 M+ x- I9 W* f) @% s' C
25. Given a local inner class defined inside a method code block, which of the following statements is correct?
$ b) x0 S% @/ F( |8 m: @7 gA. Any variables declared locally within the method can be accessed by the inner class
* ?0 ?. W; e6 B# V* L8 @B. Only static variables in the enclosing class can be accessed by the inner class
2 R/ X T% C( |, \0 ]6 F; y* JC. Only local variables declared final can be accessed by the inner class
" [ K( v, q9 z& ~+ Z. cD. Only static variables declared locally can be accessed by the inner class.$ b k6 T4 r# ^/ v" e, a
$ u" ^6 u7 r' J/ G& Z
9 U6 v, j0 H# o* `7 c, a4 _7 S- p: W3 n( [( }/ O: G! s4 K, [( v
26.public class SychTest{
6 o7 D6 f, R$ E3 G private int x;
7 r. L# d" S$ `! h3 i- q private int y; & ^. G* A2 K' d' T
public void setX(int i){ x=i;}
6 I+ |3 ~# t# A+ R* {* s7 W public void setY(int i){y=i;} 0 C! ~6 V, x! i0 D, A
public Synchronized void setXY(int i){ % |/ Y }& @5 C, B& v7 F& E/ p
setX(i);
! q, s# Z% s5 g0 m" H setY(i); ! o5 C* c q: N, G H4 Q
} ! ?4 N, V- }# m' ~8 F
public Synchronized boolean check(){
& W9 M6 ^4 E( N3 Q4 u3 V return x!=y;
$ \& s. J4 D3 O! H+ e$ `0 S1 b }
8 D1 Y8 u$ D) ~, W5 n" M } 5 L5 g# {- h5 O9 O5 h
Under which conditions will check() return true when called from a different class? ) S1 s& v4 Z- j- w/ L/ n- }
A.check() can never return true.
4 ^. Y! f$ A4 C4 c B.check() can return true when setXY is callled by multiple threads.
: x% \: H& A0 B$ E0 z" s C.check() can return true when multiple threads call setX and setY separately. 1 c, L4 ^1 r: N
D.check() can only return true if SychTest is changed allow x and y to be set separately. ' v: `& j' S( n" B( |7 M( X3 c' Z
6 c3 B* G" w3 y4 ? P
27. 1)public class X implements Runnable{
$ z* f7 I: `2 I0 N, G 2)private int x;
4 ]6 S; D K" j$ h. s9 m: n 3)private int y;
3 C( D \" {0 H' H6 A4 g 4)public static void main(String[] args){
* k6 O6 M! r- `5 c# T# U/ v5 L 5) X that =new X();
+ ]( c6 F, K3 z. a5 y 6) (new Thread(that)).start();
7 f8 W1 k" A5 Y6 j. j 7) (new Thread(that)).start();
# }" E" M0 q" G% Q; \, |5 V }
/ W0 Q$ H& ?# s! w 9) public synchronized void run(){ 1 o& i2 Z0 L/ e0 i2 C
10) for(;;){ . O7 m3 }( T3 M! n7 `& O$ X
11) x++;
5 Q, T4 z( V7 F$ ~1 a* G) w- t3 P 12) Y++;
; F7 w6 @9 `' D 13) System.out.println("x="+x+",y="+y);
; m9 H& C6 S A/ q6 Y 14) }
0 e5 k$ C) L9 x p! {& A 15) }
" v2 e+ P3 {% \/ m 16) }
$ L% g5 V/ t8 N* k4 s* i2 `6 C what is the result?
; s- I! R) m- ]3 S" u2 i: Y A.compile error at line 6 f0 u6 f' A; x- i
B.the program prints pairs of values for x and y that are
( V8 O+ W: g/ Q8 {" i always the same on the same time
. ], i% Y7 R" F9 |
. Z$ Z5 a9 e1 A" O- n2 c28.class A implements Runnable{
* R. p' i$ g) ` int i; , O" Q7 n" {7 a4 F9 H. `
public void run(){ 9 J' j* b5 G# i5 G
try{
& T. a' p/ S. o7 P1 j4 ` p Thread.sleep(5000);
: C2 B# [' ]6 C* S4 Y# i i=10;
5 t d4 N! ~' Z$ J }catch(InterruptException e){} $ z- G w* K( B9 C0 E3 Y; s. d
} . v' [ J8 K& d: N
}
: E! d2 A+ S( E" P8 z9 M: u public static void main(String[] args){
$ T, N, `2 s9 U) Y6 D p try{
& L2 W# s% A* m7 d. y A a=new A();
* y$ d4 Z0 V9 z# R& y Thread t=new Thread(a); ; m8 d8 I$ K4 O% R
t.start(); ; x. h2 p; M8 E% h
17) / Z) |7 a2 f& o% h' i% Y
int j=a.i;
, Y8 j4 m4 P+ _: d 19) ( h [1 A, n- s. C& C2 J( v+ N
}catch(Exception e){} U' H0 C* x" G' i; M
} O% N2 p1 u' [; O+ x+ V6 W
} / |0 P% `4 o) N
what be added at line line 17, ensure j=10 at line 19? , R! q! }3 B4 J0 @2 K- B
A. a.wait(); B. t.wait(); C. t.join(); D.t.yield(); : F& K$ J! ?; ] d3 @0 c1 j$ {$ Q
E.t.notify(); F. a.notify(); G.t.interrupt(); 5 t- }6 B+ ]# I; ]- |" X1 _
29.1)public class X{
# M% R8 G3 r/ T; f# i, P. E$ x( W3 @ 2) public static void main(String[] args){ . e; L z2 h+ N& Q
3) String foo="ABCDE";
% k- a. n2 r, V5 ?- ?& B$ m: S 4) foo.substring(3);
! P; b6 Y( H9 n# q/ P 5) foo.concat("XYZ");
( A- C. ?- s( u! w/ [# k% } 6) }
1 c5 W+ X# s) {; D5 O( D7 V 7) }
. Q! k) ?, K! l$ f0 y% l6 P) r what is the value of foo at line 6?
& K8 c& Q, N8 m6 v$ M3 v30. public class Test{
9 Z7 U5 x- n& R public static void main(String[] args){
! o. l8 ?& z3 x& h" e) r StringBuffer a=new StringBuffer("A");
7 @) t) ?& `6 n I) V StringBuffer b=new StringBuffer("B"); * r) w1 G5 P3 N1 f
operate(a,b); / G$ P) a( \% U
System.out.pintln(a+","+b); + B# `3 t; K2 u
}
% ]2 _3 L- Q6 L1 A9 M, P public static void operate(StringBuffer x, StringBuffer y){ % |" c3 x) g+ o, x, S% T
x.append(y);
& K) y4 N+ s7 e+ Q/ m+ c8 [ y=x;
) F! o1 y: h4 f5 a; o( ^ } ( H+ s, q+ x% e& `+ ]
}
5 w4 O4 S& M1 f8 n: x9 r what is the output? |