Sample TextJava培训考题7 W6 Z6 u) g6 P1 _: Z
1. 给出以下程序:
6 [# l* ]4 H9 r/ j6 V+ V1. public class Colors {: u" U: w. [$ U# A) y
2. public static void main(String args[]) {7 w9 P$ ]2 k6 E7 s* p" V8 {
3. int n = 1;$ m3 Q- w' b \4 b9 y8 C1 T% I
4. System.out.println("The Color is " + args[n]);
V+ f0 N1 n0 \5. }: t3 O7 g+ Q# E0 U4 p( g4 ^9 r
6. }, a4 m! a! S- |! i: U' k
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”' Z9 o. O, |6 V2 f9 A$ T6 v
A. Colors red green blue yellow3 e2 b7 R% t6 O' s0 H ~
B. java Colors blue green red yellow# J. `( u9 K6 E" r8 g5 o
C. java Colors green blue red yellow# _; n. W: `2 T$ k I. K
D. java Colors.class blue green red yellow5 I5 m7 U4 D b& U3 i
E. java Colors.class green blue red yellow
- l# W! t ^. h3 E5 J: P) N2. 读程序:1 u/ G4 \& G, w. P3 u' n) f3 w
1. public class Test{
+ z, A+ o) K$ X) R; m2. public static void main(String args[]){
# J" M7 C. e" H4 J- n$ a5 |; H6 B/ q( w3. char c = '\u0009';
) O1 m: a1 {4 ?3 I4 O4. if( c == 0x0009L)
( X/ h+ c; Z. C0 u4 \. H, l) }: _5. System.out.println("Equal");/ m# `! l+ ?) U5 A6 \+ Y- S
6. else System.out.println("Not Equal");
) \8 L$ @: p7 o# I4 E1 V. u+ d7. }1 w I: d" M, `+ N* y! j
8. }
( G$ f. y0 {8 |; V! `当编译和运行该类时,将会发生什么:& U2 k8 @! ^5 t1 y, |
A. 程序因为第4行将出现一个编译错误+ o1 f- r l; ~/ R$ }# x2 J/ ^
B. 程序因为第4行将出现一个运行错误
! r* Q& L, j. @) o' V; m# vC. 程序因为第3行将出现一个编译错
/ V5 q, R2 V9 T% ED. 程序输出”Equal"
* I' g8 E6 q( r' y2 Q1 x1 CE. 程序输出"Not Equal”
; c& }3 d! c3 @! [& h g3. 读程序:
3 z! H& R1 i# ?9 L1. public class StrEq{. k5 w+ o7 D6 w! h3 q' j9 [ _
2. private StrEq(){. ?6 U6 s, K5 D% v, [) W2 W
3. String s = "Bob";
4 m9 S9 ?" L, e0 r9 Y3 w6 a1 C4 Z4. String s2 = new String("Bob");
6 {, z# e- i5 I; @9 M, o, d" Z5. if(s == s2){
7 h# u6 H6 o3 e5 v |6. System.out.println("Equal");% \$ ]4 V5 |( X
7. }9 J$ k% Q3 ~: v5 U8 h) m
8. else{+ l7 T+ U* f& O" ]2 w0 l* j
9. System.out.println("Not equal");# B) u1 n. P3 `7 x
10. }5 A8 V3 C* a' D6 X" v- J! g
11. }5 g4 e3 D! r, a; M2 W% a& a+ k$ V$ k9 a
12. public static void main(String args[]){
8 O' ^9 Y) j5 M7 G8 W$ T13. StrEq s = new StrEq();3 j) b& X& n6 x5 J- L
14. }
! ^- k1 y% T& ?1 Y2 t. X15. }/ d- x5 d/ F) P6 C$ |6 T3 k) O
当编译和运行该类时,将会发生什么:5 [/ `) M ? B! |% ~2 Y/ K
A. 程序能够编译,并且输出“Equal”
D9 P4 G, ^$ @$ z, f% s$ u8 cB. 程序能够编译,并且输出“Not equal”1 C4 h" L6 O$ z; f7 O5 F/ N9 }
C. 程序因为第2行导致一个运行时错误- c# A# v; m# c/ L
D. 程序因为第2行导致一个编译错误6 f9 d/ T' U# ]! ^+ w& M
4. 以下那个赋值是非法的:7 |7 C$ E$ W: ?9 F7 }6 ^+ M( G
A. long test = 012;' ?( Z& F4 a/ e2 c1 t: z
B. float b = -412;5 B, |; p) I# k/ x
C. int other = (int )true;
: q* o* f( M& L% lD. double d = 0x12345678;) i* d8 O# s8 T6 e
E. short s = 10;9 U0 {8 c4 [" W, I0 S- d
5. 读程序:) J6 Z& d" M# G& Z9 Q0 w# K, G/ C9 j
1. class CompareString{2 T: f$ Q" E8 S
2. public static void main(String[] args) {3 f, z) H- ^* F. _" Z3 ]: R, B( Q6 [
3. Integer x = new Integer(5);
1 }) K8 J( |! ~1 X, E8 M% l: \' k4. Integer y = new Integer(5);
$ a# v6 n- O2 j( `3 U$ h5. Integer z = x;
I9 j$ U: \* A9 O6. if ( ______ )
( r: G ~( S: I! L# ]1 |# z7. System.out.println("true");
` t/ r& i( i* ~8. }
& _1 \/ O; t C+ T2 D6 q4 a# i9. }& d$ T' V7 s, g. W
以下哪个选项填入第6行后,不能输出true:' m$ v, |; ] ]& |4 E1 i- \
A. x==z
& Z! Y8 n, U" _3 O/ x. @- J5 K" |B. x==y$ F4 o" \1 K' A }/ U1 ?
C. y.equals(z)% b v i; E7 @- F. D" ~( a
D. x.equals(y)+ |/ d/ l8 v8 w- n) d/ c1 m9 z
6. 读程序(双选):7 G+ b5 n7 W* i2 A8 O
1. public class Declare {
1 x8 ~% Y b" P- E. i* o1 P( V/ w' c2.6 t7 d8 Z! C' Y* |) `
3. public static void main(String [] args) {
, H! `: T8 _8 |; V4.
" e+ _# S5 p# ]! W s4 F5. System.out.println("The variable is " + x);9 G3 M* w0 w" `9 K2 i9 X
6. }
# @! k8 r5 m9 f) }$ o7. }) f* W: A; `3 l4 O, X5 ~
以下哪个选项中的代码可以使程序编译通过. A. s2 e' M, @* Y2 C
A. 把“int x;”放在程序的第2行
; R. |" A" c% a. TB. 把“int x;”放在程序的第4行/ f7 A0 \ A& H+ Z
C. 把“int x=5;”放在程序的第2行0 W, I4 F! [( W# a% }1 z0 Q5 ~1 C
D. 把“int x=5;”放在程序的第4行
m+ s$ g0 f. x2 ZE. 把“static int x;”放在程序的第2行4 p- F$ h! X5 ?& N+ U \, \
- C4 u& q: ~" s
8 B/ z9 l* A* Z4 M! M y5 K6 X7 n2 e9 l) n/ A
7. 读程序(双选):
' n, Y# Q; F, h4 u2 ]# @% i6 W7 K6 v1. public class Child extends Parent{
# C, _/ ~: W2 M; H/ Z# v+ X2. int a, b, c;
" o7 v) k! q5 B" F; m& m3. public Child(int x, int y) {- s7 S n: H% o9 e, x; ?. {+ o9 \) R
4. a = x; b = y;
$ b7 y1 W( j" {- l1 Q5. }9 D D! k+ K+ F' H6 a1 p: N
6. public Child(int x) {
6 u8 S+ O* d Y* v+ ^# Z' ~$ N7. super(x);
f" M& @5 p/ m( F7 k* Q( C8. }
5 F: u, S& Q& t/ R8 N. L5 Z- u9. }9 _- A" V i6 ]7 Y. s( y' ` ^7 n
为了能使该类编译通过,下面那个构建器必须出现在父类中:
* W& q+ [, U6 Q1 i1 UA. public Parent(int m,int n)% k0 Q; Z4 | b
B.public Parent(int m)
9 t" J* X& ]4 ^! u) |C.public Parent()
& E1 G$ v4 M9 v# |D. public Parent(int m,int n, int o)
+ J7 w' P* x. d/ C9 o: [8. 读程序:
/ V! K4 b, D. F1 c1. public class Derive extends Base{4 g* @0 O6 K/ N' b: ]. h
2. public static void main(String argv[]){
* @9 ~1 B* f3 G3. Derive a = new Derive();4 B( @5 }' l4 A8 ~* j" J. [
4. a.method2();/ ^+ r/ k6 L: k, N. W( R, j
5. }# S& X5 x l; u7 \2 s
6. public void method1(){7 M; r% [" }/ y/ E* i6 W
7. System.out.println("method 1");
, {0 s! Q; S2 c# q! F) K2 M Z8. }
9 G- c: }5 ^2 r) m" N9. public void method2(){/ Y* d. B' u# L9 U8 p5 ?; q
10. method1();
$ K. p5 c' a+ q( \/ M11. }
) k/ Q' f& P# M. \. s8 d0 z1 I; @12. }
0 ?- i7 r5 M) D13. abstract class Base{
Z. z4 g8 _+ p9 z; z4 {14. abstract public void method1();$ ?/ z* G# K) D8 ?
15. public void method3(){
2 ^. c# W% V3 _7 r16. System.out.println("method 3");
; a; R8 g! W2 ]) H2 C17. }
; [3 a8 s6 M" a9 R( C5 W( c8 K# S' J}
- `! M! M3 j& U) z! K1 h5 z当编译和运行该类时,将出现什么:0 A7 f9 v5 B6 S# ?
A. 程序输出“method 1”
/ @9 t) b; W! y/ p- P8 r) W1 ^$ CB. 程序输出“method 3”% s _& W( s3 e
C. 程序因为第15行而出现编译错误
" r9 N- B3 N$ ^' e+ vD. 程序因为第15行而出现运行错误
! f" c% I- T8 d0 X, _% u: }2 b9. 以下哪个接口的定义是合法的:( p% u7 v/ F3 ?& W# ^& w
A. public interface A {int a();}
' {! w% V2 J7 T; z; l( z; [B. public interface B implements A {}$ o/ s) o9 g9 _$ }& I
C. interface C {int a;}0 b* z+ C: X; x
D. private interface D {}
z$ Z2 l( b& v+ ~# j10. 读程序: F, X. H& y+ t( @$ M. T
1. //File SuperClass.java
) L# t* K& O6 {" e, C2. package MyPackage;5 `2 Y* c1 _7 C2 M' {5 ]
3. class SuperClass{0 Y, A( ?* @# ?$ H' A! C) ~3 \! [
4. void myMethod(){
, l* j! m' E7 v9 t- n3 B1 L5. System.out.println("SuperClass");- ^" ?- }6 k g" y5 ~
6. }4 k. \7 l" H; ]) }
7. }: ^6 e4 @, k9 b
8. //File SubClass.java
0 T6 H! ? C. T- q' ?9. public class SubClass extends SuperClass{! l; d9 W4 _, S5 |5 n
10. public static void main(String[] args) {
* y3 T" |7 a; v% y$ q0 t: U4 \, Q11. myMethod();$ M& M+ {. C8 F \, U* o4 h' d
12. }9 g1 r* u5 ]+ }. K |0 x
13. }
0 t* c' {# R5 d2 E% k* V当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:
+ F% p% N# D4 p+ |+ D) n" v4 }$ gA. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”
+ H7 {2 l( l. \4 D6 \& ]B. 两个类都回出现编译时错误
9 X0 A- i& k0 vC. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误
. x& I' n6 c( Q! @9 J6 Q2 N% fD. SuperClass.java可以编译通过,但SunClass会出现一个编译错误0 W7 R3 C* B" [1 a) \. f+ e
11. 读程序:1 Q' x: _/ Y0 t$ S' r
1. public class Test{
$ ^" e' j! D6 y/ ?. C+ b( v3 e2. public static void main(String args[]){& f! R7 }$ ^# n* r% P
3. int[] i = new int[5];1 ]5 @% F% ]! z# b1 Z( U
4. System.out.println(i[5]);
1 ?( i/ u5 w" J5. }& N* |( ~, e: V; C2 _1 y2 a: M' f
6. }
* j3 v$ W: j2 \, g% S4 D. O当编译和运行该类时,会:
' i2 B$ h( y; U8 RA. 出现编译时错误
. W. P5 W! i# h" I3 GB. 出现运行时错误' P( n7 @0 i, B7 I9 G
C. 输出0
) Q* s/ ?0 n) l1 A- VD. 输出null
1 c- P4 B! b8 n" ^) D$ X& ]12. 以下哪个选项是正确的数组声明或初始化:; g8 A* V9 E' `: @5 |/ ^; l
A. Array sizes = new Array(4);4 G& B' N9 m* b) ^& r" ~: `
B. double [] sizes = {1, 2, 3, 4};6 |# K( @! Q$ ]3 q* p& x7 h: n% B
C. int [4] sizes;
$ L! C- R: P% w; X" ND. long sizes [] = new [4] long;
9 F3 P: x! L# x5 G2 o13. 读程序:
# |7 N" B8 U5 z# I1. public class Initialize{) R& |0 S: ]1 _' b; k
2. int x = 200;
% h: M; F7 J9 D8 C s% Q! O3 \3. static {
H& y! V: w5 b9 f7 Y: Z2 X4. int x = 100;
- z1 C4 S o& {: m' S5. }
( c4 S* z2 G, @ ?4 I* |: W/ A6. public static void main(String[] args){
& ]3 { P7 n9 s: X7. Initialize init = new Initialize();
" t& M7 @! ?+ u; X8. System.out.println(init.x);
8 B6 W; c0 ^1 J* l7 `# O0 m& y9. }
* I+ Z8 q) C8 e, L t10. }
+ B' {$ U1 C F7 Q! D" R9 p当编译和运行该类时:/ X3 d3 W! f! B- L" Z
A. 程序因为第3行产生一个编译错误
8 l' H3 U6 ^( {# Y' ~B. 程序因为第4行产生一个运行时错误
# P4 k6 b! t# @4 E! Z$ sC. 程序输出100
+ e: K6 |9 x/ j7 K3 a. AD. 程序输出200. Q/ I) k3 q: J. p
14. 读程序:
) S9 K3 H8 n& k. ]8 \2 Y. p1. public int abcd( char x) {
9 T ^& w2 d* E8 @: G( H5 b2. if ( x <= 'M' ) {
+ K/ P, _! \2 j/ v3. if ( x == 'D')
4 G0 ~, P Q& H3 d5 c% p4. return 2;
6 I0 j: s6 d8 P# K+ k5. return 1;
0 l u8 m3 Q( j% M6. }
3 |! G9 f9 V; i( B% O+ d/ s7. else if( x == 'S') return 3;% F: k# N; t. Z7 n8 p
8. else if( x == 'U') return 4; {* ]# `7 ]% r' j
9. return 0;
" W7 ~. m w7 r3 y) _1 X- a' P10. }& S* U+ J' [, |" Z. d0 r: U; a2 B6 w
在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1
! b7 x; b, h: ~) iA.‘X’
K) T( l! n8 z1 o" EB. ‘A’
# g( O1 e& R$ hC.‘D’
- k3 R7 L' b1 V- QD.‘Z’
& {: C9 t+ k: p15. 读程序:; h4 y8 W' |% \4 g1 U. k
1. class TryTest {
$ i9 _! {0 U8 R4 v2. public static void main(String[] args) {
# N* A5 M& h+ X; b3. try{
+ _3 z2 H( B9 j0 ^) h3 G. e4. System.exit(0);% ?3 J$ t) o, I' i; k. ?0 V
5. }& c1 A1 k9 ?. L* B! c% v u9 V
6. finally {
$ e+ g1 x" l8 R5 |7. System.out.println("Finally"); n& p4 G4 r$ F+ z9 W2 U3 M# Z
8. }4 U" o8 Z( b$ |) |+ y- p$ U
9. }
1 [6 f x' q& b6 f5 Y: h10. }
2 J$ ^( [( ^, v/ @% Q当编译和运行该类时,将:1 R, e! t7 d% Y6 w5 k. Q6 v
A.程序因为第4行出现编译错误3 p; k: X( P) G3 V- ?8 ?
B.程序因为第4行出现运行时错误2 H! w2 a; s( [, {( n. \" F
C.程序因为第6行出现编译错误2 i! I Q5 u0 V6 w$ f
D.程序因为第6行出现运行时错误) F9 Y! @9 Y5 ?& D+ A* Y
E.程序输出“Finally”
0 `, d. k/ K* m2 ^9 ^' W0 HF.程序可以运行,但什么也不打印
' d% {6 L0 K {; M* n/ T* v2 a& i4 Y- Y8 @( ]7 u2 @* l
16. Given the following variable declarations:# N8 S5 L, \* R+ N1 T
Boolean b1 = new Boolean(true);
* l/ E, k @% C7 JBoolean b2 = new Boolean(true);; c! Z% s' k$ E* M! f; O
Which expression is a legal Java expression that returns true? 4 K2 S& \; z8 Y3 J4 j
A. b1==b2
. c, F5 Q: G% g$ sB. b1.equals(b2)9 ~2 Y, F0 r* M1 d4 g
C. b1&&b2
# z9 D3 h+ ]7 x! O5 wD. b1||b27 P5 g5 Y# J0 R, T
E. b1&b2$ B+ T3 G8 }) u0 Q# `# k7 C
F. b1|b2
9 R5 m: X/ y% |5 `% ~9 w+ H" P9 n j17. Given:
! d+ ]/ u3 s5 a+ H1. public class Employee {# ?- P. N' s7 g6 ]5 A
2.
3 Y7 ?6 k1 k. D( Q3 j0 q3. }& x3 E$ A4 a4 n' Q$ y* j
4.
: `& n8 C; z7 Y0 q9 x& l2 L$ T5. class Manager extends Employee{
; s* v" ^% r2 h: Q4 `. h- G! e6. }
( {- }7 v* R7 |6 i; R7 n0 z3 yWhat is the relationship between Employee and the Manager?
4 l' l s/ |( L, HA. has a4 U$ ]/ M/ q" ~+ A+ C
B. is a+ X! n' @6 ?# F1 R
C. both has a and is a
! f" @ j) h- C+ \4 b$ w0 TD. neither has a nor is a
1 A3 ~: o2 Y# p( L18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?
N5 N9 v, Z% g$ b: r1 b. ]A. BorderLayout
/ d' D0 R* R" x Q$ |B. FlowLayout
% u$ B# Z: i2 }C. CardLayout/ ]. V5 \* m5 s4 V) U- D
D. GridLayout
9 u3 c4 H$ _2 K3 F19. Which of the following AWT components can generate an ActionEvent?: @0 l6 C A( I, Y
A. java.awt.Button
: x% ?" W: l/ ]4 o) o$ N7 RB. java.awt.Panel
# N6 g: ` N! j( N" t; b3 h" r, Z& v% r6 [9 eC. java.awt.Canvas$ y8 `" u4 D; L" k! x j: B
D. java.awt.Scrollbar
$ F" k9 A' N% [0 c20. What kind of Stream is the System.out object?" [& i) ?2 p* L0 F2 \( H" Q$ }
A. java.io.PrintStream
/ |7 n6 s/ G" f9 w" UB. java.io.TerminalStream8 W5 [$ j7 k4 v- p) ~5 b
C. java.io.FileWriter
6 t3 _& k8 e. s0 T2 ~' \9 r, qD. java.io.FileWriter
1 s8 m/ b- m7 y+ H, o21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)* m% t6 x/ o9 A# e+ [, d+ _- `
A. java.awt.event.FocusEvent2 U* A1 |6 \8 L- r3 g) g1 D5 \! u' M
B. java.awt.event.KeyEvent q( I+ x4 A: f
C. java.awt.event.ItemEvent
- z1 ?& `) _% W8 B8 ~: sD. java.awt.event.ActionEvent# a: F7 y, u3 o; b; H
22. Which of the following methods are static methods of the Thread class?(双选)
& `1 `5 `3 J8 z1 _! R! _ fA. sleep(ling time)2 O! x0 y6 b# p0 s# i
B. yield()
; g8 y$ l9 _5 F$ b3 IC. wait()5 K) m, u: d B& B
D. notify()
, E; d, v' W! u23. Which method do you have to define if you implement the Runnable interface?# R# a- s Y! @" l3 ] {
A. run()
( p3 C0 t, l/ X( PB. runnable()
0 V6 V" q0 L; {2 _7 G# e+ UC. start()
# K" d) L- b" `) KD. init()
7 o$ M3 P% w7 o P! X; N4 t# KE. main()1 K" U# A( \0 F) t7 n
24. Given:. y& Y% K; B; |: L7 q" r; p) G$ ]$ l
1. class MyThread extends Thread {
2 y& L' P$ T# V5 W! x. E) }2.
$ T. j+ t$ a& E4 d0 W3 U3. public static void main(String [] args) {4 L6 j6 s" q- s
4. MyThread t = new MyThread();
/ i; N2 W v: k" [. C5 h% J+ `5. t.run();& N Z0 e: F6 k, {1 l2 H
6. }$ Q4 M8 U& Z6 p) {
7.
9 l9 {* r5 y. v5 B$ x, @8. public void run() {( |- a9 v; ]3 _# J; d/ c$ v# t
9. for(int i=1 ; i<3 ; ++i) {6 ?- q9 j( E4 p4 l4 c% s
10. System.out.print(i + "..");) P9 q' b/ O3 F' Y
11. }
! m5 V- o* I3 J3 w v) F4 ~12. }: [4 k) |( R/ H% W5 P% z
13. }
, W4 E% u# l! T! J' O7 rWhat is the result of this code?C
5 M- ]) n3 E) J% D9 `; S: QA. This code will not compile due to line 48 s; I R- p9 n% Q& p
B. This code will not compile due to line 5" `. x* X2 {3 [1 S
C. 1..2..
( O X/ F5 D2 c3 }% l4 yD. 1..2..3..
& |4 U8 n* j- Y+ y25. Given a local inner class defined inside a method code block, which of the following statements is correct?
$ n$ S4 ?5 O. r+ B( kA. Any variables declared locally within the method can be accessed by the inner class, y3 J' F `% N0 E/ G+ }
B. Only static variables in the enclosing class can be accessed by the inner class
3 S8 v3 g+ v( j3 W* u7 QC. Only local variables declared final can be accessed by the inner class# a& V; P- \8 Q. U* g
D. Only static variables declared locally can be accessed by the inner class.
3 {' U6 r5 M3 |' p* I# B" b! U6 E- g
6 ]5 U2 C1 T2 }( \1 y! f! I6 ?
& ~% o. g5 H9 ?: ~% x
26.public class SychTest{ + H6 c6 I. X! n% y: T1 c$ ]
private int x; & M; u% q- Q/ H% U) X
private int y;
5 ?3 ]; e6 ?/ a; O public void setX(int i){ x=i;}
7 w7 s- z+ a' _5 n) [ public void setY(int i){y=i;}
1 ]6 u' ^. {0 U' z public Synchronized void setXY(int i){
+ v' Q% f& [- p8 @# ?3 b0 x. f setX(i); 3 F4 {: k! d, ]4 m& t3 S+ F' z' r
setY(i); 6 P0 W6 X/ I4 k; R
} % @7 W( F( a- O; g3 b8 J3 n- Z
public Synchronized boolean check(){
5 V& ^9 z0 j1 X( D. i( @- k return x!=y;
$ p2 s) F8 ~6 ^+ s, ^* R } 5 C9 J7 K/ K6 `3 ]& }' z q
}
* M, k# o; {5 C/ r Under which conditions will check() return true when called from a different class? 1 Q" A3 h& x# N1 |
A.check() can never return true. % J, m' Z* \9 a8 [7 N
B.check() can return true when setXY is callled by multiple threads. # S0 q( S3 F- ], |, U
C.check() can return true when multiple threads call setX and setY separately. & [7 u0 Q. \- k. n- W# b3 C# _
D.check() can only return true if SychTest is changed allow x and y to be set separately.
% K- l T6 W3 t: h) ~- D5 x5 I5 g% {2 ?* l, c
27. 1)public class X implements Runnable{ % u9 ~3 l! n( y# m7 j
2)private int x; 8 `+ z' x) ^) _. @3 \
3)private int y;
! W; Z/ o% q1 C2 r 4)public static void main(String[] args){ & Y& @, Q/ G2 B( M# l% p
5) X that =new X(); % v# `8 u) O5 J7 G
6) (new Thread(that)).start(); ! i# H+ {! X* h5 b: `
7) (new Thread(that)).start();
' q+ w9 E, H6 s6 {) m }
7 m9 H. f7 R7 j 9) public synchronized void run(){
, \' O# a+ K4 C: g 10) for(;;){
$ ~8 G2 L. M. Z* F 11) x++; 3 Y! ?, o- [# v( c5 S- X; V( |
12) Y++; / q; ]0 Y$ B9 i1 j& b; l4 X. w
13) System.out.println("x="+x+",y="+y); 1 S* o4 z6 H; v7 a9 U0 j! X$ y! i
14) }
- G! H. D; Q M g2 }( _ 15) }
; u, d2 N2 M6 T% U6 N 16) }
, L. S5 Q0 k0 m/ O, y what is the result? , c0 r1 @/ X+ _
A.compile error at line 6 1 s" d2 y- Z" ^
B.the program prints pairs of values for x and y that are
0 D& d9 E- |' B( m2 O always the same on the same time
" B0 z3 `+ l1 a; u
6 w. B4 g' m& t5 S5 I: \- s5 T' [28.class A implements Runnable{ - Z6 h( d( \% W. D0 Z& V. \5 H6 C
int i;
. W0 {+ x1 s8 j1 t, S public void run(){
$ I' j) D( u8 P, B0 g: m+ q" H try{ & U5 g( k' t$ T% z
Thread.sleep(5000); 1 x4 t; v( o6 u% B
i=10; ; g% Q* L; K& y. P' |- D% `
}catch(InterruptException e){}
7 ]0 E k0 \" j }
5 q$ X2 j M: |- [- w }
7 j y8 Z0 {: l( i2 d' F: J public static void main(String[] args){
3 t6 n5 {4 \2 p+ ~( U try{
& L8 C* u: Q6 p. |+ k5 T8 y A a=new A(); 5 ~3 _1 t5 N9 g- n$ F5 |( D4 }
Thread t=new Thread(a); A) A' F! W! n* w
t.start(); . X5 d: F0 |; D& k, U! O
17)
, t; g' Z+ D( u3 I int j=a.i; ( e& |0 D; Y) s# h# P u# S
19)
4 G- s! D8 x7 P. X% s }catch(Exception e){} / C0 ?8 P( B$ z% H/ q8 H! K
}
5 X! C& O' S; b% k5 B( _ } 3 W# X7 n: S) F( Y0 T
what be added at line line 17, ensure j=10 at line 19?
3 b. P8 o: Z: j( a A. a.wait(); B. t.wait(); C. t.join(); D.t.yield();
7 z! B& k& i% t2 Z E.t.notify(); F. a.notify(); G.t.interrupt();
+ x8 g# t4 ~0 z' o. T) c' y29.1)public class X{ ; r3 Q5 Q" [, n3 d7 V3 Y) i
2) public static void main(String[] args){
# C& m+ G+ X' C. f+ H 3) String foo="ABCDE"; ( ~" a) g1 C! b+ A; F* B
4) foo.substring(3); . |% Y3 ^9 i% J9 f0 T9 f
5) foo.concat("XYZ");
, B* k; t( h A) H4 A7 b& T/ q 6) } 2 R, D6 C& \3 j( K; e; W/ }
7) }
( Y/ o9 c: e" D& T2 p: d. p& J5 d what is the value of foo at line 6? $ M4 k" n, Z: Q$ ~* ~; S2 P. |
30. public class Test{ ( e8 _* J) O" o7 n: J+ I M
public static void main(String[] args){ 4 ^9 R% b% L3 x, o- Z1 D6 x) o% K1 n
StringBuffer a=new StringBuffer("A");
" L( s, q: U, |' s; f StringBuffer b=new StringBuffer("B"); . l6 P- V% s7 I
operate(a,b); e; i# S& N0 C
System.out.pintln(a+","+b);
6 _; {- t N2 _6 e$ ]7 C } , a N w8 \* P. a
public static void operate(StringBuffer x, StringBuffer y){ ! @5 v( I2 v' S& ^. Y$ ~) U
x.append(y); 2 S6 f+ @9 y0 _ s- `, a- `
y=x; 6 [; a! t* A5 y) {$ {2 a* Q1 m; t$ ]2 l% h
} ; u# x& W* X0 |3 F1 n
} " {1 c Z4 g `3 R0 t h3 Z
what is the output? |