Sample TextJava培训考题
v" ~, ?2 k/ `* n1. 给出以下程序:
7 F+ s- [* V. P) K+ E1. public class Colors {" \, M, X; T- d+ f1 f
2. public static void main(String args[]) {
. |' j' _# d1 {4 @. ^3. int n = 1;/ b( T7 c( L5 O; }" Y
4. System.out.println("The Color is " + args[n]);' e j8 k4 }8 @, A
5. }
% e n* i5 u( i% U6. }
; n2 |5 H. f7 Z假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”+ s5 H5 T7 R$ W! z: l4 c/ `$ h: O
A. Colors red green blue yellow
{3 `0 k4 Q* w) y" C: _B. java Colors blue green red yellow
3 I& T" q$ M% X5 ^$ z) JC. java Colors green blue red yellow
* r4 T$ `8 x, u; I) jD. java Colors.class blue green red yellow
0 c7 z5 a9 s4 q4 q8 \E. java Colors.class green blue red yellow- S# J4 Q$ R$ w# U- ?1 \. l
2. 读程序:/ U B. m) \5 D; o( C4 \
1. public class Test{
I; f7 n9 C- [2 v3 d& T0 D2. public static void main(String args[]){
9 J! S, \' i& `/ ~ V3. char c = '\u0009';) s. z( i- O7 i- _: J! v
4. if( c == 0x0009L)
, O% K& [8 q7 g0 N8 Q9 Z5. System.out.println("Equal");
9 v; M( I. p# K0 P% K9 U& w6. else System.out.println("Not Equal");- M( J$ ~: {, V: r9 @' g
7. }8 u+ G8 t2 C- q) ]: L$ G
8. }8 E7 m5 R' f7 O$ ?" O+ Y/ o) _9 w* k
当编译和运行该类时,将会发生什么:
# X4 T1 O' b/ T: P* H: x/ eA. 程序因为第4行将出现一个编译错误
% o/ d# D! k/ d2 F# G4 EB. 程序因为第4行将出现一个运行错误- l* E1 l8 B. \8 n% q3 o1 t
C. 程序因为第3行将出现一个编译错, U4 q" Q; q; y2 u9 A' R
D. 程序输出”Equal"# |0 e( O5 u+ ]8 o0 b4 }" U7 Q
E. 程序输出"Not Equal”5 m( A/ G; s4 f: s l
3. 读程序:5 _& ~3 J- R5 P7 v. n5 s$ o9 i8 a' u
1. public class StrEq{3 q: X9 @% v' y5 B
2. private StrEq(){
) g5 ?; L4 H" e6 G8 [" y) ~3. String s = "Bob";, J5 Z: g+ R J: s8 H# W2 R% ~/ m
4. String s2 = new String("Bob");
" _0 a% n3 O3 S5. if(s == s2){
8 [) S9 T9 b* _; A; a& r6. System.out.println("Equal");, w1 x# l. J I D0 J; W
7. }) E, `6 x+ J: l% J+ K1 V+ m
8. else{7 s- O |! a1 t# T
9. System.out.println("Not equal");" @: c; p, _* }; {8 f, d; t* V- S
10. }( u; Z" C9 D/ n: v
11. }
4 X/ u* `" @7 g2 S2 `12. public static void main(String args[]){
! |9 s. r, W: B9 }& v3 N7 R0 B13. StrEq s = new StrEq();
4 `) r3 p' i( z0 b0 r. W14. }! B# Z( d) Q0 b: z
15. }! D6 j: F: C V. f5 W7 M; e3 w. [
当编译和运行该类时,将会发生什么:
n4 e: C+ V8 H T" pA. 程序能够编译,并且输出“Equal”
7 m- \! p H1 L6 J$ h! A: gB. 程序能够编译,并且输出“Not equal”
# f& `- q! `% I) X$ |C. 程序因为第2行导致一个运行时错误
- ? f6 O5 L+ ?; v% F! y! WD. 程序因为第2行导致一个编译错误
! P7 z2 D4 V* S2 q0 s8 D4. 以下那个赋值是非法的:: @ n( }; G c1 _9 J2 s
A. long test = 012;- z7 f* W+ h1 J' h8 a$ D
B. float b = -412;
8 e( `) m# ~ iC. int other = (int )true;
6 f7 m6 y* s+ N! PD. double d = 0x12345678;1 p" C( P) y7 G$ F3 J
E. short s = 10;
+ z1 e8 u8 y) w7 e9 n5. 读程序:3 c5 `, n8 E. o" X) F, |0 ]
1. class CompareString{. b7 b9 Q( U- }; C! L
2. public static void main(String[] args) {
0 w- M2 D" Z T3. Integer x = new Integer(5);
+ M3 |% I+ p8 ]9 T5 }4. Integer y = new Integer(5);
7 i8 B! q5 P) ~# J* i$ k5. Integer z = x;3 ?/ k) B# Y. [, M0 `$ _
6. if ( ______ )
3 J: ?: u n/ G3 G1 ]# s7. System.out.println("true");0 X# ]! @: }' T6 @( p
8. }
' C, n. z2 ~/ T0 T& T: m) M9. }
4 i! a. ^" i0 }4 z. G以下哪个选项填入第6行后,不能输出true:) K6 n x. P2 g4 t$ F# ]/ k
A. x==z
# X5 Q& H$ p% Y/ JB. x==y- S2 E* ~. b3 i- ?% G8 K) P- O
C. y.equals(z)7 J" U4 Y. \( n' J" c) H* d. H' a
D. x.equals(y), u& X0 y: R7 a, T9 }, z+ ?: `5 u
6. 读程序(双选):# Z0 x( F7 P) G" e1 d
1. public class Declare {
2 i8 h; R0 j$ Z( |; \2.
' A- a) }9 M; n" R9 d3 K6 ]2 F3. public static void main(String [] args) {
+ j" f$ L* }7 p# Q6 e# s& ]4.
8 Q, D# f# g7 R# e7 ]5 n4 U6 ~; A5. System.out.println("The variable is " + x);$ i# o0 m( b% K2 u
6. }. {% ^" f$ P/ I, g1 w g( C& A
7. }$ B% g: O* r* v. k
以下哪个选项中的代码可以使程序编译通过! ~1 V% {2 @9 Y/ N) D* I" D
A. 把“int x;”放在程序的第2行% T! l* \! T3 _# o7 V3 J' l
B. 把“int x;”放在程序的第4行
# q, t* l1 h9 W- i; D; T' TC. 把“int x=5;”放在程序的第2行
* \' b% B3 V% q5 C. gD. 把“int x=5;”放在程序的第4行: h& K' }+ H, f! J
E. 把“static int x;”放在程序的第2行
! i- e! e( b( f0 v: q+ Z7 n6 z: t2 X/ E+ q5 Z
! l; d! \6 N- l1 Y8 U
# F7 d" m# g; b* e4 F8 @7. 读程序(双选):
" V. R+ F9 P! S s. A1. public class Child extends Parent{6 N3 [0 H% N6 L0 S; Z3 @: t3 b
2. int a, b, c;
) w, k& H4 a1 | H+ N* r8 E3. public Child(int x, int y) {
' v' z1 R" S' Q; B4 Z- x1 W4. a = x; b = y;
+ |; [) v6 l. \5. }' j' X. \3 _" [! Z% N1 q* W' r
6. public Child(int x) {3 S' f- J, h3 S+ O
7. super(x);$ x+ i' ]% G2 T/ ?! a" S6 l+ a
8. }
. `$ d% W0 j8 W; z) p# f9. }
2 r: k# b1 L1 B& H9 ^为了能使该类编译通过,下面那个构建器必须出现在父类中:
0 b* w/ [$ h$ U- z: ]$ N8 zA. public Parent(int m,int n)4 x2 L0 U# f. b, e
B.public Parent(int m)" x: X. L. A2 j0 ^. g
C.public Parent()
+ f' s* C5 U% ED. public Parent(int m,int n, int o)/ Z" k* g! i% `) M+ E7 m$ J
8. 读程序:
! Z! p4 w* ?5 d, {1. public class Derive extends Base{
- W0 L' d$ s F j2 F8 k% A3 T% r2. public static void main(String argv[]){9 i% {- a i2 W! M7 s
3. Derive a = new Derive();2 d& z2 e7 \% a) o6 T) L/ `' a
4. a.method2();: m2 |3 \. v7 O+ d: s. u0 g8 J$ ?
5. }
! Y x- e) U i( i6. public void method1(){4 y" r7 l( L: O6 Z) e9 D# F% y. v/ H
7. System.out.println("method 1");
9 }4 l( U( W2 i, K5 r/ C" X9 C) h8. } 7 P# e S4 H" ?
9. public void method2(){5 _; `2 W0 G& a+ V. C
10. method1(); - x% K6 N" d2 }+ S# s9 ]7 r
11. }! s! V- z6 ~- V! m! U
12. }
" j. a- y5 b% h, Y, I: n13. abstract class Base{0 j7 S( `8 b2 Q( d, _
14. abstract public void method1();
+ r1 f5 P8 \0 c/ u# y15. public void method3(){
* k* p- W3 R8 ?% d16. System.out.println("method 3");9 }" Q/ C. s9 s- y
17. }
( z6 p2 `4 O) G1 b% X0 ?4 D( n0 g5 N* ^}6 w; S: r( P Y6 k9 S6 ?
当编译和运行该类时,将出现什么:7 D! l4 S, B. B4 o @: r5 L
A. 程序输出“method 1”
7 v% }. N! s* b5 D6 bB. 程序输出“method 3”
, n- b# f" w, f" Q: _C. 程序因为第15行而出现编译错误* Y5 v4 n$ k# B$ P8 n6 A& a2 D
D. 程序因为第15行而出现运行错误1 G2 p( W6 K% S4 f: c' l
9. 以下哪个接口的定义是合法的:: ]/ X, r2 z. g
A. public interface A {int a();}% U, w' d$ z6 Y# e8 a. [4 B" \
B. public interface B implements A {}
( } X; v5 r# H" T9 ]( DC. interface C {int a;}
% f5 o: G6 J! h# k; B, ^# ]* QD. private interface D {}2 I" e0 d% R% V" Y6 J5 {8 L
10. 读程序:
) n' r* S' @. V1. //File SuperClass.java% O/ A4 u S3 ?
2. package MyPackage;# }7 I- w. F3 Y3 [
3. class SuperClass{. a6 B: L R, @8 ~/ a3 _* \9 Z6 t
4. void myMethod(){
3 P3 ~( w- {) p* K+ v5. System.out.println("SuperClass");
0 u2 d$ y3 b6 p: C. A6. }
: D( q; k/ h4 b% L$ {( A; e9 |" }7. }
& B1 M! O0 W' \7 n8. //File SubClass.java4 w, P3 Y8 w1 j! U
9. public class SubClass extends SuperClass{1 Q) j* M8 t: c% D" [+ i& ]7 T
10. public static void main(String[] args) {
9 I) @9 h; G4 L4 ^& T* ]6 F11. myMethod();
% Z3 R. }% v4 T0 Y0 Q0 V" r; V12. }% o* S2 g: f1 ^' A. I9 ~9 [. y
13. }
- y9 a+ p9 x6 Q) }; V4 p2 ^6 q5 s* N5 h当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:
% N' {3 Y3 u4 N3 QA. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”
4 z: F$ F( \ m1 f- T HB. 两个类都回出现编译时错误
- J1 k2 U; l' \. |0 ~- e. nC. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误
$ V0 r. V7 L. `) J6 k& zD. SuperClass.java可以编译通过,但SunClass会出现一个编译错误/ n H, Z( N( X0 Q: p* b L# i
11. 读程序:, y% }- b# [+ I8 q! i7 e
1. public class Test{
9 n! R3 ^. ]- W2 H" c3 q2 B2. public static void main(String args[]){) x ~4 H' N( o) K# M' G7 H5 W
3. int[] i = new int[5];
. F- b3 z9 Y. X4. System.out.println(i[5]);
I6 A$ Q" p/ m* R( o5. }" V$ \( u0 R( h+ t( @
6. }9 d9 |$ a/ A1 n2 A" }! {
当编译和运行该类时,会:: Y# i8 q7 p9 i2 \+ z: Z
A. 出现编译时错误2 s( t% A" S0 X$ L
B. 出现运行时错误5 h# |6 J! D# z9 e5 X( C. i" c2 F
C. 输出01 V* y7 H/ `( j/ S0 `9 k
D. 输出null
S5 Z: s; _* E6 O12. 以下哪个选项是正确的数组声明或初始化:
+ w( D+ Z3 Y7 L! ?A. Array sizes = new Array(4);
, b& p+ W' ~/ {+ ]B. double [] sizes = {1, 2, 3, 4};
& m+ `6 A7 b i) k+ ?; _C. int [4] sizes;! y2 p7 G- m7 J( M, k- e$ [
D. long sizes [] = new [4] long;
3 \# Q# q! H: ?% p) U6 `, i1 f13. 读程序:
2 M* C6 N9 r( X4 m$ l3 z1. public class Initialize{8 f/ G( l! D% q8 j- o6 | w( R
2. int x = 200;
6 g3 F: S/ ~4 p, C4 V8 S3 a7 H0 D3. static {7 n3 q$ o6 [' I& e' O8 ^& S
4. int x = 100;
9 b/ f ]% l: o1 @3 u6 t, b5. }$ q+ p+ R2 {1 N+ z4 @' A
6. public static void main(String[] args){
4 x5 ^ K. s* a) o5 t6 O7. Initialize init = new Initialize();
3 k9 y0 i" H% b8. System.out.println(init.x);$ I" d9 g' l* C
9. }
, L+ Y% x: K/ v% P2 t) v$ }# a- j5 [. l10. }2 f) a: y, J* s' q
当编译和运行该类时:- }9 d4 X# Y8 R8 I
A. 程序因为第3行产生一个编译错误
9 [9 x! C5 m/ ~7 H1 v7 i4 \, |B. 程序因为第4行产生一个运行时错误" L, |0 W% e! R
C. 程序输出100
" w; S6 A! k4 ]( s7 b M4 T7 XD. 程序输出200
1 L; S2 ~- t; Y14. 读程序:) P/ _4 z0 {9 w' Y* z
1. public int abcd( char x) {
9 S6 W* P1 \1 K6 V7 ?1 }2. if ( x <= 'M' ) {5 [$ R# {2 V; j! k- t$ i7 P, \9 I# L
3. if ( x == 'D') 0 Q9 H. j! T) f
4. return 2;
$ d! Z2 G/ {1 m" M5. return 1;
1 P! l: u8 G, q' M! O) X& }" O, f* d6. }
; |9 a/ s, K! [8 D7. else if( x == 'S') return 3;- d+ D/ N. I+ [, h2 v
8. else if( x == 'U') return 4;2 o U6 N4 [$ v0 k8 S1 c
9. return 0;# z7 |1 |' i$ D* v& N. [6 R6 X
10. }7 I2 }; n- Z9 t
在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1
; ?# N7 Q! e& B+ ]# e; V2 e2 f, qA.‘X’
6 z' e$ H9 R/ A% ^& ]B. ‘A’( ~ s- n* {* K' Y! Q9 S/ r
C.‘D’
2 O6 T6 t- @; E, P- Q9 L3 XD.‘Z’, r4 K$ q$ o+ |' |$ R% k
15. 读程序:
. I& _4 P' j- O- c9 ]) |8 G; |6 f1. class TryTest {
1 T M4 Q5 a/ m; [- J% R2. public static void main(String[] args) {
2 i( y" \. s4 f, k3. try{
6 j' t; i2 o8 f! J! q4. System.exit(0);
- F% k. w8 ^0 j2 [9 V5. }' }& R- @+ Y5 @8 N7 I8 ]3 _
6. finally {: b/ j+ j1 n9 o3 o* Y
7. System.out.println("Finally");
, P) m% y- J8 W! G8. }
. G& F& W7 \7 s; f1 i9. }
2 K# L4 _8 i- n2 a' t$ L; n- J10. }2 e D4 D! e6 q% i/ H9 p
当编译和运行该类时,将:
, U5 d0 w9 Y8 }" w0 ZA.程序因为第4行出现编译错误
( v; g) z: F" K. j- e) x0 K- V2 `B.程序因为第4行出现运行时错误( f5 d7 @; y- j
C.程序因为第6行出现编译错误
7 q9 B/ r( U/ VD.程序因为第6行出现运行时错误5 d% \) D9 p+ N6 W7 V8 o5 j
E.程序输出“Finally”
) K8 d( a. g! \. z0 `7 {: W! ^* @F.程序可以运行,但什么也不打印
1 L. g1 Q# k* s$ I- h' E% @- i
l: B$ D) B$ `& T, F16. Given the following variable declarations:! G) [3 u+ b) ?$ h' w: p2 W3 _/ f
Boolean b1 = new Boolean(true);
; G& M& E3 D* A" H1 Y2 |, vBoolean b2 = new Boolean(true);8 W; {) _6 x. B4 V% h9 }
Which expression is a legal Java expression that returns true? ; b+ _5 n* q9 L$ P5 e
A. b1==b2
5 H d! S U9 u3 q: s+ I! _B. b1.equals(b2)
5 n+ S4 }1 S3 y2 c& ~- wC. b1&&b2) z, ^9 j7 P& g7 \. N( L- `
D. b1||b2
c1 O% @; @3 m/ O0 [" k; LE. b1&b2( b3 |( P; i( r) o- s0 r
F. b1|b29 ^. C' d3 X/ \" P
17. Given: % m% f5 ?' x/ U: ~1 z6 `+ e" _0 Z9 i
1. public class Employee {
3 t6 f# b( K6 M7 z9 _; [2.
5 S. X/ g+ V( R3 b7 J) w# y0 \3. }' e1 H3 A1 u# e2 C T' [6 K
4. ; ^, q! p3 c& u3 P" W l
5. class Manager extends Employee{
$ H W8 C( V9 P3 k6 c; q- ^6. }! z) `6 f3 C f! P- v0 q# F4 g* r
What is the relationship between Employee and the Manager?
! D% {" W J: W; E) _A. has a$ O) p8 O$ o: y3 ]% |" n
B. is a
1 w/ D/ G! J4 Y, I; {/ xC. both has a and is a" ?8 o* |! s& N+ c
D. neither has a nor is a
) o. G0 _2 W. J) x( H5 z18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?# |1 B5 I8 m. a( e, j
A. BorderLayout
4 d8 \. q% c6 F/ H: A5 f. V4 Q0 MB. FlowLayout
! G6 ?- M% c! ?; c9 }C. CardLayout
0 c- ^ D s% O+ z& F% iD. GridLayout# l$ s1 U* e$ a0 \" @9 _5 j+ L
19. Which of the following AWT components can generate an ActionEvent?! i) q' T3 ^* A4 T9 A- l" w$ {
A. java.awt.Button
/ f# L3 J4 E8 ]$ A) P4 i+ HB. java.awt.Panel& H$ X2 e, w1 u2 z0 h; v/ \
C. java.awt.Canvas
. {5 P- f( v1 x# K: {D. java.awt.Scrollbar! r6 a+ ~% ^1 ^
20. What kind of Stream is the System.out object? [5 C1 C' R+ `8 g* r+ I9 ~
A. java.io.PrintStream
& L* |; B0 P1 n3 [' \7 dB. java.io.TerminalStream& E9 f T! Q8 M7 y6 j) a6 q5 R
C. java.io.FileWriter; _# a/ o; q* I/ ^/ W" P+ N$ ^
D. java.io.FileWriter& D+ d0 g! ^9 _& {. N2 H! Z
21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)
% B2 _+ ?7 g2 k* r7 V2 D, M* lA. java.awt.event.FocusEvent0 ]9 N7 _5 s% x) F
B. java.awt.event.KeyEvent
. g2 {$ G% w' q9 h6 `1 _C. java.awt.event.ItemEvent
& [. `* o; X% C: V+ U$ dD. java.awt.event.ActionEvent. }5 K3 J& u9 P1 E D! L% J
22. Which of the following methods are static methods of the Thread class?(双选)) z" u8 I8 B" b5 p# Z h: d( J
A. sleep(ling time)
: ^7 u! P8 h6 _" V& E, i, aB. yield()
O9 U8 r' y/ \- l- j, nC. wait()/ r, r3 p( d1 o$ E" Q( M2 g
D. notify()
* h4 r6 i& L' I. Y D; K. y23. Which method do you have to define if you implement the Runnable interface?
- x" Z$ H9 O! @! oA. run()
% Y& [' `7 W' h# I7 V2 f. sB. runnable()- u& A+ J* I3 a7 e5 {
C. start(); J% H# ^6 U9 p' {2 Z1 W+ ~2 Z! n
D. init()
( a6 s @5 v; z- i1 DE. main()) e4 o, M: h( d; e% {# x7 o/ ~# D
24. Given:
3 j6 r, N- h/ c m1. class MyThread extends Thread {$ B9 }' F+ P. u; B, c# }& Y
2. 7 R- U) C1 e2 F! g# M3 M, x7 w
3. public static void main(String [] args) {
& V$ O9 @6 p7 c8 j+ N4. MyThread t = new MyThread();' u1 ]9 f' c: ^, u# u7 J4 [
5. t.run();
6 {3 w$ n0 j9 x) f' l: r; D6. }
0 n0 }( p9 |: X7. " X1 L6 g1 ~& j3 T7 [1 K% ^+ g( U
8. public void run() {
; n9 D. Z9 \: h* I9. for(int i=1 ; i<3 ; ++i) {
# w: P( q% V% J9 W L5 |10. System.out.print(i + "..");
) F; r B" L8 J11. }6 M3 R$ l+ y0 v' c- H% ?$ B6 Q9 V
12. }& D' w6 F0 e9 @
13. }
9 b F+ E5 y5 O% p; MWhat is the result of this code?C H3 H6 Z# M X7 v+ ?% m# S! n
A. This code will not compile due to line 4
- c. \( f) t! e+ sB. This code will not compile due to line 5
4 ~$ |: Q+ C2 E/ @, s+ EC. 1..2..
* Q- c; H; j. c( R& A* OD. 1..2..3..0 P4 }5 i3 c, ?3 N6 C
25. Given a local inner class defined inside a method code block, which of the following statements is correct?
% q0 \# K: I. u1 x* P ]/ KA. Any variables declared locally within the method can be accessed by the inner class4 D& s- z& | h
B. Only static variables in the enclosing class can be accessed by the inner class
) b% ?+ L/ }* D3 gC. Only local variables declared final can be accessed by the inner class
7 J% N) u# u& }% z+ i2 lD. Only static variables declared locally can be accessed by the inner class.
/ G; I. S d x$ r8 V9 Y; w8 w' Y* c
( E7 R! o: Q/ ]9 k) _& E1 Q, x0 |" }: {# r3 C$ |; K0 d% ~% B
' J* S8 ?/ V5 a4 Z26.public class SychTest{
9 ~1 F4 H9 I! w3 k, G private int x; * _0 c8 d$ j& I- x* A# l
private int y;
& [5 }* B4 C8 @+ `! g5 q0 h' F public void setX(int i){ x=i;}
. z1 b, T9 @/ D6 p, j' r8 p% P public void setY(int i){y=i;}
# p6 I! W+ t; ^" ]% P1 }0 l S public Synchronized void setXY(int i){ " e0 s! `; ]6 \- F' ^! t4 w" [
setX(i); % r8 o5 Q+ E! y [9 N" |/ i
setY(i); 9 I5 Y+ k! b, ^7 } o) R- Q0 Z! S- F
}
5 X% M I7 b/ @& w' ] public Synchronized boolean check(){ $ H) d# }0 `* S! K% B
return x!=y;
! M1 e) z' ^* T1 [! }; a+ n } * s" |8 D( h% v
} ' v8 v6 U5 {% w
Under which conditions will check() return true when called from a different class? 8 K" Q* y' e( J: c( O1 c z
A.check() can never return true. . B! T- d3 Q8 n* w
B.check() can return true when setXY is callled by multiple threads.
# V5 [/ j6 k' m! s& L C.check() can return true when multiple threads call setX and setY separately.
8 o5 w& V4 i g; h% x% d7 V D.check() can only return true if SychTest is changed allow x and y to be set separately.
) e9 Z# W8 v% L' Z# S; ^1 @% B- Q! `, J5 {9 U4 [
27. 1)public class X implements Runnable{
2 c/ L$ ~. R! A( v8 P 2)private int x; 7 |6 i4 N r$ G) x3 f' w% F" @
3)private int y;
# p4 A6 a6 v$ m! f" U$ | 4)public static void main(String[] args){ ! y6 j4 i4 e& H2 R( p5 O
5) X that =new X(); # J6 P2 A7 ~! Z
6) (new Thread(that)).start(); ) y/ Y3 _/ z0 x+ c; d6 V* j: t" Y9 W
7) (new Thread(that)).start();
+ P, j4 S a+ S- \8 |% C }
7 W4 f0 p0 G5 ]( v3 M' Y 9) public synchronized void run(){
9 {- t9 i. ]# J" \ 10) for(;;){ 8 O+ q8 j8 U! O5 l4 Z
11) x++;
# |$ S3 z$ o/ y+ T- P2 v) L 12) Y++; 2 j+ H" a; ?0 k
13) System.out.println("x="+x+",y="+y); # C4 L. }* A* N* \/ K+ H; k& q2 j
14) }
4 u$ {4 A1 P+ R) x+ w" A, B$ m# { 15) } 6 C8 ]* n5 e% e8 _+ ]
16) } 9 F1 U& P3 M/ J8 n
what is the result? 3 m) g" o' R# T4 K
A.compile error at line 6 3 k1 E( H- Q6 l2 c; F
B.the program prints pairs of values for x and y that are
0 r8 P$ c1 z6 g9 l always the same on the same time ; M" W! O0 s. _# k0 v+ c
1 O% L9 E. w: q8 M
28.class A implements Runnable{
# q) q4 \: ~& h, a int i; 3 h0 {" y8 C9 q. ?
public void run(){
+ }0 B# n+ m6 D. h9 W) O/ v try{ 2 K* L5 _ _5 r) Y% f
Thread.sleep(5000); ; H/ _* l0 {2 J! y' k
i=10;
' H+ u/ n; k7 n. {8 ?: x }catch(InterruptException e){}
7 H4 b9 ^6 T+ @& X } # t l$ R k8 E3 X' ?: t
}
) q1 j* X3 V( W$ F public static void main(String[] args){
9 N5 q5 ~7 X: V K% J) n$ X% e try{ 8 u# ^/ _& v- m1 F
A a=new A();
- N$ U+ T* H! T' T! P- J, u Thread t=new Thread(a); # V7 j# _4 e1 y/ q
t.start(); : X5 A8 K" F" a/ q) Q- N2 h
17) 7 A a! m5 j6 ]$ C5 z+ g, a7 [
int j=a.i; ; Q' b! c+ l. Q5 Y7 Y
19)
/ V7 L6 |9 `/ t$ v: x" j }catch(Exception e){}
, s- w/ `( G! D$ a C }
- q6 B, f, e. u3 A% Y3 K } - Q5 K9 K5 Z: F' I' O) a
what be added at line line 17, ensure j=10 at line 19?
W$ a/ W% g5 s9 K7 z9 B6 ^ A. a.wait(); B. t.wait(); C. t.join(); D.t.yield(); 0 W+ P. p, c1 f3 j4 X9 _% O4 G4 V
E.t.notify(); F. a.notify(); G.t.interrupt(); 4 B4 }) w0 K- [
29.1)public class X{
! o( b) o" U' e) a. @9 f 2) public static void main(String[] args){
8 [3 r. {0 M8 e. q) m 3) String foo="ABCDE";
; j0 Y) ~. B5 a: w( u( n 4) foo.substring(3);
% f6 F B" i5 V( w 5) foo.concat("XYZ"); ' V+ I( ]% c/ f
6) }
& o) |$ p9 T, Y: F/ Y 7) } % W" z/ b- @* s }
what is the value of foo at line 6? 4 ? n: ~7 k. q6 X) }# a
30. public class Test{ & |- L* @9 d" y4 p7 K
public static void main(String[] args){ - v7 v& g3 P+ }8 E2 p0 X$ _
StringBuffer a=new StringBuffer("A");
. G9 |- S, |8 K+ B) M$ m) i9 } StringBuffer b=new StringBuffer("B"); 8 w2 b* ? g6 ~( D$ k
operate(a,b); 6 m# b$ p" g1 S$ n& \
System.out.pintln(a+","+b); & ]$ v* f9 R- B- e. m* }( {
} ! l# a) p( i* |& S& P* Y3 N2 L3 g
public static void operate(StringBuffer x, StringBuffer y){ ; ~* |+ @8 Y+ Z' \6 N) s5 R" k) N0 @' V
x.append(y); + [ r9 I+ J$ P1 x! Z
y=x;
, L- T8 |7 S4 P& | } : Q- O& O) U" W+ T% R
} % s. } ?4 s1 b2 D: y6 ]6 Z' ^0 y! C- l( a) x
what is the output? |