Sample TextJava培训考题: M5 c% j9 R& g% r
1. 给出以下程序:
% v7 G" { t) O3 b+ {) m1. public class Colors {
6 k1 z2 ?" ?8 D6 c* ?2. public static void main(String args[]) { K& Z/ ^( l, d
3. int n = 1;
& _. t9 D$ z( o+ L( ?! q5 o+ L8 I1 Y2 N4. System.out.println("The Color is " + args[n]);
# B/ k! ^. Y5 R5. }
# A- ?# _+ l) [% B1 ] y6. }' x5 s' d6 o: q
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”
; j% W5 ]1 o/ W+ yA. Colors red green blue yellow+ o* u( H/ g& `) P9 {
B. java Colors blue green red yellow
. y. {. k/ N2 A9 a$ Y1 x- O" pC. java Colors green blue red yellow4 q, L2 n' B5 l! N1 e! W1 ?
D. java Colors.class blue green red yellow
: ^' K6 q* j7 m; s" U) |5 y& fE. java Colors.class green blue red yellow
" ?) T& u7 s* D4 K7 ^" Y* w2. 读程序:( X- X8 M5 Z3 Y- p
1. public class Test{5 v: t! F f2 s' K8 A; R
2. public static void main(String args[]){
/ H6 E7 |2 n* p, \- a4 T7 W$ v; A& k3. char c = '\u0009';
8 J- [3 B% R; P% k* m; o4. if( c == 0x0009L)
7 c9 `. v! h; r# S5. System.out.println("Equal");
" C0 R2 @6 | q& k3 U+ a6. else System.out.println("Not Equal");
0 v1 S+ i0 w+ G d! F! b7. }& q/ n- a+ S7 y2 A [$ |
8. }
- s0 j3 Y3 P9 m, @6 K& Y0 l当编译和运行该类时,将会发生什么:: ~4 `8 b$ x8 z$ D5 Q$ p2 g
A. 程序因为第4行将出现一个编译错误8 z4 b, M. a8 S4 ]0 Q/ {
B. 程序因为第4行将出现一个运行错误
, j/ b' z7 x* I" qC. 程序因为第3行将出现一个编译错4 \, Z* j& s6 _, j: P
D. 程序输出”Equal"; f. Y: [& ]9 Z, a" N, S2 A% F' {2 f) j
E. 程序输出"Not Equal”% C& d0 \1 R$ Y* q0 R
3. 读程序:1 ^: \3 M/ m1 k+ L+ N2 m$ z; I1 w
1. public class StrEq{
+ g9 d2 X6 G- m4 g4 ~2 T7 b2. private StrEq(){8 z0 k( K$ G) ?9 C
3. String s = "Bob";
* U4 h9 ]) D2 C- j: ?; s4. String s2 = new String("Bob");
0 @' ~) O7 s) f3 l' o% d5 N8 H5. if(s == s2){# u: B1 I9 \& a& j% k( J
6. System.out.println("Equal");
9 u ?; ]3 p' {* {7. }: [+ t1 F% X! r1 u G" G
8. else{
4 O1 d3 l: Z% q; |, F9 f9. System.out.println("Not equal");( Q& [' S4 W& ?4 Q9 L b1 l; y
10. }
, ?5 L1 W4 `6 G$ n# b1 q# A1 [. {11. }
0 ^- ^) \5 m+ j* R. G% B. Z6 A/ g12. public static void main(String args[]){
) E, p- \/ Z( @# p6 g9 N13. StrEq s = new StrEq();
: F: G, h* B5 `! u14. }
% I. L$ {( X, f& t- K; k% h8 K15. }
: G- L0 q- D/ x当编译和运行该类时,将会发生什么:
1 F, n/ |4 R2 l* GA. 程序能够编译,并且输出“Equal”
: J3 s/ w1 y* V: @8 B$ OB. 程序能够编译,并且输出“Not equal”
: e. ^6 r' b0 E1 o$ yC. 程序因为第2行导致一个运行时错误
: v1 K) \1 N) m* m; G- ND. 程序因为第2行导致一个编译错误
4 N/ p* h, p3 L8 |9 ~& k7 y+ b/ E( L- k4. 以下那个赋值是非法的:
! q$ Z% m2 p7 a* D: i; iA. long test = 012;
" H N* u) n7 y1 `1 W- }. OB. float b = -412;! k c4 ]( t+ `0 Q# O
C. int other = (int )true;& G/ u. r$ I6 f! c7 i' e' k
D. double d = 0x12345678;4 [$ l: x% s2 R# B. m. |5 R
E. short s = 10;
8 P. g% A2 ?" _+ k6 q- F( N5. 读程序:9 T% p" c, Q$ m, U/ S+ ~1 O5 R& d
1. class CompareString{
. W- K6 \5 n$ o% Q4 c) t/ H2. public static void main(String[] args) {
9 e0 `+ E L5 l# L2 D5 N; i3. Integer x = new Integer(5);
% s8 h; `6 o- N# P4 r# m4. Integer y = new Integer(5);! \6 Z$ t' M& u+ u, O, ^$ o
5. Integer z = x;
, l B9 N/ A6 o, [; ^+ Z( y6. if ( ______ )
7 v1 E8 @( r4 R6 z7. System.out.println("true");' |3 W9 d, o6 w6 m' ~* }2 k
8. }
$ p% {& f, e, [# A4 b9. }: d+ n Z! ?' S- L* ^* @+ l
以下哪个选项填入第6行后,不能输出true:+ R* ^" D. N6 N% n5 @6 x
A. x==z' `" y! h/ J( g# j2 I
B. x==y
( s" L1 k! ?: U' P2 v' MC. y.equals(z)3 ?; y& w+ q3 c6 I0 \3 r) i2 P
D. x.equals(y)
O" ]) r: ]2 s, D; {) I( ~5 Y6. 读程序(双选):
4 z! \ n6 A. _' N$ @0 D1. public class Declare {
: g- G( ?. |( V) }& w2.
% `- g+ n* g& R; t: l7 D+ l3. public static void main(String [] args) {# c5 _+ b. J R/ g! T1 s
4.
% [, y% k3 r- L- C6 C0 K9 a1 T5. System.out.println("The variable is " + x);! S4 g# y0 H8 b: f7 O
6. }
$ ] G' t y& b* I9 O8 ~# G- @7. }
1 c( x5 m7 Q! n Y- ^/ k以下哪个选项中的代码可以使程序编译通过
1 A; `) ^" D) l9 G6 Z- W) e1 E! HA. 把“int x;”放在程序的第2行
% m$ Q3 J) a; d, wB. 把“int x;”放在程序的第4行: H F% B7 T* s6 I5 ~
C. 把“int x=5;”放在程序的第2行
" F" ^6 L% \) s1 S4 @0 ]& }D. 把“int x=5;”放在程序的第4行& @7 W& a$ n! j+ j3 @2 O
E. 把“static int x;”放在程序的第2行
) E( |: u4 P* n2 p. n' a2 ^1 @0 Z& ]1 C& M; K5 p
N6 e# B @( {- U1 {
; `( {/ Z" S* W6 f7. 读程序(双选):
- c; G" D8 d* d3 v3 @( d& I1. public class Child extends Parent{
& P$ m) E+ ]1 _2 {) H; e7 t2. int a, b, c;( b' V" O0 c! z: x8 d- n
3. public Child(int x, int y) {$ o& `2 g& u2 k/ @! o# ^- K4 y
4. a = x; b = y;
0 j& T% g* @" ?9 S+ R5. }4 M0 l; j+ F" z+ f6 j9 F- f
6. public Child(int x) {
* ]5 R# e1 r: k& e U; y+ F+ M* ~7. super(x);/ j" u; P+ H3 s7 W9 T* }
8. }( l0 Y1 ?5 Z* x5 W
9. }5 `/ S9 G7 L. m7 x: U
为了能使该类编译通过,下面那个构建器必须出现在父类中:; c4 m4 A8 ?9 x5 d P. \
A. public Parent(int m,int n)" x0 U9 [" ]/ E+ _- V
B.public Parent(int m)
, g, `* }* ?0 U2 _, A# [. ?C.public Parent()
! `2 j# h. H3 P qD. public Parent(int m,int n, int o)
- ^2 X( B9 C6 x( R2 c5 V O8. 读程序:
* D4 v; L4 T+ H3 W z$ Q1. public class Derive extends Base{! F" B6 |# b5 v, Q
2. public static void main(String argv[]){
' c# W* [9 s3 j9 h+ M3 M( O5 V" l+ T3. Derive a = new Derive();
8 l, l% j9 Z& J' [- ~' m4. a.method2();" I2 K& F. l5 P9 d+ B, L( S
5. }
% R2 f8 [7 [9 d& h$ l9 t- t3 p' q5 h6. public void method1(){# N* R7 c* U5 X0 A2 M
7. System.out.println("method 1");5 B, u4 {8 A7 ?# N: f, B
8. } 2 I4 l+ e w5 C0 Z: H
9. public void method2(){
6 ?& W8 j. D9 E10. method1(); 5 F7 ^2 ~) |' B* G _8 a8 t2 D
11. }2 W* O) d& j' I/ K3 `
12. }
/ [0 z0 {" k% u& H13. abstract class Base{0 Z, j) r1 `1 E( }: G) Y
14. abstract public void method1();# [7 d! h. N. Y1 ~
15. public void method3(){, G0 w, B/ Z/ L7 x9 y
16. System.out.println("method 3");( l. }9 B% |; G' X7 w |& q" S) D
17. }
0 y8 E5 ]) t+ h! d- I2 k}
1 H" K7 l$ W& `, f当编译和运行该类时,将出现什么:
# n# u: `8 g) l( ?9 T2 a' h1 TA. 程序输出“method 1”2 m' h6 {8 H% q8 i
B. 程序输出“method 3”, {3 p1 |2 d, W+ i1 T, O: d7 I
C. 程序因为第15行而出现编译错误$ R! f( E$ X2 P/ a. I
D. 程序因为第15行而出现运行错误; x' b: @0 z+ F5 a
9. 以下哪个接口的定义是合法的:8 i- C4 P& G- v) }; Y$ O+ |
A. public interface A {int a();}; c+ B6 d2 X# `0 |+ S T+ O
B. public interface B implements A {}
! ~/ U2 ]/ F7 {C. interface C {int a;}
4 ~9 a' P8 \ O! r1 X2 TD. private interface D {}4 h# ?) H% b( R0 E, B, c& [4 X S
10. 读程序:5 n9 g y" E( u+ e
1. //File SuperClass.java2 b; M5 R% j7 P' U5 A
2. package MyPackage;
4 {6 o% q1 t6 d' M3. class SuperClass{
6 O' \: T/ j# S/ k6 g4. void myMethod(){& c j' U5 B4 t) { c5 c% I3 E
5. System.out.println("SuperClass");; G6 I6 E+ S+ E! R0 g
6. }
+ x7 H/ [' m0 ]- F F- L7. }) c# Y! ]3 M( p5 R4 }* r
8. //File SubClass.java4 h8 l7 f$ I+ X3 N! s$ R
9. public class SubClass extends SuperClass{
6 ~* A6 X5 T) g& B% z" O1 L# [/ _10. public static void main(String[] args) {7 _1 Z( E% c$ l4 a
11. myMethod();! ^- D; f+ l0 l+ G/ Z: [8 F
12. }/ q9 C4 Z3 b* S$ I% X" w
13. }
: ~; W: F9 g) i# h当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:5 f- l: `% _+ ^: V* m
A. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”
8 Q3 f4 s9 e m6 q7 }; AB. 两个类都回出现编译时错误$ E" D. _, F7 }; E* m
C. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误
0 x1 \5 Z; {! Y" X. r1 oD. SuperClass.java可以编译通过,但SunClass会出现一个编译错误/ |. L+ Q! L& E* j1 `4 A3 Z% X
11. 读程序:
( C p) _9 _3 w2 h1. public class Test{$ w p n" I8 o$ D) t e) a
2. public static void main(String args[]){! I0 ~' r6 {6 K# a$ ~9 L
3. int[] i = new int[5];
" ~' y* H5 e4 e6 X- G4. System.out.println(i[5]);
5 X% R, ^: v9 E c- z Q- U/ {5. }
. I- u# }- f3 u9 l! z7 t6. }% J& g) e3 F! t& s4 @5 N
当编译和运行该类时,会:) [" ^9 j3 z( ~( ]& A7 X
A. 出现编译时错误
4 P& v. b& ^+ Y1 f1 BB. 出现运行时错误
7 f! f% t1 M7 l' T6 rC. 输出0
% E1 n3 q* B, ]% L- C; U/ L) @7 w, V* ND. 输出null, l- G5 p) L# o% t
12. 以下哪个选项是正确的数组声明或初始化:
: L/ v ^# n+ v XA. Array sizes = new Array(4);: e! `% n$ Q" q8 v
B. double [] sizes = {1, 2, 3, 4};
8 [# ^3 r9 ?$ D' M# r1 t' kC. int [4] sizes;, [. K7 Y( X! T6 M, P8 Y) p4 H! P! m- o
D. long sizes [] = new [4] long;. a" w$ E7 T6 U) b" p0 u. K1 v" ?
13. 读程序:0 N; `6 V4 K" X/ N
1. public class Initialize{/ A" i+ w, x$ j% u, n! f5 y
2. int x = 200;
+ |/ P# C+ s3 Q& t" k3. static {5 d# e8 @3 r+ X7 J" {6 o
4. int x = 100;
) W& P$ U' Y2 T" ]4 {5. }
. G- Y( j: t- j8 _2 _: G6. public static void main(String[] args){. |# \6 Z% _) F+ `! O
7. Initialize init = new Initialize();
5 W% \. `- L' \/ s6 g. U0 f8. System.out.println(init.x);
# U% y6 ?6 F5 P9. }
& P, K V, I1 ^10. }
# Q6 ]% w" ~8 R* M6 [当编译和运行该类时:
( _1 Q( o3 P8 v0 W, L3 XA. 程序因为第3行产生一个编译错误
3 T" V8 z+ S5 i; a' x# ~, wB. 程序因为第4行产生一个运行时错误+ B* j/ W0 S) y1 N9 d
C. 程序输出1002 P' R4 ~2 _2 b
D. 程序输出200
( K% t9 H6 g8 S14. 读程序:
4 ?* s# G L% G& s& J l1. public int abcd( char x) {3 j% s9 y( b9 B0 @6 u9 S1 N- W
2. if ( x <= 'M' ) {
3 J3 y9 D4 x$ s" P4 t) v3. if ( x == 'D')
h* {6 H8 x0 x+ M9 z. P4. return 2;: m4 b5 I+ T/ N
5. return 1;
! j' H9 z! e+ g! H' m6. }& A0 g3 z( f4 H* z; f6 g
7. else if( x == 'S') return 3; H( b& `- N0 I& I. h" t: R3 P' W9 ]* u
8. else if( x == 'U') return 4;$ |6 Z$ t+ W8 b9 J! s& A
9. return 0;
7 m2 C! n7 d. }2 w; Q# O10. }
+ Z2 u4 E" n, G在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1
, L t% F+ b* uA.‘X’
: r; F8 ~( _& n4 {) u$ _) oB. ‘A’
. V( M% S$ ^% S) w9 m4 NC.‘D’& {/ K& a4 ]. z# h* V
D.‘Z’
& j/ ? o2 F/ q4 E0 f/ x# o15. 读程序:7 k7 Z. f1 C: H6 b$ W
1. class TryTest {5 h1 \0 B5 e, K4 N! ?( o6 `
2. public static void main(String[] args) {
# u5 T) ]* I- T4 J# B5 {3. try{9 E; _2 V- ^ I) d
4. System.exit(0);
: p i; ~; O; ]( A! n% H1 R" S8 Y5. }) E2 l: R' o/ U
6. finally {4 N8 ~) F+ w8 a5 Q, P
7. System.out.println("Finally");9 M0 X7 E8 h: _8 B7 D
8. }
- m" c% P; Z; l. |9. }$ }7 ~6 |1 ?& |: H4 m" i% P. u2 i
10. }
' \1 K0 @1 M+ o+ t: F当编译和运行该类时,将:* k, @0 Q+ H) b$ i$ v6 j1 q" g$ s
A.程序因为第4行出现编译错误8 _/ b& R/ X- i& J' u$ C2 [' f; o K
B.程序因为第4行出现运行时错误
8 ?7 I2 R) D% f; N) f& ~& o' o0 TC.程序因为第6行出现编译错误8 h1 N) @. U$ W& ]- n
D.程序因为第6行出现运行时错误
/ I0 h0 G% Z- A" z, TE.程序输出“Finally”
1 U# z: m) X* q9 M: aF.程序可以运行,但什么也不打印4 u* `( R* [5 z( k
) e) X2 Q- v; l* Y16. Given the following variable declarations:' \" f) y4 o; v. { C' ?
Boolean b1 = new Boolean(true);
! ~ k: O \% N% O0 ?+ A4 d. dBoolean b2 = new Boolean(true);6 ]9 e* i* p9 A! S: E, Q. T0 N
Which expression is a legal Java expression that returns true?
( d* D5 i3 O4 j$ zA. b1==b2
4 c k' j+ G; K) k$ T1 mB. b1.equals(b2)
O/ w4 @. @; rC. b1&&b2/ z2 c+ u% H! b8 t* T9 d/ y
D. b1||b2
+ G, I7 c+ |" W9 UE. b1&b27 k d5 b- V; b
F. b1|b2
$ Y7 q! ~ R4 \17. Given: s9 ?1 D, |/ Q' @
1. public class Employee {3 U! S. ~3 C w* ]: i# m) d1 F. i
2. / D' i8 n; T5 `. R6 i
3. }
6 g, k4 U& Y) ^" E! v) v* ~/ Q4.
' X) t4 ]: d% t4 L7 ]% ?2 Y5. class Manager extends Employee{
* ]" ]/ P" S s6. }
# Z, N4 o+ p4 ?- t4 {What is the relationship between Employee and the Manager?
: d& w5 K+ e) O8 n' B9 y# eA. has a# s7 ?- s4 O* ]) f8 d+ n
B. is a# ?# o0 I$ `4 C. t) A
C. both has a and is a# E% @6 ?/ b4 \ Q2 @: a& U
D. neither has a nor is a- b$ L4 l$ a* ^5 J; R- H
18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?
- H% _! ?+ r9 KA. BorderLayout
, m) O) a- x# o6 xB. FlowLayout
1 F% t& t- Y! w$ a0 |C. CardLayout, k9 W: W# L1 Y e8 y6 q( F
D. GridLayout9 p+ K7 ?0 x5 A. \) }! y H
19. Which of the following AWT components can generate an ActionEvent?
! V8 u9 I3 W3 kA. java.awt.Button# ]" Y' ^; m8 f8 b5 i7 C0 U
B. java.awt.Panel
" H& D7 ?/ [( E! w; {C. java.awt.Canvas
* w) m: m$ |1 N, w1 V9 o8 @2 \9 W9 iD. java.awt.Scrollbar
/ y6 ?6 U: T" b" s20. What kind of Stream is the System.out object?+ U3 Z9 O5 V4 ~* E3 ?
A. java.io.PrintStream8 {0 h# d1 r; q: p4 @2 `& n
B. java.io.TerminalStream2 a8 c( s9 Y. V/ @. _6 G3 a
C. java.io.FileWriter9 \) G- | G/ V) M2 J& [& ?# [
D. java.io.FileWriter
$ j6 q4 q/ K8 v; L21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)6 b8 x" h2 |2 \5 Q- q; X. ]+ n
A. java.awt.event.FocusEvent
1 P, a* x/ W8 Q* MB. java.awt.event.KeyEvent
) H, U4 g+ _# ^% W! b$ V+ zC. java.awt.event.ItemEvent8 \1 D& H; o0 w# _' a: P6 u* O! N. g: g
D. java.awt.event.ActionEvent
?% R4 M: i9 C0 B% ?5 _22. Which of the following methods are static methods of the Thread class?(双选)
1 S' G: ]$ c8 c+ N/ q! jA. sleep(ling time)
4 I; p9 _5 T% F" {' F- nB. yield()! m2 Z/ V* Z+ f& F
C. wait()
3 [* J! r1 C6 {/ dD. notify()
/ s V+ y9 }* g: V: v23. Which method do you have to define if you implement the Runnable interface?
0 x- H- ]) I$ X+ |. t) h" \A. run()
, b4 p+ Y' w5 R6 k/ u1 B- |B. runnable()
5 l3 t, n4 b! b8 \! x$ WC. start()
; y" k& j: E7 f& b( N. \, P( MD. init()9 @/ U' t6 f' ~& f5 q
E. main()- W5 O9 e, c% f! g4 h+ ^4 J; V' p
24. Given:) M& m$ |5 O3 I: d( o; B
1. class MyThread extends Thread {
_( a# q/ ~1 I6 D9 d+ ?7 j2. " L! a! B9 K0 l( Z
3. public static void main(String [] args) {* a7 w, Z4 A! S C' c% l
4. MyThread t = new MyThread();
! b+ F+ T0 [8 P0 H5. t.run();
$ f0 V+ A; e. q8 K8 V& o5 C1 q# s6. }& `! t$ R! M: d: A4 i+ U
7.
) @4 d. j4 M3 F( v, T/ b* t" Z; S8. public void run() {9 l3 d, |; b; V, f* G$ c
9. for(int i=1 ; i<3 ; ++i) {
3 Z# A, z7 t$ A& s! v( ]! H10. System.out.print(i + "..");
* V5 R- g! F# F) ?! N. K p11. } p7 T# y4 d% N, T2 e
12. }' U7 O7 x. I6 E0 G& Q. k4 p
13. }
% m5 U3 z- W3 _! wWhat is the result of this code?C
; p& E8 ~$ Z) k& n" @. qA. This code will not compile due to line 49 K8 h0 } \5 Q. N0 b
B. This code will not compile due to line 5
) g+ k6 {9 o- ^: iC. 1..2..1 s) k7 w" H9 t# Y$ ?
D. 1..2..3..- l& S: g; K7 I5 e9 ]
25. Given a local inner class defined inside a method code block, which of the following statements is correct? I& K5 y9 b1 W6 t
A. Any variables declared locally within the method can be accessed by the inner class
! I/ e4 o: O( v/ y% J6 BB. Only static variables in the enclosing class can be accessed by the inner class
7 Z. W4 D4 S/ S: p0 C% ]7 H7 v4 \C. Only local variables declared final can be accessed by the inner class) n- \5 O$ V. @
D. Only static variables declared locally can be accessed by the inner class.
2 @- {, J8 |4 C0 Z0 ]4 Q$ O4 d
/ ]+ B4 |" n T7 Q# [! E! p$ L0 x+ @3 b y# X& u) d
+ O' G3 K; M( }. r
26.public class SychTest{
4 @! k* r$ L& V, k q: k2 ?4 [( ] private int x;
" Y; w$ M5 r# r: B) u private int y; 8 `( K) x3 o5 M' f
public void setX(int i){ x=i;} 6 d1 h0 M- b& x
public void setY(int i){y=i;}
2 x) U3 \; l8 r7 }0 w: Q# x public Synchronized void setXY(int i){ ! Q* t( p0 V* h) y& _4 I
setX(i); . e+ _% t: D. s% ^# O
setY(i);
0 y% K+ {+ o1 ]0 M, C4 e! u } " A+ \6 w/ ]# o
public Synchronized boolean check(){ " S5 d4 [3 H7 I/ l+ ~& h) Q* ]
return x!=y; ' { M# e5 e5 u
} * x3 `, F3 U2 X5 `4 n! I P
}
5 j/ u7 T7 g0 M5 f( r Under which conditions will check() return true when called from a different class? 0 g2 x- c0 {3 D5 O. ]4 T
A.check() can never return true.
% _! Y) B% f, x! Y9 T B.check() can return true when setXY is callled by multiple threads.
! f" I+ F/ N; i( M6 m6 M+ h C.check() can return true when multiple threads call setX and setY separately. $ U" O9 k) `/ Y% i( ^7 Y) j* g' G/ q
D.check() can only return true if SychTest is changed allow x and y to be set separately.
4 m1 ]& W* _+ A4 `+ v8 a$ c
0 z% Z/ w2 U1 `& _" L4 z" c27. 1)public class X implements Runnable{ % p3 @& U( {# r
2)private int x; M) a8 R* R0 v, U
3)private int y; + c' H6 }* f, K0 k
4)public static void main(String[] args){ * j N6 M* b" ~" X+ o
5) X that =new X();
: K# ?' B w, F3 X# j 6) (new Thread(that)).start(); ' c) i, h* R( o+ {, I
7) (new Thread(that)).start(); X& e* l% G# d
} & M9 ]( {4 _* s6 F" R
9) public synchronized void run(){ 7 Q& m+ ~1 V+ x) S* v8 s' ]* U
10) for(;;){ , k8 Q5 f* Z0 o, I4 F$ n5 u: I
11) x++;
' K+ O1 R$ n0 s' N 12) Y++; + d2 S+ t( P5 D. z
13) System.out.println("x="+x+",y="+y); " { ]$ \4 Q' o9 w S
14) } ) `$ s6 f, G3 s% u9 z& L. e
15) }
2 k$ i2 b* j H" p2 Z 16) } 2 `( u! J/ s2 a: q- n7 z
what is the result?
$ ^* V4 g. l4 i* s$ \& B0 i A.compile error at line 6 * o8 ?' [ X& s, Z# s8 j; L3 r
B.the program prints pairs of values for x and y that are " r7 O m% b- t7 x
always the same on the same time & h% @) ^+ C( ~: }, @
$ a( s6 V& g3 f* {! n& Q+ X; `28.class A implements Runnable{
! Z4 e% N* O& r7 X$ t/ J, D int i; + \1 ~+ H* E# j2 u. n! b% H
public void run(){ 1 F0 f V! g% h/ N+ j4 R
try{ 4 I9 d: n9 { u8 t+ o
Thread.sleep(5000);
, m, C1 W) F" c2 `& D i=10; # J6 I3 g; {# V6 ^6 M9 ?- b
}catch(InterruptException e){} - M2 @" G7 J8 o2 |
}
. g* a5 D7 }+ Q! ^8 c& Y5 p } j5 N6 `. K) q2 l
public static void main(String[] args){ ' w5 D5 t' A `1 X
try{ 8 q6 b1 G/ G- a7 _" t
A a=new A(); ; w3 n# I4 _' J4 Y4 c; E
Thread t=new Thread(a);
' G' _ Y; D n( g t.start();
) t' v6 \3 N1 G2 C" z" s& x" c 17)
$ }: @; g( t6 k, i6 ]& u5 P int j=a.i; + |9 p6 `+ J0 y0 s: @5 r! z
19) 0 `4 e3 a/ T3 k' J( u& H: O
}catch(Exception e){}
1 k& i! G2 I# y, F. _ } ) j$ v* g1 w% h1 S: `
} 0 s7 ~6 i Q; F+ W* ~
what be added at line line 17, ensure j=10 at line 19? . t3 T/ O7 x: V, s/ h6 B* t& k& a
A. a.wait(); B. t.wait(); C. t.join(); D.t.yield(); ! z8 a. M @/ y2 }7 F" b; j
E.t.notify(); F. a.notify(); G.t.interrupt();
; \/ n" W3 O+ w# i7 N29.1)public class X{ 4 k+ c+ i! |( E5 B
2) public static void main(String[] args){ I! p% s# i9 q; \
3) String foo="ABCDE";
" T" A1 e; _; c6 H, a0 r9 L: H 4) foo.substring(3); ! R- O$ L* N' l, n1 i$ o
5) foo.concat("XYZ"); , o) [( w2 |. f) ?/ f0 }( y
6) } 9 Q& E0 V' z/ c5 Q1 y; v0 s0 x5 c
7) } * A% S* c2 J' O5 s2 q
what is the value of foo at line 6?
7 A C: h/ D- h1 t d0 w30. public class Test{
, N" y0 U( |$ }0 D$ z w public static void main(String[] args){ : K3 D9 e5 s6 C* F: Y; P7 G
StringBuffer a=new StringBuffer("A");
2 t3 D0 S& T. \6 T" d( L StringBuffer b=new StringBuffer("B");
3 W5 h+ ?' J' p, W; Z operate(a,b);
) \! l: W# ^5 C6 \. z3 F# M" |/ N- i System.out.pintln(a+","+b); / \* X( @+ E/ u) M; a# q
} % m! U& l0 S3 o6 r# G- F9 X
public static void operate(StringBuffer x, StringBuffer y){ ! h; J, M: f G" r0 H* ?3 Y! J2 c
x.append(y); . B, o6 j/ x0 B1 M; K2 |* L
y=x;
, M8 V5 d( `- D* E }
. [/ N/ K: O/ U } 6 K9 q z0 U4 h: e" `! i
what is the output? |