Sample TextJava培训考题
- I2 V+ q1 c! C9 G$ L. J1. 给出以下程序:) l! i. m( R" K* f2 R
1. public class Colors {
2 o5 K8 V$ K9 A U2. public static void main(String args[]) {9 L) K- F% J0 c) q
3. int n = 1;
- [6 H$ ^5 ^8 T) I$ W' y4. System.out.println("The Color is " + args[n]);
# Q1 L" q6 K1 b6 F: L) j5. }* S3 `9 B6 P5 m2 ]
6. }3 ?7 f2 `, A; Z
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”4 x; h8 z$ k# ~/ n a
A. Colors red green blue yellow
* W- @2 w9 [$ p: ]/ _3 g& JB. java Colors blue green red yellow
" ~2 P9 `, ?( L3 q' nC. java Colors green blue red yellow. S E! i# k% j4 V6 C
D. java Colors.class blue green red yellow
+ ^$ \4 d% Y1 I& t1 ?E. java Colors.class green blue red yellow# B: U7 m% `, ^) u$ S8 Z% n
2. 读程序:
0 |) n6 X. v/ I$ e0 [1. public class Test{
{# h7 Y# a1 P* C+ [9 L2. public static void main(String args[]){* J9 t5 P. v" E* p
3. char c = '\u0009';- k, ^3 Y; P6 m) ^9 L% v9 y
4. if( c == 0x0009L) W8 n' a4 _; j
5. System.out.println("Equal");- `" R$ I; \$ T" Q. n ~
6. else System.out.println("Not Equal");
& N9 h/ m" C1 d( c$ D7. }/ z) {0 y# j8 I2 I( t3 N' X8 A
8. }' v3 N) S2 r3 a, C& w2 n) S
当编译和运行该类时,将会发生什么:
: v& d! \9 ?2 n; m+ a, [A. 程序因为第4行将出现一个编译错误
8 V) s6 O" L8 R# X6 q5 VB. 程序因为第4行将出现一个运行错误' _, b# c5 T- u
C. 程序因为第3行将出现一个编译错
5 I+ O2 A6 A& @' N' s. p. OD. 程序输出”Equal", f$ \3 v5 G) |: \: k9 L% H
E. 程序输出"Not Equal”
( B9 |# m! x3 @5 d+ _) H2 k8 U3. 读程序:
. M0 R# S. {* d' T% `" N. q& _1. public class StrEq{
/ ` ~& Z* p+ I/ E' Y: |5 m+ k/ e2. private StrEq(){
7 v( ]# ?" U6 V3. String s = "Bob";9 v, l( R" f" I+ p2 q
4. String s2 = new String("Bob");
/ n* R9 _2 F' ?: s4 \/ F. r5. if(s == s2){
" s* R( w" }* ]2 ^2 I- U6. System.out.println("Equal");; ~" n# ?4 z6 g8 h6 u: s
7. }: N6 z5 e& C ~) B
8. else{; v6 A9 ~0 l# b. @
9. System.out.println("Not equal");
9 Y; @. e+ D/ u) K% O2 _3 S10. }$ Y2 Z' I/ m( n$ x( B
11. }
- m. l: ^. T) k+ I% e. h$ X12. public static void main(String args[]){
* s5 L0 c8 h& e2 j6 B' o13. StrEq s = new StrEq();
; Z: ~+ r. e: p6 g: U14. }+ J- u6 K) `0 u/ @$ {
15. }% ?; o) K A7 q# |$ w
当编译和运行该类时,将会发生什么:7 z7 D- R, T' h/ J7 ~
A. 程序能够编译,并且输出“Equal”
: c# }; {4 V& _( EB. 程序能够编译,并且输出“Not equal”
3 P, x* u4 w4 Z2 W1 GC. 程序因为第2行导致一个运行时错误% _% y+ `* X! V( P5 h0 s
D. 程序因为第2行导致一个编译错误! l+ M* @+ j( J# v$ f) M" h& L' A
4. 以下那个赋值是非法的:
; m0 B4 E! h- t3 G& X; F2 N' fA. long test = 012;
" n, c% ?# V1 [7 U+ sB. float b = -412;) Z* J* A$ L3 K5 d/ L! V( @
C. int other = (int )true;3 y; Q8 {/ I6 {- @" {- F' F P
D. double d = 0x12345678;
% }+ D+ c7 F. h' T# OE. short s = 10;
. W, N- e2 R/ S; m+ C8 i; p5. 读程序:/ P Q* u7 B. V+ d; j) A. w3 Z" r
1. class CompareString{/ r$ w j) Y4 X6 D
2. public static void main(String[] args) {; U/ w S2 U0 r. c1 Z
3. Integer x = new Integer(5);3 C9 f# d' I" N" p1 i+ s! o
4. Integer y = new Integer(5);8 a% x0 l4 q* R7 U: O& D
5. Integer z = x;
( V8 x$ D. [$ [- U0 r6. if ( ______ )2 e) Z A* A0 P4 ~# ?: n& r$ F/ ?
7. System.out.println("true");
6 a: E! U* I- g! _8. }
+ I# a: F. t$ O" b+ }9 P4 j# x% u9. }: x* |% `' E; } U) u) L- z; t' B
以下哪个选项填入第6行后,不能输出true:
5 p: j7 w8 _" |8 |A. x==z
3 A+ G N! |3 s2 \+ o* EB. x==y4 g4 |: Y' _' m6 t6 b
C. y.equals(z)* Q3 {# J4 @8 O- e1 C) |: s
D. x.equals(y)& r& @ ~0 {1 y" i2 E% d u
6. 读程序(双选):2 U6 O2 C6 U7 _% }0 Q
1. public class Declare {
+ M8 ^" g$ K8 q- A* P n" f2.
% `2 X/ H% ~0 y3. public static void main(String [] args) {
/ y. G4 f/ n3 x& W. C# J4.
& @5 V! g1 E" I" |0 j5. System.out.println("The variable is " + x);& e' D; ^. G9 _5 n6 G i) H
6. }
0 h' q9 H3 u4 B8 q3 B7. }
& f4 L. d+ w* Q0 v以下哪个选项中的代码可以使程序编译通过# y3 ?, J, F9 E" k8 K
A. 把“int x;”放在程序的第2行" ^7 b" [% B' ~5 t3 t/ z
B. 把“int x;”放在程序的第4行1 l" N! C2 g! X5 S% U: l
C. 把“int x=5;”放在程序的第2行
7 G2 ~1 d1 ]0 S2 f2 u# `% ?D. 把“int x=5;”放在程序的第4行3 ]8 ?! S( E4 a0 R4 J* N2 [! m n! ~
E. 把“static int x;”放在程序的第2行# ^' W/ g/ F% c4 {% H. g+ f
3 P& x q- m$ [( v5 u
7 {4 x3 P9 _4 Y9 J s$ O, {
6 p# g$ o/ Q9 E
7. 读程序(双选):
, n0 i# G k5 }2 {1. public class Child extends Parent{8 k! j( ]2 b' O* V6 T! ]% h0 X, h
2. int a, b, c;
& t5 j2 ]/ ~% Y% m2 M6 m5 G3. public Child(int x, int y) {4 w {. s% |/ ^
4. a = x; b = y;4 H3 s+ h* F& d% f9 N# x* U
5. }
" [6 b+ [$ S) Q! t, ?6. public Child(int x) {
1 r) T6 z9 @& T! k7. super(x);( q9 M/ r5 f! z, ^4 n
8. }
7 |3 v" }6 V2 L; ?2 e2 j% p1 Y9. }6 \4 z8 n& ?: n# ~5 K" Z# K; |
为了能使该类编译通过,下面那个构建器必须出现在父类中:& s8 S' T5 v* D, M2 x7 F9 H9 |
A. public Parent(int m,int n)3 `* k9 a$ T4 j1 o/ y9 x2 A; \
B.public Parent(int m)' r1 ^2 m+ {: Z6 L( u% L, q
C.public Parent()
2 ]* r4 j2 s$ a! ]" f8 JD. public Parent(int m,int n, int o)9 r' p# e9 p, ^1 I0 u; _% k
8. 读程序:
0 o& J" U$ b. A+ L3 e& O1. public class Derive extends Base{' f: h5 Q1 }: |+ Y2 p7 W$ |
2. public static void main(String argv[]){, o: [; D- t. v
3. Derive a = new Derive();
+ F1 n: z, V; H1 M" O1 @$ {4. a.method2();- n2 n$ N; x4 n7 I
5. }7 I" x4 F! v- R1 H3 o0 [2 B$ g
6. public void method1(){$ d# g7 G, d% O
7. System.out.println("method 1");
% Q( n8 Z) d4 r0 f8 ]) [' m8. } * D9 X% o- n2 Y5 K) C' w
9. public void method2(){
1 I( J; R# A( J10. method1(); : J0 ]+ `& C' q
11. }1 P$ z0 z/ ~4 h3 D
12. }
v( A2 S) v4 f13. abstract class Base{8 L1 M# s0 Y e4 p6 R4 @/ S' d: J
14. abstract public void method1();
( K3 ?4 S! n: c( v( W15. public void method3(){
6 @# a1 B- i7 c! V8 K16. System.out.println("method 3");
& [0 Y" K: v$ ]6 c$ F17. }" _; Q$ {7 J: }: E; E, j- \
}$ B9 E; b" B" f, {, @% W
当编译和运行该类时,将出现什么:
2 K; j- N' w# U1 bA. 程序输出“method 1”
+ O* h% x, T2 Y9 T& Y1 S- tB. 程序输出“method 3”& J0 T* C7 k8 m+ S
C. 程序因为第15行而出现编译错误1 E) H2 F' ~) M0 H, L3 |4 a
D. 程序因为第15行而出现运行错误
/ U: g2 S1 A0 L5 b" Z! ?' ~9. 以下哪个接口的定义是合法的:
/ h3 L# w3 _ \( z& g; n/ TA. public interface A {int a();}2 u4 P% y- A+ W% M2 `/ V
B. public interface B implements A {}3 t" }- y2 s2 m4 B
C. interface C {int a;}
- ^7 S o, O9 ]* i& X) p& GD. private interface D {}8 k0 R( ~: \: O9 ]) T1 W
10. 读程序:2 N4 }+ X6 }4 x4 Y4 {! j9 z' q' s
1. //File SuperClass.java
+ t3 g8 a5 o# x1 w) y2. package MyPackage;3 l& A7 j- Y4 r2 u0 `: `) U
3. class SuperClass{6 c! U/ o0 ^3 A, e$ _
4. void myMethod(){6 c7 I3 q5 T9 S a, F- z0 G! w0 K; j
5. System.out.println("SuperClass");4 A$ N( t$ @' g& m$ o
6. }
4 e) n/ B0 r6 `, ^( K4 C, b) \7. }8 L+ s% C6 i+ I4 \8 l8 k9 J
8. //File SubClass.java1 V! N8 W6 k1 {
9. public class SubClass extends SuperClass{2 j/ j' x3 X3 W+ a
10. public static void main(String[] args) {5 d2 l) ^1 m$ k$ I9 \
11. myMethod();4 W- F: m( Q' G' @: U; [9 _0 d" x W
12. }+ r6 V: r q- n$ v3 ]
13. }
+ @% a& ]6 J7 s当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:
7 }6 G% N5 l4 f8 `A. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”
Q; g: s$ L& \0 D1 nB. 两个类都回出现编译时错误
" a+ k/ R6 C* Y: I; R" m& EC. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误
- D8 e* |1 H. I, w' Q: W" f# QD. SuperClass.java可以编译通过,但SunClass会出现一个编译错误
7 ^( }( }3 f) u11. 读程序:5 ^3 k) q, q6 A$ P
1. public class Test{$ E; T7 K5 m5 ~7 A- t* j& ~- A
2. public static void main(String args[]){
$ Z7 {4 }5 g( i8 M" O3 ^3. int[] i = new int[5];" s8 C' u/ y$ n' ^+ C4 u
4. System.out.println(i[5]);
$ `; A' B5 j' ]0 J Z2 f& D, W5. }
) _; b! t# `$ T* Z; o6. }+ _- {8 n2 u0 Y' t% c# T7 u/ ]0 N
当编译和运行该类时,会:
% O) w5 @) `% D. i9 lA. 出现编译时错误8 k- }( v4 r' l/ f+ X
B. 出现运行时错误
6 L$ W/ M: P; C: U( qC. 输出0. v1 r$ f# N* D+ ~3 C. y" Y, M
D. 输出null
7 ?8 p; `+ I; ^% {2 W12. 以下哪个选项是正确的数组声明或初始化:
1 K; P$ W, a5 h! f' DA. Array sizes = new Array(4);
7 k( t, u+ J! L6 T+ `5 xB. double [] sizes = {1, 2, 3, 4};) d! T. X+ K! e/ N
C. int [4] sizes;
( l" X5 K( p9 u+ p0 J: BD. long sizes [] = new [4] long;# Y- m- g& ]. @. f. F+ p2 F r
13. 读程序:
- ~' T7 m% J( }5 k1 v1. public class Initialize{
}2 i( R1 A6 E, F2. int x = 200;, {' S1 U0 g6 d% U& y$ p
3. static {1 \$ ?0 G6 n! E. J1 F9 o0 E
4. int x = 100;* I7 H( ~* ] K" L3 ]
5. }
; b! x3 I6 H- p8 G1 X6. public static void main(String[] args){3 y8 N$ Y. Z; A5 m Y, I) ]
7. Initialize init = new Initialize();
! l: Q; s- b3 S* t4 F4 W9 x, z+ L8. System.out.println(init.x);
: H$ P; G5 i! y3 [9. }' q: U7 P# B9 N7 f7 m: k3 Y- F
10. }
^4 P( K5 B: Z/ Q3 Y4 q当编译和运行该类时: S- m) E* p/ o/ K; D; |
A. 程序因为第3行产生一个编译错误
5 ^' C, \) h( u a# K8 yB. 程序因为第4行产生一个运行时错误
1 q5 z! a% p$ a+ n% I/ [: E8 }C. 程序输出100! p+ D7 T: l; I3 @
D. 程序输出200
# E1 e( G# |' D# L14. 读程序:7 Z% j, A; q* J6 u0 n: {4 u0 J
1. public int abcd( char x) {
% c* `, B t K6 ~. Y9 C' e2. if ( x <= 'M' ) {
- ~$ A- [- p: v1 v- D3. if ( x == 'D')
) n- M% k! k8 ^; E! a1 G/ N4. return 2;
/ l: b& |6 N' r) I7 @5. return 1;; Q+ {4 h) T6 K. C) j
6. }7 I0 Z9 d# x% T/ h1 {
7. else if( x == 'S') return 3;6 k$ v/ c9 ]! t9 s8 b
8. else if( x == 'U') return 4;
: K& V. z' `* e9. return 0;
2 e: ^, F1 y. E" N: e2 Y10. }
: b- i3 V; u$ {# {3 X4 g9 w在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1* X2 M7 N! D. _
A.‘X’) a: V5 A: x0 `- U% m( M, G8 L! L+ r
B. ‘A’
- L) I. ?0 n$ A5 nC.‘D’
" s% h d D2 yD.‘Z’
/ O3 i, I& ?1 y4 \15. 读程序:
3 {/ A: x6 Q2 [' `% U1. class TryTest {6 B, ?" m: o: H
2. public static void main(String[] args) {
5 J1 v ^4 w) \" ?3 c# |/ m3. try{
6 C* d, a/ |$ I6 u% a1 J+ p4. System.exit(0);
0 x0 E5 F9 p& y4 j! a5. }3 I& f- R }% E
6. finally {. z4 D6 Z5 L1 I) T! l6 W" G/ p/ Z
7. System.out.println("Finally");# N$ D" W3 f; o1 D+ }$ {! r: V
8. }. U, c, `) [) d2 s
9. }
( H/ V, C! U! [( o* {9 h- C10. }* o: I& i$ P: o
当编译和运行该类时,将:8 b8 ^* d2 v2 t$ v
A.程序因为第4行出现编译错误
1 G3 v' x( X R1 F0 O6 tB.程序因为第4行出现运行时错误" |) h3 F8 V0 K9 V/ T1 ^
C.程序因为第6行出现编译错误: \4 G2 g1 ]& j& q9 n* P
D.程序因为第6行出现运行时错误
" t4 m; J! k# e4 @/ t& s4 UE.程序输出“Finally”
& V% [/ N/ D; x; Z1 ^* ]F.程序可以运行,但什么也不打印
! A- Z2 D+ S5 U2 y2 V- A/ j) k9 F1 n' j* w% N+ R8 B1 R
16. Given the following variable declarations: r l6 {/ |$ ]/ K9 I6 v$ L4 H
Boolean b1 = new Boolean(true);
, U, @1 V, o& T* a& ABoolean b2 = new Boolean(true);" A) i5 @. g' }+ t
Which expression is a legal Java expression that returns true?
, y! A/ j! }: a. ?2 KA. b1==b2
. h# r: X' t9 L1 ~. \% }B. b1.equals(b2): \9 q0 V, q' | `" |
C. b1&&b2
+ E* O! `7 t/ k- }3 KD. b1||b2
# \5 P) W: ~6 w; Z% x: g* C+ XE. b1&b2
6 `5 T; X8 X% q U* e& ?* _# K+ fF. b1|b2
" C- K6 R) P5 u17. Given:
0 y, A6 X" ~0 B% f3 j+ ]1. public class Employee {$ Y, C8 O+ P: ~' i+ _
2.
( x4 ^ {% @5 y2 z3. }
% I- M, J) |5 v. h/ [; R7 W4. - V) i5 _9 v. N9 E1 i) e$ e
5. class Manager extends Employee{4 P( e6 {$ u, U1 O
6. }8 y/ N5 i3 i Z9 A8 U- `! s
What is the relationship between Employee and the Manager?
& d( H' q. i/ L W7 E YA. has a
. D, l/ f0 O$ m3 n& G/ xB. is a1 Z4 Q& b1 w; [9 J
C. both has a and is a9 t: ~( \+ H9 }. Y& C8 j8 r# I! `, |
D. neither has a nor is a a9 t& _9 Z8 v, K% d
18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?- {! T- v, @9 I# Q7 R/ j M6 a
A. BorderLayout: W1 W1 S5 Q* b7 y
B. FlowLayout
4 \6 k7 Q, O4 O. L; c, M+ K( }C. CardLayout- g" r% f) \5 Y
D. GridLayout. q* X1 Z: Q/ } A
19. Which of the following AWT components can generate an ActionEvent?
5 E! Z! n ^+ k# O( ]. yA. java.awt.Button
* g G) r, r! q+ f% AB. java.awt.Panel
9 ^- }6 m/ i- M6 z0 r. ^% M0 ?9 g. P+ NC. java.awt.Canvas
+ q$ z+ h; U7 }D. java.awt.Scrollbar
: {1 \, `4 }3 x0 M4 P6 y20. What kind of Stream is the System.out object? g8 u! @# K& n, L$ P) k& V6 ]
A. java.io.PrintStream
, }' h* a6 s, Y/ \5 ?# }B. java.io.TerminalStream
$ u$ a( m' _4 @- v( l* wC. java.io.FileWriter
# P; u, R! {5 q. I- TD. java.io.FileWriter- D# x5 H% j @- p
21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)* p2 t1 e) {" o) U6 q% C9 ~
A. java.awt.event.FocusEvent
% F' P7 I& \2 O- S+ _B. java.awt.event.KeyEvent/ E) v! h; m& b
C. java.awt.event.ItemEvent
) c% t# b; u: F8 mD. java.awt.event.ActionEvent. D4 E T1 d4 L$ ]
22. Which of the following methods are static methods of the Thread class?(双选)
( j- o8 o; r1 A; [% hA. sleep(ling time)
- i6 u3 m5 x0 N8 cB. yield()
4 h- @7 L0 X3 M) R- K2 K) `9 kC. wait()/ {( x4 _' Z2 k# g, C/ ?/ z4 B6 H- u
D. notify()2 J6 S- @9 ?$ J
23. Which method do you have to define if you implement the Runnable interface?" R& w; f) m& W5 L+ z
A. run()
4 V l5 N1 H8 A: j* pB. runnable(), O/ s$ l0 n1 C' X5 M
C. start() w; _ j: H$ v# H. i, v
D. init()
0 c7 }7 r8 n( `- |5 w7 C' YE. main()+ v/ h4 ?) x+ v$ Y% U
24. Given:
- S! j+ z2 f' Y, h; k& j5 |1. class MyThread extends Thread {
}- p) ^, N5 T' H( \2. ! a+ K! U J& b: L* s! n7 k6 Q
3. public static void main(String [] args) {
0 Y% z8 p# t9 |" y4. MyThread t = new MyThread();. r& F$ l) d# k) F/ O6 }9 K5 j( I9 R
5. t.run();
$ _" Y% C* c7 r5 I( c3 m0 X: s) ?6. }
0 [! x2 {$ P* N" P+ J/ }2 p7.
/ O( ^0 r T% I3 y8. public void run() {
- k: d) G6 q5 d5 A! J) i [ p' K9. for(int i=1 ; i<3 ; ++i) {
) o7 r1 \) E4 `) C. L10. System.out.print(i + "..");
# T) q% ^; r9 j; B& v b11. }
3 s6 Q% {+ ], d12. }) J2 ?' A$ u, s( _0 ]5 C
13. }7 I9 ?4 J) G' o' ~9 i
What is the result of this code?C
7 b" ^ G9 l% c$ a$ N8 d3 Q( `A. This code will not compile due to line 4
2 G. h9 i) B2 W7 yB. This code will not compile due to line 5
% G( |8 |. Q# A' G: m4 B5 h9 LC. 1..2..* W+ _8 [6 ]) e* E! n
D. 1..2..3..- p: L% R) z1 _6 ?
25. Given a local inner class defined inside a method code block, which of the following statements is correct?! @& W6 v& F" {
A. Any variables declared locally within the method can be accessed by the inner class% }# j5 `4 _. ?0 e- G3 r( w$ X1 M
B. Only static variables in the enclosing class can be accessed by the inner class
; G1 I" |5 s# oC. Only local variables declared final can be accessed by the inner class* h( m `9 a+ X, Z7 \
D. Only static variables declared locally can be accessed by the inner class.
; S; w1 ^+ l9 S1 w! x5 M! p' ~/ G/ O6 v, a! Z' E
2 W5 v, s$ b8 z. o) o; }. J1 }+ O5 }' s1 ?6 ~
26.public class SychTest{
0 l- { O2 r" C! j3 P& M8 ~ private int x; , {( |! C0 W; [; a
private int y; ! ]' j8 K; X6 a' C" q4 j: Y. B0 t
public void setX(int i){ x=i;} 0 g9 l) T2 H6 ^( ?7 Q9 M- A! n
public void setY(int i){y=i;} 7 G8 s$ c: b r' T: g, m
public Synchronized void setXY(int i){
0 o0 b0 ?, d" l* t" } setX(i);
# N) f# V5 E& R& [! `2 U setY(i);
7 f% \9 T* {0 C" { }
! O/ [2 S9 _+ S! n1 t. e public Synchronized boolean check(){
$ |! c6 A" M* y return x!=y;
4 }7 x b3 p& {# A3 _+ G } " j2 b) o- f9 x9 ?7 K3 G
} + B$ T; U: L7 F* V4 m
Under which conditions will check() return true when called from a different class?
5 ?( q" ?& a6 C( Y& Y! t! [7 Y A.check() can never return true. " F5 b' h7 T3 b4 y' n
B.check() can return true when setXY is callled by multiple threads. " U% b- D _( [* i) G
C.check() can return true when multiple threads call setX and setY separately. * I3 G) [3 L+ ~2 w4 Q5 m) Z
D.check() can only return true if SychTest is changed allow x and y to be set separately. $ i( N9 G. a8 n; J5 J; H
, b N* b0 @4 e% V4 p/ H7 o3 E7 G) W
27. 1)public class X implements Runnable{
! x. H4 ^0 [0 z) n o, M+ G 2)private int x;
8 L; H3 E6 {' G# n+ R 3)private int y;
+ Q3 N/ j* }$ @' b; B$ Q: u2 k 4)public static void main(String[] args){ ' W6 m- D% Z" _
5) X that =new X();
$ ~/ n6 r1 o, w6 W 6) (new Thread(that)).start(); - \6 v! \% T: c* }* l$ g1 K6 `
7) (new Thread(that)).start();
+ w4 R5 J7 J G. ]: F }
3 x# d9 p4 Z/ u 9) public synchronized void run(){ - G+ o3 t) E8 \# h2 B
10) for(;;){ ( }7 I/ r/ R6 S F0 ~' L. O% F
11) x++;
! g8 `; f4 Q$ D J& z/ K3 p# d 12) Y++; 6 Z# I$ k2 ^( T( Y9 t. n
13) System.out.println("x="+x+",y="+y); + A& U- K! b+ n0 g( y, h
14) }
/ P( N% [$ Q$ }- c 15) } - O- P0 ~% h: R. g6 b0 R
16) }
" C1 K+ |( k7 S! G3 W what is the result? 5 G7 J7 K5 M% T2 O/ g# p; n
A.compile error at line 6 2 G9 R# V/ {, Z2 P7 }/ U# g2 _
B.the program prints pairs of values for x and y that are - p( _' K* \4 w# p; @
always the same on the same time + G4 z J; z% q
6 `4 c# |' b& ^" W
28.class A implements Runnable{
: x$ k" A, e# D) |8 L int i;
; ]2 `9 F0 ~. D2 S, _ public void run(){ 8 G1 U3 Z8 d4 k, a$ ?- c% n
try{
b3 l* g' [. z* A% w, t% D* l" Z Thread.sleep(5000); * r- f! \: b5 X$ \$ o
i=10; 6 X) I, p( N8 n& ~7 Y E
}catch(InterruptException e){}
- I8 d/ p4 J/ x) f. J% U }
- K* p, t: J+ g! _3 |$ X+ }$ Y } : n' Z! ^6 ^+ q% g
public static void main(String[] args){
4 Z" h9 n, i/ q6 L% i try{
( X4 `# `5 n. Y0 T% \* p A a=new A();
- N5 Y- y( F3 `# R7 i' i Thread t=new Thread(a);
1 C& A/ c% H5 r6 b t.start();
# L% y& P4 e! [8 v" X3 o 17) ; W0 l4 y3 B- F
int j=a.i;
' j+ \: f; d7 Q$ W$ ]1 _1 E; i7 ? 19)
' f# L5 p4 K; U+ p2 k4 n0 ~ }catch(Exception e){} " D0 R' M" A7 a, ~! z9 a
}
! w3 Z; P( u9 ?0 L! K, m }
# ?3 d7 Q6 l4 Q) h# E. b- A! Z1 J( s what be added at line line 17, ensure j=10 at line 19? " S* c |( L3 @9 I) X& R
A. a.wait(); B. t.wait(); C. t.join(); D.t.yield();
W9 l* n: l9 c+ ]; M& z/ j' f8 R8 ] E.t.notify(); F. a.notify(); G.t.interrupt();
8 d3 k- A& T$ v K* A, `2 E29.1)public class X{
( E5 ~. P( s: f* N' T( t/ p 2) public static void main(String[] args){
1 ^) \6 b1 N1 O! g$ Y 3) String foo="ABCDE";
) Z4 x2 e: m u 4) foo.substring(3); % M9 v( m, K& C
5) foo.concat("XYZ"); 7 u% t; D& p4 T7 g' r# V) ?+ l& ^
6) }
. j( o: `( _; @6 @/ A& U' E- S 7) } & ]& f+ e s3 ~- R& L
what is the value of foo at line 6? + b' }4 { z8 J) m2 l0 b
30. public class Test{
' @4 S) }8 P: z% `6 n- g6 w- Y- ^ public static void main(String[] args){ ( ]6 G$ ^& E3 j, B! @
StringBuffer a=new StringBuffer("A"); 7 J4 W+ I( ^ q/ s
StringBuffer b=new StringBuffer("B");
- }" o( |; `* T) j# ^ V# A operate(a,b); + [) o( M a5 T
System.out.pintln(a+","+b); \2 y; J4 D1 I
}
" {) w9 z% h# F* ?6 T public static void operate(StringBuffer x, StringBuffer y){
0 f) q3 [4 P) X& C x.append(y);
J9 d/ m9 S, c1 Z" d; p y=x; $ Y3 M6 K5 d, K$ j4 r- U
} 8 k1 ?3 R, M/ q$ X/ p |
}
' o, Z- N+ M2 g! _ what is the output? |