Sample TextJava培训考题
+ v c0 J+ d! {/ T1. 给出以下程序:
! K5 V* B4 p9 a% K& `1. public class Colors {
& [ z9 R8 X6 S0 O) X6 }2. public static void main(String args[]) {, h. q. A& R( V" V5 W& A
3. int n = 1;& |, g: o& Q1 g2 J X
4. System.out.println("The Color is " + args[n]);; [2 L, M9 J/ o. H, g
5. }: i3 D6 U2 G9 ^- N1 p' C
6. }
- U. y! c( [" j' b& y假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”
; x# M4 c3 S0 D' bA. Colors red green blue yellow
! @; o- ?9 O, O% H6 iB. java Colors blue green red yellow
2 F5 ^' e3 t8 J7 R9 u- E3 Y0 ~5 sC. java Colors green blue red yellow! u5 J! @: y1 ~. }5 L- K
D. java Colors.class blue green red yellow
1 h: R/ | E( |1 v9 hE. java Colors.class green blue red yellow
+ g5 [& S" u/ D% o9 Z/ `2. 读程序:) x' [# V" T' f2 _
1. public class Test{
' d! K0 ^! B3 v( B2. public static void main(String args[]){ G! K% W! S9 H8 s; U+ e
3. char c = '\u0009';
. q. [' j+ Y' R# e, ~: R6 ^4. if( c == 0x0009L)
9 `- o2 \) V* [6 S2 `' N9 W! X5. System.out.println("Equal");
! F3 J% B) l3 X* D4 l6 N( x: u( @6. else System.out.println("Not Equal");
X' a0 o2 Q! [5 T7 A. h7. }; n" M8 d! \: e$ A# d2 r
8. }) E8 k. m; L0 n% R* R
当编译和运行该类时,将会发生什么:
, d7 w+ P& i JA. 程序因为第4行将出现一个编译错误
& s: ^0 [8 N. |) B; y1 S1 PB. 程序因为第4行将出现一个运行错误
$ `7 t9 i- U0 u+ i$ l3 E! yC. 程序因为第3行将出现一个编译错
T# M6 T# T( k$ ^6 v- ~D. 程序输出”Equal"
6 z3 Y Q' K- p, pE. 程序输出"Not Equal”7 F* c* k9 A- T8 }2 |
3. 读程序:
& {7 o) e) V b' B! ^8 Y' X2 L1. public class StrEq{
5 k7 J" ~. q2 a$ e2 ^2. private StrEq(){4 W" Q6 B- V& ]! B+ U r3 }
3. String s = "Bob";
, {5 x$ P, P1 b/ |- W+ i2 M0 H4. String s2 = new String("Bob");
7 n3 d4 x4 V! [ H6 G5. if(s == s2){2 V9 Y7 |1 C( N0 ^6 U/ m) ?* A0 `
6. System.out.println("Equal");
* I9 d* V8 N, ^( f7. }) p% Z; p# ?8 c0 c, S" ` A
8. else{
3 x' l+ J; B( Q! l6 z5 l) S9. System.out.println("Not equal");' j+ \9 ^ ?. S* O$ t
10. }7 B1 Z( p) T4 Q
11. }
& p5 B8 b7 W$ }& E0 ]( \# W+ a! f12. public static void main(String args[]){
) ~3 F! H& ?$ }, [+ v13. StrEq s = new StrEq();
3 ^0 ]: k' Z& U$ O# P8 ?* |14. }: q) t- X# r6 g8 f
15. }
( K3 `2 T3 @. K. ^, ]当编译和运行该类时,将会发生什么:' r9 @3 k3 S+ @$ E, g1 Y
A. 程序能够编译,并且输出“Equal”
/ A) G% T G* `B. 程序能够编译,并且输出“Not equal”8 z8 C+ v# F% z$ \; Y; ^! f& X
C. 程序因为第2行导致一个运行时错误' b! H$ o' ]0 ^0 d2 M% q" c; G
D. 程序因为第2行导致一个编译错误, N- N( M7 B5 F
4. 以下那个赋值是非法的:
I% J( G v3 _1 HA. long test = 012;
* o! V& N0 n' {8 w) ?B. float b = -412;
5 I* O ]1 S$ |C. int other = (int )true;2 K9 a! a7 S! x q
D. double d = 0x12345678;
9 c Q# Y, c" g* r, C$ T# Y" T/ JE. short s = 10;2 F" X6 D* K" o! A
5. 读程序:/ A: p4 w, X6 L0 E ?+ s
1. class CompareString{8 e& a/ z" S1 z: _% g# s5 h
2. public static void main(String[] args) {3 n6 W; \7 i% z% X8 p" K
3. Integer x = new Integer(5);
" A" n& Y+ l* K4. Integer y = new Integer(5);
/ p& E+ W! h6 g9 g, K, y5. Integer z = x;$ M) U: h% W. Z. j) ?; [; \
6. if ( ______ )
! F5 [0 Q( D7 w: |2 V/ j7. System.out.println("true"); m' ?( p1 t ?$ U8 f. N" B8 r5 f
8. }" a. O8 {3 B& l+ \# a
9. }0 b+ T5 i! t/ F( O
以下哪个选项填入第6行后,不能输出true:
5 Z) ?1 `$ D: q5 X, i$ bA. x==z
$ t: ?# g# B3 V+ y0 ^5 b5 g5 PB. x==y7 @3 L. ~1 M# q8 N! l: g
C. y.equals(z); ]$ G$ i! ^4 g4 M
D. x.equals(y): C" g O. ?/ [7 V Q
6. 读程序(双选):
; r* _/ N, {0 q! v/ U7 j8 E/ l& A1. public class Declare {
, b7 K4 v u+ A, P' ]2.
3 t( N; ^# }+ N6 S7 M0 {$ A3. public static void main(String [] args) {9 E0 u' S9 `% g+ H1 t5 H
4.
# g5 T: v) V$ r) j+ N$ Y+ m4 i5. System.out.println("The variable is " + x);
3 Y( K* M( s8 M; y# i6. }
+ L u$ }! z* `" c5 o7. }9 O2 a7 E' q( o& e7 k
以下哪个选项中的代码可以使程序编译通过5 I( |3 u9 N" A6 \( a. T3 v
A. 把“int x;”放在程序的第2行2 \3 R: g ?9 R
B. 把“int x;”放在程序的第4行 ]2 U' o4 p7 A+ B# w+ ]% O
C. 把“int x=5;”放在程序的第2行
$ w R# _4 |& ]/ C$ L+ B. @- c! v0 A5 UD. 把“int x=5;”放在程序的第4行
$ N7 o, f6 s0 {5 e8 QE. 把“static int x;”放在程序的第2行+ p l4 e3 l2 i! c$ F/ ]4 E
2 y. X: f8 B+ N! b( F2 Z' }* \ t
9 |6 e) V( M2 ~7 d* R
2 `% k' G1 y. D/ A' L
7. 读程序(双选):
: l8 z; M i* `9 b5 h1. public class Child extends Parent{3 ?9 f+ A, K& s1 Y; \0 g$ v8 j4 C
2. int a, b, c;
4 I+ v7 J' k. J5 ~5 i, H D5 G. y3. public Child(int x, int y) {1 m- t% j9 u) S& g8 X e! M
4. a = x; b = y;
* B" v' h" b$ N, |5 M/ ]5. }2 p+ H: B, n7 T* p% D
6. public Child(int x) {. q/ I$ j/ z5 D. f
7. super(x);
1 \ Y+ i9 Q% G, K8. }3 o; U) G) a" z7 w
9. }, d k: _% V: Y) A3 z% |
为了能使该类编译通过,下面那个构建器必须出现在父类中:
/ T' _. D3 W; x3 o/ S4 o- ?6 ?A. public Parent(int m,int n); e6 K$ j4 ]- x
B.public Parent(int m)8 L2 q) o% q5 f
C.public Parent()
0 U" [" C, t, W( L- N5 {% HD. public Parent(int m,int n, int o)( q+ P' q& ^# T5 h
8. 读程序:
: J8 f/ K; _4 i! b6 W! q1. public class Derive extends Base{
1 }- C, D3 K( [6 ^3 ]2. public static void main(String argv[]){ y; g& Q- |9 B7 e3 L! M8 X
3. Derive a = new Derive();5 R/ s: {* E$ ]7 o, e9 p; r
4. a.method2();
; \1 T+ v+ H6 d( h5. }
/ u2 d8 B R! ^, W" u0 A6. public void method1(){2 f! B3 m# j/ ?1 H
7. System.out.println("method 1");
0 Y# C4 [1 Z: V0 L- D) T o! J8 H8. }
# s: n% ~- z% P/ O" w5 M. Y9. public void method2(){
6 j# z" @- T1 M- Z( ?1 K8 l10. method1();
' }& J# W5 \' S1 E2 E2 p11. }
% C9 L2 z) {2 ~# J12. }3 K* C7 K/ b7 h2 k/ U$ {6 {" l
13. abstract class Base{
0 @5 N+ [, D3 D0 @9 x% O8 r; J14. abstract public void method1();7 N, }% [ k* u
15. public void method3(){: a$ i7 j8 l& W0 n
16. System.out.println("method 3"); U1 D2 n; `8 I E4 {6 r6 ?
17. }
; r( ?3 h" [; S% x1 c( b# X}
( l! l* g0 h! O+ t1 f2 }当编译和运行该类时,将出现什么:
( B! ^4 d! B3 o/ zA. 程序输出“method 1”
. v! U K% R2 w1 i5 W# _B. 程序输出“method 3”
+ K& R9 n" _* z5 EC. 程序因为第15行而出现编译错误
! L- ` r# x! K& |" ?* B z4 z$ pD. 程序因为第15行而出现运行错误# W8 c, s+ G/ j5 V
9. 以下哪个接口的定义是合法的:
9 R: }2 r" S8 d* V+ qA. public interface A {int a();}" s1 c7 d0 B0 u4 \
B. public interface B implements A {}
2 W: r6 h/ D2 H. K( P8 l" QC. interface C {int a;}5 R& `) p- T% F; e: V! n0 A2 E1 ~, J' Y' Q9 a
D. private interface D {}
0 ?7 n7 Q7 V: x% e# e0 z10. 读程序:
8 ~8 h; K: e) [/ u" X$ W1. //File SuperClass.java4 W6 ?; j* v6 M1 f
2. package MyPackage;8 m9 c, D/ H. m9 W4 e
3. class SuperClass{$ R& r( E/ R5 e( W$ ^
4. void myMethod(){ y, ]% U v; u5 i
5. System.out.println("SuperClass");- a( K3 ^- y8 |8 C* [) Y( q
6. }9 T" @6 I7 |' I2 r9 F
7. }6 F) W0 z8 C3 d+ T5 h! k
8. //File SubClass.java
4 K! [( P; T# n8 \7 `; ?- M9. public class SubClass extends SuperClass{9 F& m# ?3 u; }1 v2 `, u. y, @
10. public static void main(String[] args) {
5 p) ~3 c: x! L' W2 t11. myMethod();9 o2 E0 [3 r, {& n7 V
12. }, G j* |. K3 k. e) a
13. }
H+ W& w. F) g3 j9 `; d当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:
1 F4 P$ K1 ]4 ?, l0 y6 SA. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”8 B* {( u" U' |: ]( S
B. 两个类都回出现编译时错误
2 V8 Y+ b u+ W2 c4 ?6 H( AC. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误2 g1 ^ N7 d. o
D. SuperClass.java可以编译通过,但SunClass会出现一个编译错误' [, f( R; ~5 A- b# A" {7 N0 R5 m
11. 读程序:0 R0 F8 C3 C! D7 A; n9 ]. {
1. public class Test{ X- Z/ F+ N' \
2. public static void main(String args[]){
" a* q9 U( w# R1 h: e; U( A3. int[] i = new int[5];" n5 u. x6 Z% `. R3 q
4. System.out.println(i[5]);% s# R$ K5 C. V; V. }: }
5. }0 m! s$ g: F( ~/ v! V
6. }
1 p# _7 A" {/ k0 k' d/ `当编译和运行该类时,会:
7 ^8 p! d% \& b5 B% Y8 pA. 出现编译时错误 { z3 t! A0 V1 C
B. 出现运行时错误 m) q, E; T) m U
C. 输出0
# G+ V, e$ |, G0 _: f* }7 iD. 输出null3 f5 l5 X( ^9 i. x
12. 以下哪个选项是正确的数组声明或初始化:- |- D" c) w' x' T* Y2 ~
A. Array sizes = new Array(4);
# U: _- f$ E7 p3 O" ]$ H; fB. double [] sizes = {1, 2, 3, 4};2 ^3 N+ D. w2 i
C. int [4] sizes;
% a# R$ J3 `) B3 r& S2 _D. long sizes [] = new [4] long;
3 t8 o. b2 w7 y: Z13. 读程序:
# }' ` S/ q) G; ^2 o6 \1. public class Initialize{
. ~: z, X, _# v! c5 p7 j1 [2. int x = 200;- Z7 S; v2 }/ B, j0 w
3. static {
$ R9 t9 o& Z: n# r& C4. int x = 100;9 c; R+ I" t& W' O0 Y7 E. I# \# T
5. }
( ]# x% u% d: v& ?6. public static void main(String[] args){
3 @4 q% r+ O7 ~+ j2 c" K4 Z7. Initialize init = new Initialize();5 S8 I' C7 F4 ]- Q
8. System.out.println(init.x);; _7 v% q$ l# y7 S# I, r& g9 `
9. }* }/ e& _7 B# f0 M0 V, `, q
10. }# u4 \: C" b1 o+ Z1 A9 a* C
当编译和运行该类时:. Q* S1 `/ N+ v; P3 W
A. 程序因为第3行产生一个编译错误
3 K6 \2 e2 w9 w" j. H; U3 g1 WB. 程序因为第4行产生一个运行时错误1 U2 f) d$ z+ A1 Y, v; N8 o
C. 程序输出100
) o2 G' \* y) b b6 i$ VD. 程序输出200
- f3 w4 P) q& x3 q& |7 u4 p14. 读程序:
I9 v5 j/ x: L9 l5 v1. public int abcd( char x) {
0 Y$ U6 `" ~5 m2. if ( x <= 'M' ) {+ T C: O. w! R, z5 L# Y! ~
3. if ( x == 'D')
* g2 y' ?$ l) K/ _+ V; I4. return 2;/ Y, J6 ^5 R9 D) l2 C7 m R
5. return 1;) a# G8 A4 f, C
6. }: W9 F) A- M. b% i
7. else if( x == 'S') return 3;
. D; z: K0 x0 h( t, V" s8. else if( x == 'U') return 4;
( y& a T1 T; V- E8 e( ^ |4 k9. return 0;0 g |" U2 I0 l
10. }& @: b u$ P2 B3 N g9 n
在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1# ^3 ]" ?* \( d! R. ?
A.‘X’0 z9 H: S4 k5 M% o
B. ‘A’" _$ R5 }4 P' k+ C, C3 o
C.‘D’
# G$ S6 y: e- Q- SD.‘Z’
! K4 k& Z |3 {; m t15. 读程序:
. X$ O# R% c% R: \1. class TryTest {& m( H6 m7 l7 f( z
2. public static void main(String[] args) {4 C" o: ]7 y5 a, ~' _
3. try{6 U: r' Y4 Z- ?* V2 J* P: z
4. System.exit(0);
+ F! T8 C5 Q" l8 a' C, e) Y5. }9 y6 a! I* C6 F& }$ d! Y
6. finally {
2 O9 d+ {" H% |8 ?8 G; b( N. ]7. System.out.println("Finally");
( N" V& o( [! x! L$ b7 ~0 D$ C" M, s8. }: j5 x, I2 @* V( e
9. }: ~2 E/ M* y4 T6 T! l
10. }
$ n2 |0 V( q3 ]7 g o6 t当编译和运行该类时,将:7 n, }8 M6 W1 h) R9 P$ l
A.程序因为第4行出现编译错误9 Q0 O( B1 s4 w- W7 J s' R
B.程序因为第4行出现运行时错误- T8 e; _ Z9 _9 ]2 O
C.程序因为第6行出现编译错误. k% f2 R( e, M% z
D.程序因为第6行出现运行时错误: b0 i( \; i. _3 ^8 y- B
E.程序输出“Finally”
7 ?' |- X1 E8 o1 Y; G& C9 G: sF.程序可以运行,但什么也不打印& \- e, l/ a, _0 |6 C/ @" z
! J9 w& z0 g {2 a8 y+ y5 ? @( ?16. Given the following variable declarations:% u. M7 D& p. T
Boolean b1 = new Boolean(true);
9 i# r2 L$ ^3 o8 \9 ~$ \! x: IBoolean b2 = new Boolean(true);( P4 O: e- u' M4 d2 V" N0 j" u
Which expression is a legal Java expression that returns true?
c2 t3 N9 @: J$ e( U$ RA. b1==b2 n( I0 a8 P4 v8 r# g3 T( s9 R
B. b1.equals(b2)
$ d9 F1 S/ B sC. b1&&b2
5 h1 h4 C1 t! S5 d# D ED. b1||b2& l0 F' N { F0 l" X- \
E. b1&b2& ^, t5 F7 Q$ L* _! W
F. b1|b2
! @- R$ q O1 n- Z' O. R( a17. Given: . {% D/ s$ V4 b9 @* j
1. public class Employee {6 B# R! u9 L5 I, e
2.
4 T4 m. k0 t: ]; T: L( x3. }
1 `' H2 p, |& C3 p# k' f4. % ?8 R6 o2 S2 N
5. class Manager extends Employee{3 t8 i( ], k4 f) y# w3 V
6. }# }4 K; Y$ _8 {2 o e
What is the relationship between Employee and the Manager?
7 L0 K( m$ T3 O" A O( G h3 L: LA. has a. u0 d+ ~1 {! t; u3 _% G
B. is a& X Q! Z' Z8 x+ P
C. both has a and is a. n( `6 _) P- J7 {# e# M
D. neither has a nor is a3 U- @0 J2 x* J9 Y
18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?0 q# a5 ?2 j4 {/ J, `
A. BorderLayout
( t4 W7 {! S. Q- gB. FlowLayout. G! ^( F0 ?" j- p; i) b/ g6 c
C. CardLayout4 f% x! E# ^: E+ |
D. GridLayout* q. h5 i9 K- Y9 J7 L3 M) I9 w
19. Which of the following AWT components can generate an ActionEvent?
2 t) M& H& f1 b- H- SA. java.awt.Button
8 M( z- a+ x/ ? [/ KB. java.awt.Panel
6 [0 z2 [+ H( j4 hC. java.awt.Canvas
3 V T, i5 e* k, ^' a# r% ID. java.awt.Scrollbar3 D" x+ s3 n9 \, i( y8 h6 r7 Q* _1 I
20. What kind of Stream is the System.out object?
1 v, [; E8 }" q ]5 ?- N" N' z) WA. java.io.PrintStream
/ S" z7 o2 g" @8 b4 \" K0 W5 e6 KB. java.io.TerminalStream- O [. s7 v) n4 m$ b
C. java.io.FileWriter
! V( ]+ |1 @/ T) j8 G nD. java.io.FileWriter; O! o; m# i, m. O
21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)
8 I" U( j0 P( f6 O* Q8 F4 ~A. java.awt.event.FocusEvent, l7 q1 ~' l$ X- c2 ]
B. java.awt.event.KeyEvent
1 A" j; X6 }8 c, g& ^3 A( {. V0 @C. java.awt.event.ItemEvent6 j L5 `. w7 p
D. java.awt.event.ActionEvent
( R3 \; Y6 ?1 f0 t( a, A0 t22. Which of the following methods are static methods of the Thread class?(双选)
5 n/ ~4 n9 i9 P6 qA. sleep(ling time)
% }- S6 K! l" [& @; ]4 u/ YB. yield()2 M: r2 B" C5 p# a& e% D
C. wait()+ G6 c; @# D& [3 R( [* k" R7 p
D. notify()
0 h! I( M7 M; N23. Which method do you have to define if you implement the Runnable interface?
) L' ~: T$ w) ]; AA. run()
) b }1 C: J8 G( @$ J, wB. runnable()- G, F9 E% y, W6 M- p
C. start()
/ q3 B) K' Y9 f$ w+ d! cD. init()
. O, l* a$ O" {% O/ f4 W2 a$ ]E. main()! }+ h6 a0 E& Z4 n* Q) x$ P
24. Given:5 h2 C) p2 L% M
1. class MyThread extends Thread {9 F" E0 \, P o# m/ D7 G
2.
% O% M% P! o: W9 c$ ?* L7 `3. public static void main(String [] args) {9 n5 _, T" c0 G0 R. ~4 E
4. MyThread t = new MyThread();# G' U5 y4 _% u
5. t.run();! @2 k: Z6 S+ n) C; A5 u
6. }
E# H) e ?: M/ [3 C7.
+ V" y! b; P6 S, E3 J$ J! m8. public void run() {8 H; m A7 u j3 C1 X7 Q; U
9. for(int i=1 ; i<3 ; ++i) {
0 V% x( Q( D/ ~( K+ M* @' T10. System.out.print(i + "..");# v" G, J, C: O' P
11. }
) D. z# N+ O) B$ i12. }! {6 I& s5 f$ b8 k3 g( T) w7 r6 T
13. }% W% m2 i# ~* @$ |
What is the result of this code?C
: ]% D# u& ~6 l$ }! GA. This code will not compile due to line 4, [: O/ |6 f& }( ?, v4 s& ~' ~
B. This code will not compile due to line 5
( k# O9 j' v, n: D8 O. t" ?C. 1..2..
! K; ]1 h" R/ e& Z8 zD. 1..2..3..! w. v5 {; ^" Y/ T, y5 ?, Q
25. Given a local inner class defined inside a method code block, which of the following statements is correct?
1 o5 u* v: x2 ^: P, AA. Any variables declared locally within the method can be accessed by the inner class
* s6 r0 e: S8 f3 f( R5 vB. Only static variables in the enclosing class can be accessed by the inner class
1 j3 F8 o- O' i) ]: j2 N. [C. Only local variables declared final can be accessed by the inner class
$ g" _. a# L1 F' P9 q2 o5 |6 }D. Only static variables declared locally can be accessed by the inner class.
$ C9 c( j3 L2 B9 a# L0 l0 N( k1 ]
3 Y6 V8 Z% ?# y/ n* \3 R( B# m, @+ ~8 }: Y2 k+ Z1 A' B& `* ^9 w' D' N
' l _( ^( x7 z# {4 U) k26.public class SychTest{ - v4 T+ B' d0 N! O0 V. g3 t
private int x;
C! M: {% s4 R private int y;
4 T1 R! {2 |# u% A0 ~ public void setX(int i){ x=i;} ; Z0 m! y% a# j0 [# l
public void setY(int i){y=i;}
2 q/ `6 d/ S1 G1 D* X0 @# Y public Synchronized void setXY(int i){ / D# h, B/ r) D* `* O
setX(i); # |; P; t9 {( X8 p ^2 m6 R/ ^% r/ b
setY(i);
+ ^4 n# H* S! }2 P4 C1 { } 9 Z W: h, w) y& K) b
public Synchronized boolean check(){
3 `) {) w& G7 Y3 I0 `4 O% } return x!=y; ! \* f7 B) M( f$ R/ I; F
} $ v& r" D9 q7 G! m
} ; l ?$ b! |& l) z; G
Under which conditions will check() return true when called from a different class? 5 b4 s* R/ i8 U+ N* p2 A- {9 n8 Y
A.check() can never return true.
; F% D7 t, n% ?7 Z B.check() can return true when setXY is callled by multiple threads.
( O* @/ N$ h' x1 K9 ~, u; q4 D C.check() can return true when multiple threads call setX and setY separately. , x }1 Z0 I0 Q+ ~6 d/ E
D.check() can only return true if SychTest is changed allow x and y to be set separately.
. z* l# B2 w5 `' M. d; ^
6 H' G- i. h& i" O" H27. 1)public class X implements Runnable{ , g: L9 G' v! v; a8 ]
2)private int x; 4 ] s8 D( g( w) i' o4 g9 n$ `$ N% K
3)private int y; " b4 S* N$ j& B" G6 J
4)public static void main(String[] args){
' k. n, Z. q( B3 [# ^ 5) X that =new X(); 9 h/ q, N- x0 ^* C2 t7 w
6) (new Thread(that)).start();
7 t- h$ I- l* t5 M9 U3 b. H2 U 7) (new Thread(that)).start(); 1 I! l( y: @! p3 n# i% m4 E
}
1 u" A9 s) d+ M6 j; t 9) public synchronized void run(){ 2 K4 }$ I; v( ~9 \% h/ ^
10) for(;;){ - w; K7 w2 t4 ~# n7 M+ z5 ^
11) x++; $ a5 j: X- T7 v" e( T; }% |6 R: V
12) Y++;
9 B0 }: _- T8 {+ ?# O) M- t6 v" C 13) System.out.println("x="+x+",y="+y); ! ?1 S, @, x0 M. f# ~
14) }
g* z, x& T) s 15) } + u/ L' F, p8 _/ |2 ?
16) }
# s5 W5 o6 f k! ? what is the result? 4 @" B7 H& B4 m" G' b5 Y
A.compile error at line 6
+ W3 }8 Q0 K# A1 a B.the program prints pairs of values for x and y that are
|# y6 M& F# g) ?- S! ~ always the same on the same time
J# w7 N5 E' U0 V' J3 \; K3 M7 E
; V+ \+ |& ]- O& G; C$ D& |28.class A implements Runnable{
7 J K: w6 G$ \1 _% t int i;
: w3 `# ], F0 P7 H" ^5 u public void run(){
. i$ p# A2 H1 w/ l% J& D try{
. Z; P# [0 [, ?9 I3 C- }2 o4 M& f Thread.sleep(5000); " S4 Y5 C+ _! O- h' `8 N
i=10;
" X$ u5 o" \. B2 M. j) D* w4 \ }catch(InterruptException e){} 3 m( V8 [0 y* F8 h
} ; r( W4 N }9 T; g y
} 9 |. X/ ~# l0 L+ w7 J3 p6 b* |5 b: m
public static void main(String[] args){ 7 Q8 m' ~1 K* A; U' V
try{ ) C+ y# S/ ^& w ~' Q1 |- S8 f
A a=new A();
7 M# h1 q2 P" {, m Thread t=new Thread(a); " C: X$ p6 d( v
t.start();
: G0 G7 j$ \6 d$ B+ v 17)
3 F( f7 W( }, w' {2 g+ c int j=a.i;
* L2 J. O! p5 S% {, }8 D1 ]( n 19)
' h6 P+ u! Z! o: F/ h+ x5 C: ] }catch(Exception e){}
0 R6 E! H% y8 p5 |' r! P }
s" z0 N5 }0 d* r# S. L9 N1 e. Y } 8 U' l2 H( I, H
what be added at line line 17, ensure j=10 at line 19?
( C3 ?+ _* I9 u A. a.wait(); B. t.wait(); C. t.join(); D.t.yield();
" \2 q4 u* h- U4 A2 L E.t.notify(); F. a.notify(); G.t.interrupt(); $ }0 J' M' y4 W# U
29.1)public class X{
$ H, W2 v$ E; K/ l$ L 2) public static void main(String[] args){ 5 Y' U! E& n' k' q* x# r* ~* ^$ C2 M
3) String foo="ABCDE";
C& l3 p( X6 z' K5 h2 d* q& \5 C 4) foo.substring(3);
3 l/ x) s r: g [9 w6 p1 T 5) foo.concat("XYZ"); ; L3 j. A9 g& R& F; P( P6 o0 O- _
6) }
3 x6 h0 a( L9 Y( j# W 7) }
& c! q: `: O& ~& F what is the value of foo at line 6? / {9 _7 Y& T4 \ @ f7 Q) S
30. public class Test{ 8 e7 x2 r9 X% ` ?$ M% v$ Y! d
public static void main(String[] args){
: T- Z( E/ l$ @+ k& e' j StringBuffer a=new StringBuffer("A");
0 R9 ^! Q% a4 Z6 G9 E1 I StringBuffer b=new StringBuffer("B");
( h1 g' e n `8 U* r/ v operate(a,b);
% h5 @! D$ r; m6 W7 p0 i% V6 e System.out.pintln(a+","+b); R1 j7 }9 E: N
}
0 d$ x, u3 ]# x0 f% ^; j public static void operate(StringBuffer x, StringBuffer y){ 5 Z u6 P/ {% [' D0 f( s/ w
x.append(y); 9 \+ H J7 w: L
y=x; " N! d: U" y; T# D; i) V: ?5 X
} - F8 a3 z* [; {- Y% P2 ?% @& T
}
0 c) M9 L! M1 S8 ` what is the output? |