Sample TextJava培训考题
9 H8 v3 y" K, v1. 给出以下程序:
) V- N9 v. E# ~& q: r9 o g1. public class Colors {
: p5 _* Y6 n# s$ M2. public static void main(String args[]) {
3 Q! g. v6 _5 T8 B% k3. int n = 1;3 k" {5 M3 T5 ?9 E
4. System.out.println("The Color is " + args[n]);. p8 W2 ]8 Q7 ^2 L8 V2 l
5. }
) r' F) x1 }' ^- Q/ K6. }' Z- H8 j: p _/ U$ d P$ \2 \
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”: a' I6 f& \" T/ M, d8 {
A. Colors red green blue yellow, e! z6 R7 b) {
B. java Colors blue green red yellow5 J0 K, }7 E6 X M
C. java Colors green blue red yellow
$ }* M' k4 A: X6 S* l8 ID. java Colors.class blue green red yellow- a, q$ h- F$ J' K
E. java Colors.class green blue red yellow
! n( S: K" {8 z& q' r/ L2. 读程序:
" `! w" V9 t- l9 B, S- l) [1. public class Test{1 v2 C. {( z7 {" R5 ]3 W J
2. public static void main(String args[]){: F' ?: `' y4 Z4 K
3. char c = '\u0009';' p1 R; I5 l& U+ l9 o/ [
4. if( c == 0x0009L)% U- J; u/ v& ?) g0 P
5. System.out.println("Equal");* r& O8 |1 ?1 X1 _# G( k, \' X
6. else System.out.println("Not Equal");. ^& K1 D" q7 i6 Z
7. }" V( \0 U- _7 Q
8. }
% U7 c1 F* H+ g( E A* _. _% X当编译和运行该类时,将会发生什么:
, [( K: q$ ^7 k: j5 Q: f8 ^5 w' UA. 程序因为第4行将出现一个编译错误7 j/ Z0 b: I2 C# z$ e
B. 程序因为第4行将出现一个运行错误
; o$ o& e7 a" e4 sC. 程序因为第3行将出现一个编译错" L$ u( F2 G3 h' W
D. 程序输出”Equal"8 v/ P8 |9 I$ s! E9 [( M! s
E. 程序输出"Not Equal”
- \+ m: L$ T. N3. 读程序:) m. d& J1 e% t
1. public class StrEq{
( _' O8 } ]# V8 b3 e, o) U( Z5 Y2. private StrEq(){
* b$ Z% a4 [$ |" q8 b9 V0 e3. String s = "Bob";
- o+ v6 |& G5 h8 N$ U! v4. String s2 = new String("Bob");
) m3 Q% r% O1 r% L/ X- ^9 D5. if(s == s2){
" _( S# w! D8 x+ v2 u' H, O0 l6. System.out.println("Equal");
+ c% ~5 M( M5 [/ [7. }4 O6 ?+ n; j: Z( O
8. else{( N+ D9 ~6 t8 K/ ~: z8 W5 S _
9. System.out.println("Not equal");3 P7 ?; v& }/ x0 u* x( z
10. }
/ `6 F. f! H) h& R: q* ^% O11. }
; ?- w- i3 \5 n, d% _12. public static void main(String args[]){
. _7 ]; l5 i0 P2 k; F13. StrEq s = new StrEq();
1 h2 X" L/ d+ Y$ h+ B. }" B2 B14. }8 c' H) |' r, O: W( ]
15. }' W' O. Z: {, N: _3 L6 g
当编译和运行该类时,将会发生什么:( m8 M( w6 e6 B9 K( M9 X
A. 程序能够编译,并且输出“Equal”
3 G7 i/ d+ @9 F- R7 n/ XB. 程序能够编译,并且输出“Not equal”9 Y" l/ L& J* q: y. M% z+ j4 i; i
C. 程序因为第2行导致一个运行时错误1 W- J8 R7 E+ s! v* U) N
D. 程序因为第2行导致一个编译错误
% {4 A. t8 u6 x7 p& p4. 以下那个赋值是非法的:) D4 H/ q# ], B
A. long test = 012;1 D+ }' C. }" a# O
B. float b = -412;
7 ?; H5 Y, m" kC. int other = (int )true;
, @+ R0 M" p( s& {% f4 vD. double d = 0x12345678;: I. I* T7 I6 s# K/ l$ H: p9 n
E. short s = 10;
+ G" {5 q9 @: C6 K9 G7 i5. 读程序:; E; d1 o7 O2 Z4 H9 a) W3 n L
1. class CompareString{: j/ x0 c, T3 g' H
2. public static void main(String[] args) {
$ L5 d2 o3 U+ G- U2 r3. Integer x = new Integer(5);7 Q; B; ~1 L* M" i, G9 N
4. Integer y = new Integer(5);
, z; ^6 G$ ?. W5 O/ f5. Integer z = x;3 k' |" ^) @! L; R
6. if ( ______ )6 _; q) q' t5 H$ x: v6 E
7. System.out.println("true");
6 F% M. a% B1 u1 e4 ]7 M9 V8. }7 `7 X3 b$ H' _% s
9. }' o0 R4 R. Z4 W7 E' \7 L
以下哪个选项填入第6行后,不能输出true:
4 W o4 `7 \8 S8 n8 T8 KA. x==z
+ E M: q% G$ y7 C* [6 uB. x==y! l0 T( {. q( s% K% I& \
C. y.equals(z)/ {! o* r# N8 ]+ l j2 K# f
D. x.equals(y)
* _: w3 |7 J2 S) J6. 读程序(双选):0 M: O+ |7 l+ H M
1. public class Declare {" }2 ~6 a2 [1 T& M: a3 L u
2.# _" R7 r% r/ r* s1 T; V/ U
3. public static void main(String [] args) {
/ M( u2 _; a8 n4 Q, Q4.1 B4 ]$ d7 Q) H7 e" C
5. System.out.println("The variable is " + x);" M3 ?2 X: B* ~3 f7 C% M( a: ?2 m
6. } P! S+ V6 j; \+ z, B! l/ b+ w' v
7. }
- X& T$ ~, D3 l% R- p- k2 G, f以下哪个选项中的代码可以使程序编译通过0 E/ O/ E( n3 b% _
A. 把“int x;”放在程序的第2行
2 z2 q* q( _7 Y* g9 G1 GB. 把“int x;”放在程序的第4行% D/ H" m4 B8 g( Q# i& s
C. 把“int x=5;”放在程序的第2行
% F7 Y3 O6 Z8 J4 t+ S& q& }2 RD. 把“int x=5;”放在程序的第4行4 U0 u& @5 P: c. g' E$ t& z3 T
E. 把“static int x;”放在程序的第2行: I4 r6 {1 t; w% ?1 ?1 j9 i
5 T8 C# C( i/ d( ~
. k0 i* G- M: j- F! C
( ?% o# R4 q4 S+ E8 }! r/ X2 \7. 读程序(双选):' X5 V! ~4 K9 Y( e- F9 b; O
1. public class Child extends Parent{
) ^, A* W$ r' a+ W9 C2. int a, b, c;: ^0 ]; T# L. X- ? ~9 u, b
3. public Child(int x, int y) {
0 x p: u3 }3 |9 \- z& [4. a = x; b = y;9 c5 G8 a" I# d5 y0 f
5. }5 p4 P. i. W0 }1 u) l
6. public Child(int x) {. C: l$ F& a! u$ C* B5 X
7. super(x);1 N/ t; }' b9 b
8. }
# L! _& y# h7 J8 _3 Q g% g$ v9. }
0 _' ?+ h3 Y" _9 z为了能使该类编译通过,下面那个构建器必须出现在父类中:
$ X; z; V0 f) @7 oA. public Parent(int m,int n)% i% \3 ^/ B$ L% z2 L
B.public Parent(int m)
8 f; i' q: ^! |7 }C.public Parent()
2 |/ M" J- \& dD. public Parent(int m,int n, int o)
3 T+ R: K' q) e5 k# o. U/ z7 ]8. 读程序:8 f9 B1 B" ~" I/ B
1. public class Derive extends Base{- D, H$ v" q* q# V; J [
2. public static void main(String argv[]){; z' I8 V0 Y( l+ C
3. Derive a = new Derive();' A/ M' x8 B3 l) h9 [& p. y3 ^, M
4. a.method2();0 w& F9 O) a* ?# T8 G
5. }* N% v) z$ b4 h. P
6. public void method1(){
! G3 c- Z& t! z5 @3 f7. System.out.println("method 1");! Y4 z$ ]# a" Z9 `* S
8. }
% c9 V5 r) s V% D4 \ Q6 f, a: N9. public void method2(){0 D* _# [7 j' J. u+ K" O2 l1 e
10. method1();
. X0 ^, B$ f$ P2 ?% a7 o1 P11. }1 Y$ b R% G9 U5 R
12. }
5 E) V9 T7 d8 j13. abstract class Base{
' s" \3 `; T5 w: d w& ~ w) `' n14. abstract public void method1();. e/ F' k W1 z7 r/ ?' L
15. public void method3(){
" f' j9 H9 P4 x8 \% |16. System.out.println("method 3");
/ W+ w- W' I( K* K8 D; \17. }) W( j9 o- |& i, M1 v
}
7 z- d; V# p+ x8 G# O1 ^当编译和运行该类时,将出现什么:
4 ^# m+ q9 R; V! OA. 程序输出“method 1” z0 N3 }$ D, G" ?( C
B. 程序输出“method 3”
3 H, I5 D: z; u {C. 程序因为第15行而出现编译错误
* H- F& K8 i# c) ]0 M. ND. 程序因为第15行而出现运行错误
7 _& ?3 c/ |: y% P) t- Q! w4 i& }, {* W9. 以下哪个接口的定义是合法的:) {/ X7 M; g! v' r; e
A. public interface A {int a();}
8 S! J+ Y3 V" E( ~1 MB. public interface B implements A {}
! u7 `( T$ l0 RC. interface C {int a;}
. c7 |: @3 H8 \+ k9 c. F) S$ f0 B7 A0 UD. private interface D {}
4 p: y$ L0 F1 G* q: [10. 读程序:* `5 f- m- k( r9 {. O" I
1. //File SuperClass.java8 Y* O4 [- ^' J
2. package MyPackage;
2 ~" E+ Z6 Y" v7 u* F3. class SuperClass{. S% o8 u8 t# S g+ |
4. void myMethod(){& R; Y+ w9 M* g7 D0 U0 [
5. System.out.println("SuperClass");
3 m5 ?- D. t1 Z4 s i x ~) o6. }. @/ d2 I5 M8 o& m. [5 x' }
7. }' P5 s4 E3 w! k: ^+ N
8. //File SubClass.java
$ A* c5 p7 h, E9. public class SubClass extends SuperClass{
+ s' J% K1 Y6 S) v& b& v5 }) Q10. public static void main(String[] args) {
! \0 b J. j( C3 i11. myMethod();6 r: n1 f( o# s: d/ Y% q/ S; H
12. }
$ V; A' F2 X& M4 s13. }
$ {( J s5 w Z6 o当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:' s4 [5 e+ G7 |2 w, R6 K6 G
A. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”, P2 ^$ j1 i# v& o% _2 }
B. 两个类都回出现编译时错误$ D7 B9 ~. Y- n2 O
C. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误
# i- z5 ?0 M% a- k3 w/ [" P8 JD. SuperClass.java可以编译通过,但SunClass会出现一个编译错误5 v/ \0 O3 r7 G1 v% i
11. 读程序:& L' G; Y2 \+ @) s5 A! `; {3 h. V
1. public class Test{
) A) U1 o% X1 e2. public static void main(String args[]){
5 T) ?5 |) }' R+ \( A g) t3. int[] i = new int[5];2 H9 I; @) L% P1 [- G: E
4. System.out.println(i[5]);
' v5 J7 N$ c ^1 r: ?5. }
4 L+ ?( j1 N7 d K6. }. J/ a( ]3 T- e4 E6 j
当编译和运行该类时,会:
2 C6 s5 B# N0 n, PA. 出现编译时错误: T4 U6 T: l9 F; X: |7 u
B. 出现运行时错误
; e8 |5 ^1 W/ e& _# H( U1 F% {" l$ hC. 输出0 O$ q6 W7 V7 b; n
D. 输出null
' I8 \8 H: x5 V7 F" Q12. 以下哪个选项是正确的数组声明或初始化:2 `7 A/ S+ p/ p1 w! M+ o
A. Array sizes = new Array(4);. F- E2 C! q3 {9 U0 ]
B. double [] sizes = {1, 2, 3, 4};8 Q! s) E G0 D# j
C. int [4] sizes;) e# \+ p$ v+ a
D. long sizes [] = new [4] long;3 o7 w1 z6 d# {7 g# f t" M
13. 读程序:
) F1 K( q, {; M9 w: L1. public class Initialize{4 d+ ?4 w8 i6 q4 Z+ L
2. int x = 200;2 e+ g+ L5 D1 G" \
3. static {
3 a1 G( ~" L" G2 v* Y4. int x = 100;
4 c$ E6 H: i7 W; J# j3 M- y! n4 a) k5. }
. q$ D- f/ }: {' x6. public static void main(String[] args){
( _/ e. G: @+ X% j# i/ i7. Initialize init = new Initialize();: v: P" C+ e9 E
8. System.out.println(init.x);/ Z& Y. C9 T+ [- [) Y
9. }% P7 }' [, X4 z) h& ^1 b/ y5 e5 ~6 S
10. }# O: R& \% y3 C0 A, N
当编译和运行该类时:
% w& [1 X4 E, ^; k2 ~; lA. 程序因为第3行产生一个编译错误& \: i5 e# u; Y
B. 程序因为第4行产生一个运行时错误
/ y$ c. @4 s' j7 v7 S J- ~7 x1 O6 vC. 程序输出100
3 J/ u8 K v7 c, w' gD. 程序输出200% C" _- v/ N2 e/ e4 |
14. 读程序:! w4 s2 u5 H# { z& E) A
1. public int abcd( char x) {
% Q; m. G L8 Z0 W/ X' u: W( q7 h2. if ( x <= 'M' ) {
8 ]' B/ M/ a; M; O9 D3. if ( x == 'D')
4 v) m6 B" {2 [- H% W* E1 ]% Q4. return 2;
! p$ C- J9 [7 J# H4 p+ U5. return 1;7 U$ u% r; }& D/ _" X5 D
6. }
8 i2 L) N( B( g* K( C6 X7. else if( x == 'S') return 3;/ t M; i. x+ |# b4 M# i: X4 ]
8. else if( x == 'U') return 4;' n% T- x9 m5 ~5 N+ t& g* L% L5 z
9. return 0;
# {4 [0 U) } r! J# b" r10. }
/ \4 l$ g3 a% N' E% @0 P在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1* f M5 w; L, `6 M7 I m/ W4 t
A.‘X’6 P& j( j$ f- r b
B. ‘A’
; e! F6 X; e/ o& rC.‘D’( y2 W2 \& h. [: c7 t) v9 ^+ }
D.‘Z’+ X! s/ C/ t" \$ |1 k; j" F! @. E$ x
15. 读程序:( i9 D# c( O' w8 s4 e
1. class TryTest {4 k1 i- b) w; c" n$ i
2. public static void main(String[] args) {
2 D2 i9 X3 Y* }5 A; s6 L3. try{4 P; T% n3 t2 V0 C' K& I2 D
4. System.exit(0);1 Y( q- M$ V( J/ m
5. }
- t2 I L! u t3 X. l6. finally {3 [: u; A' N6 l. Z: k A
7. System.out.println("Finally");! b8 N6 y0 L5 y
8. }
; z( |# O6 f E" W9. }8 ~* G1 o- Z9 I+ J$ }( h6 r$ D L
10. }
4 A; e. _ ]9 e% l$ }. m2 r# l, A当编译和运行该类时,将:; b3 H( L3 J$ c
A.程序因为第4行出现编译错误, |9 H* _7 Y$ p" _2 n
B.程序因为第4行出现运行时错误
$ H( ?2 l: D- [6 r5 c5 B' Z2 GC.程序因为第6行出现编译错误) e& W) H }! _* U( i7 K) r' I
D.程序因为第6行出现运行时错误& y0 K& M9 }$ ^
E.程序输出“Finally”
5 \- v0 B$ K- {+ I. d" F4 L2 qF.程序可以运行,但什么也不打印
# b! N' }$ y5 q% H$ u, Y; L& l' C& h* a4 N. W
16. Given the following variable declarations:# M. \! z" y9 m- ^; `
Boolean b1 = new Boolean(true);
) P' \* u( ^ y; U! C" dBoolean b2 = new Boolean(true);
7 h7 P S4 O# B0 u% L2 K0 MWhich expression is a legal Java expression that returns true?
0 ?! t5 e- l$ D' X/ H4 O: E( ?' _A. b1==b2
9 X+ I M, S3 L" I) c' T a; YB. b1.equals(b2)
3 z( i M% V9 m8 G# f- i0 lC. b1&&b2
3 @6 S% D; k5 Y, K2 |6 tD. b1||b2
& c) d; R- Z- ^% \# p) ?E. b1&b2
0 ~( z+ z* I, R& j# RF. b1|b26 i3 \4 C. c* k* t
17. Given: 3 \$ Z! t$ ?. X8 e
1. public class Employee {0 D/ A! J# T0 W3 @( {% [! e7 L
2. % ?' S+ ^8 Q" y6 H& u; w$ @* q
3. }
+ G6 r4 w' a0 `' A- a( D, d4. ) I$ ~5 G6 p \; U5 y# ~
5. class Manager extends Employee{
+ z- z" P, Q3 n: m6. }
# W. s3 ?, h, y) y4 d: ]What is the relationship between Employee and the Manager?
! q& `. h0 G7 n% ~. TA. has a
, e# }& c/ m# cB. is a
T* P& F- B" a$ cC. both has a and is a0 |3 p. u, W7 D. U& D% G6 X! f
D. neither has a nor is a5 _$ _# N, ]1 |1 R8 L9 K. X1 `
18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?
- K6 s; c% [/ v1 uA. BorderLayout W$ G0 a3 x. i! J* I/ E
B. FlowLayout- \. b+ ^( [( r) M/ O
C. CardLayout4 G4 ]- u9 }* ~, w
D. GridLayout* |1 ]; j3 P% E: o" o3 S: g9 @4 n
19. Which of the following AWT components can generate an ActionEvent?
+ h2 T3 P% q$ t$ M) z: NA. java.awt.Button5 A$ f) c$ J( b6 x/ W# H
B. java.awt.Panel
- P q0 l# |4 i& y0 S5 ^8 \C. java.awt.Canvas
5 l6 M: t! p" M) J8 _0 U u! PD. java.awt.Scrollbar1 E- v# X4 y2 e/ R; H$ N @
20. What kind of Stream is the System.out object?
7 M- c3 I; A" j; v+ fA. java.io.PrintStream
) I$ ~* p* d4 T, ` W- ~% IB. java.io.TerminalStream" ~5 l; @4 ?6 |2 \: |4 N6 K
C. java.io.FileWriter3 [7 o/ J0 ~. o: r/ l9 R
D. java.io.FileWriter) j. Z+ S0 g" ?) i' U
21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)* D; N. v: h K' C# \, ^" H
A. java.awt.event.FocusEvent7 M) B g) y8 x/ d' p% o
B. java.awt.event.KeyEvent
3 [) J$ l) g$ A2 C( MC. java.awt.event.ItemEvent) g1 ~! z8 M3 G$ N( Y5 @9 K
D. java.awt.event.ActionEvent
- n! h6 U. I; H6 H22. Which of the following methods are static methods of the Thread class?(双选)
9 Q6 w! A- Q" l- W* OA. sleep(ling time)
' o) d0 o: v0 N+ rB. yield(). v/ g+ [: g. v2 i5 u
C. wait()4 z. y2 ^0 v/ P9 _; A6 V* }# J
D. notify()
5 W: g; f3 ]; _- K- `# i23. Which method do you have to define if you implement the Runnable interface?5 s8 I1 w+ z# O( T) R
A. run()% r6 O3 m$ Q. j4 B8 ]
B. runnable()) g' v& Y# t- `& {$ _
C. start()
7 V% P1 D* @! f1 g4 v1 |8 p ^* eD. init()9 b+ ]- L" z4 n! _% Z, Z7 ?$ [
E. main()
: J. S( a. ]+ I; q8 L, I" ?6 b6 n24. Given:
% l' w# s* |% i) ^1. class MyThread extends Thread {# Z" h! z7 [( u* X: k/ f* I
2.
8 h8 @$ F% Y# S; I9 @% u0 e$ g2 v6 z1 R3. public static void main(String [] args) {9 ^! n: A/ C a9 h$ j( _
4. MyThread t = new MyThread();2 B* `, I7 K' Q- `) ~2 N
5. t.run();" p, @& m u; P u* A' U* ?. C$ B/ H
6. }2 ?9 v3 C! h% S: k& x) U6 |
7. : [5 o% d! j7 ?5 L' R
8. public void run() {( _- A6 I& ]4 a0 w$ t! S7 ]8 ~
9. for(int i=1 ; i<3 ; ++i) {, M' t' j& ?/ U2 K& Y1 ]; ]
10. System.out.print(i + "..");, w$ X# x& y; w
11. }
& ]9 C' y# L9 n/ W12. }' u5 ^0 _0 o" v- X( E
13. }5 p p4 a' C4 e5 \3 Q
What is the result of this code?C
! O! s# |& P/ ~" lA. This code will not compile due to line 4
3 i( J- I, b) ]B. This code will not compile due to line 5
0 P) h" z" O/ cC. 1..2..
. Q- N3 ]: w7 t/ VD. 1..2..3..8 z1 H0 C3 z& U/ W* t
25. Given a local inner class defined inside a method code block, which of the following statements is correct?
3 f2 K3 ~) x, @- ?' m% F6 e" P, j9 PA. Any variables declared locally within the method can be accessed by the inner class$ N8 p1 C; } ^7 `
B. Only static variables in the enclosing class can be accessed by the inner class
! Y; _& H) i( n+ |C. Only local variables declared final can be accessed by the inner class5 |& I. [" }+ v ]" |0 W' E- `
D. Only static variables declared locally can be accessed by the inner class.6 i" \: i/ J/ W
1 @4 T7 ?% D Q6 G3 t0 W) ^+ m& W2 Y, n# x5 m. |8 |6 w
- I- a1 `# y7 r9 V W0 c
26.public class SychTest{
. z" c+ D% R+ U( F private int x;
! n) l& e+ x. U. t8 r) w private int y; " E }& x. k: g1 L7 l9 s
public void setX(int i){ x=i;} * V9 a3 z% |1 g- L0 b
public void setY(int i){y=i;}
5 j+ C& c! M" z! B& `) O" s" D public Synchronized void setXY(int i){
9 N& g+ c' z9 j* s( \3 t1 s5 i setX(i); . Z8 \5 d) K# }- {: R+ @" c
setY(i);
. u, a/ b$ x' r! r }
2 a8 m: B2 K; Y/ M6 t public Synchronized boolean check(){
$ `, W. y6 V/ ?. A3 U+ r) M1 O return x!=y;
( f) n1 X' H8 e7 b" L# p } 0 t1 Q* m' ~* w1 s
}
) S0 A' n: y0 G" S. T; O Under which conditions will check() return true when called from a different class? 4 ^0 g5 z' e W @" Y+ X
A.check() can never return true. # G' W; L6 n! i" d) @( M5 Y$ n
B.check() can return true when setXY is callled by multiple threads.
+ r* c+ L f0 ]9 G C.check() can return true when multiple threads call setX and setY separately. 5 i- c* g4 u1 y4 y: T
D.check() can only return true if SychTest is changed allow x and y to be set separately. 6 p3 Z6 f0 P9 S' B( g8 p
x* _. t- T7 `* x7 i9 t3 Y8 ~27. 1)public class X implements Runnable{
! _9 f% [2 B3 n! x6 P 2)private int x; + A6 h- _3 T+ X0 I
3)private int y; 0 I. [% v: i$ t1 _4 i+ h5 r0 g; z
4)public static void main(String[] args){ w, s$ S. O: v# |3 h! Z4 z
5) X that =new X();
5 U r0 y, B' Q, O$ W! B8 Z' x 6) (new Thread(that)).start(); + S% o: x, v) q" }5 r$ x
7) (new Thread(that)).start(); 7 f9 D7 q" v7 P% [ X0 f
} : n4 M- a9 q3 l ~4 X3 {9 Z/ q9 Q% c
9) public synchronized void run(){ % T* W0 E# |2 [0 h' s
10) for(;;){ $ F& J- }. |8 b; e
11) x++;
7 S& R$ f, W4 X0 [1 Z/ u/ l 12) Y++;
; `- K8 `; v; f4 d$ d! x5 M4 a 13) System.out.println("x="+x+",y="+y);
# k( o4 \0 C0 R3 W) U- ?7 q; [ 14) }
; j v: k: z/ j6 }9 Z4 C- z 15) }
3 R: ]4 X" B, v% f6 o, f% ~ 16) } ; \5 Q* H) U& q4 c1 o6 z' @5 a
what is the result?
; K% ]8 l3 K6 Y* [+ ?3 j A.compile error at line 6 $ Z/ Y3 _7 G+ W" C7 R- K
B.the program prints pairs of values for x and y that are
+ {; L" |4 v3 n0 y' P always the same on the same time
* J6 ]" E, e9 T4 F& E# F3 b
: m- r" ~; u4 v! ~28.class A implements Runnable{ 9 ^/ M+ ]5 r5 P' n) \* C9 w' o
int i;
& c8 X2 g( t( _* q% h5 ~; C- Q public void run(){ 3 h. M3 f* ?# d8 x6 W/ K
try{ . R5 H2 t D! G1 s/ v8 I4 J' V$ A
Thread.sleep(5000);
$ C; i; `8 X+ B7 q i=10; 4 Y9 e5 S j) {% v
}catch(InterruptException e){} 6 q3 C2 y/ u3 o. j
} ; F% Y2 L4 n+ E' A s) D7 {& ]
}
2 E: \, W6 z) z/ C public static void main(String[] args){ ) v( F8 ?( t) S+ q5 K$ B3 F
try{ 3 R1 x o; U* Y6 m/ i4 C
A a=new A();
# G& C9 u$ W2 b/ N% M' ^ Thread t=new Thread(a);
: ], }4 V: c" y4 W t.start();
$ Y( ^6 E2 I$ [7 C 17)
% K5 y% q% Q0 H3 T7 S& u int j=a.i;
! t" X. u1 M4 R F ~; ~ 19)
' i3 F$ G; a7 Q& j' z& ? }catch(Exception e){} , {4 ] x. G: ~7 y- I
}
( Y M# c/ Q! n0 j }
3 t) I( P" r* k! ?0 f% ~' F$ o what be added at line line 17, ensure j=10 at line 19? ; B, X+ F% x: D
A. a.wait(); B. t.wait(); C. t.join(); D.t.yield(); 2 @7 H; t& n" N( Z. L1 e; }) {* b5 D
E.t.notify(); F. a.notify(); G.t.interrupt(); 1 O D& w1 W$ f# W! |7 W( y
29.1)public class X{
0 `: A8 R! @2 V$ D3 q- Q 2) public static void main(String[] args){
6 G6 v! ]5 S+ i 3) String foo="ABCDE";
$ v" c4 h. B' y9 O 4) foo.substring(3); 6 T2 r6 [5 I: N ^% T# n6 Y/ x
5) foo.concat("XYZ"); 5 [" K- g/ z8 l# P6 m
6) }
6 }. j) y. V. P6 g( g1 F 7) } ) q; M: E; S5 K# q3 S+ N
what is the value of foo at line 6?
' h* P& d( `# a6 q30. public class Test{
3 U8 e4 f X3 }$ `# q0 e public static void main(String[] args){ 0 s- K4 G# K. n! |% o6 U/ g' E
StringBuffer a=new StringBuffer("A");
0 G; N4 t- [- a StringBuffer b=new StringBuffer("B"); $ o0 Z! B D- s! y" Q2 d. A, \# M
operate(a,b);
: @2 F* p' \5 C- E# {, f* x/ r System.out.pintln(a+","+b); ) E' q$ l. @: ^8 z9 d0 \ c( q
} 5 l' w7 ~' f5 v. ?* A- e5 n& i
public static void operate(StringBuffer x, StringBuffer y){
9 C/ D I! ~5 M x.append(y); ) W8 @& S6 f& p$ J; `' g1 r2 P, C
y=x; % H, ~) s" @; l
}
- I4 c8 m& d" q( y! R } 8 Y$ s1 y( T" Z4 s6 ~( T
what is the output? |