Sample TextJava培训考题4 f+ s" l+ R: |: h
1. 给出以下程序:# z7 j* y6 L; |! W" ?: e
1. public class Colors { k+ {* G, C; S
2. public static void main(String args[]) {
L* X* Z/ M( I- p6 {3. int n = 1;( q3 m- B! }( j( W
4. System.out.println("The Color is " + args[n]);0 e7 P& E) e. {
5. }
; b& X2 A1 A$ L$ K9 i$ e* ?6. }
; o9 x: r6 V+ S# t. @0 N# k假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”% d, V" v9 q* M* B; q& ]
A. Colors red green blue yellow
3 \9 ~ K# g6 m! s% ^B. java Colors blue green red yellow
) D8 ` }( s' sC. java Colors green blue red yellow* f7 S2 W5 i3 e( r5 F0 J
D. java Colors.class blue green red yellow2 W! C5 C M6 J0 {
E. java Colors.class green blue red yellow! G. b# V8 V) c5 N+ ~
2. 读程序:7 @# T# z8 g3 X9 A4 M
1. public class Test{
k2 x% P4 Z9 X- L2. public static void main(String args[]){
% q I, P8 X% U1 E% v3. char c = '\u0009';/ X$ p* F4 [: O* R' `' _
4. if( c == 0x0009L)- b+ k2 B1 t3 G) M5 K/ }
5. System.out.println("Equal");$ c0 P1 M: R- s7 Y( `7 s7 o7 H
6. else System.out.println("Not Equal");; e. t- b! P/ p1 \+ E3 e
7. }
! B. p$ X1 o$ n f! p- N0 S8. }
8 G0 b$ b5 ^' e' b5 }# J3 v3 k当编译和运行该类时,将会发生什么:
$ Q8 W" C& U9 J1 mA. 程序因为第4行将出现一个编译错误/ q+ c& k$ U1 R; q' u# ~
B. 程序因为第4行将出现一个运行错误
. j/ u) y" m: I4 vC. 程序因为第3行将出现一个编译错
, v" v, Y/ j. ~9 n6 Z( T( e% SD. 程序输出”Equal"
* l' O2 g4 ^+ m9 k0 KE. 程序输出"Not Equal”; k- _3 \- P3 D$ k2 b. M# [, g
3. 读程序:
3 s9 W! u! J# b3 h" ], N1. public class StrEq{
/ l4 v: E3 L5 j8 g$ |2. private StrEq(){4 f' o$ i) w; B
3. String s = "Bob";4 C+ e8 D+ O7 l% S* b% ?# U* L$ N
4. String s2 = new String("Bob");
% d# j z H, a9 p8 b5. if(s == s2){
) t6 o+ l+ |- D) O6. System.out.println("Equal");* A! D# b/ I5 Y" E9 C6 l6 `2 u/ ?
7. }1 ~4 e2 F, ]& t0 u
8. else{
9 Q2 T3 p" `3 L" B9 U9. System.out.println("Not equal");
5 T4 i" w* l0 Z: J) b6 I( g10. }
1 O! x2 H4 {# E' ^+ q11. }
% m D+ Y7 u( d a0 u( ?. y12. public static void main(String args[]){
0 Q2 {' V: M' a, {1 R* a13. StrEq s = new StrEq();3 V4 X5 U& Y( S+ m. ?$ @+ q3 R0 s
14. }! Q$ P" T& y& N, q+ z
15. }
; Z+ B, N2 H) b当编译和运行该类时,将会发生什么:
5 R4 ^+ h( T3 O# OA. 程序能够编译,并且输出“Equal”- k4 n( {' b/ P H0 n4 _
B. 程序能够编译,并且输出“Not equal”
5 d2 a5 x d4 O: \' d' ]C. 程序因为第2行导致一个运行时错误2 t$ B8 v% @- W' g8 L. ?% G
D. 程序因为第2行导致一个编译错误3 B' a/ s# |8 S; x* R+ T, S
4. 以下那个赋值是非法的:
2 z: I* I6 n# p }4 `0 fA. long test = 012;
5 e$ K+ f( M$ ~# VB. float b = -412;
" m; R3 g* Q% E; |' d+ X1 jC. int other = (int )true;
& D" |1 U( q1 ^6 TD. double d = 0x12345678;
M3 I) r; r. }, y6 K) AE. short s = 10;
9 v2 }2 u; N# `' o& c5. 读程序:" x" W. r1 X; s: r9 F
1. class CompareString{ ~0 s1 R- B1 ~
2. public static void main(String[] args) {# F) O" g8 ]5 [- i' p
3. Integer x = new Integer(5);/ H6 M) |8 I/ t! T/ Q" A
4. Integer y = new Integer(5);
% i7 @6 [, V- K$ ?5. Integer z = x;
9 s0 _3 Y+ K3 [2 S6. if ( ______ )
, v' ~. G d5 K& K% U7. System.out.println("true");% q1 G- _6 v# S8 H3 s
8. }4 g- C! W6 x0 z: w e& h
9. }# f7 \0 O: M/ S. H* W$ A5 L/ J
以下哪个选项填入第6行后,不能输出true:
# ^; U; K0 {+ o4 _2 s5 e& iA. x==z
7 P) B' [/ r, M/ g7 W Z% v/ FB. x==y
1 x6 b) z& c8 [C. y.equals(z)
: g! e8 Z$ f* p+ m; bD. x.equals(y)
5 b+ d" ]2 g* l9 [% {! X1 c' E+ v6. 读程序(双选):
- \; v2 H2 K4 Z6 I5 \1. public class Declare {
! J4 [1 E9 F1 u4 c8 U9 A6 d2.5 B; G" z% b+ V8 e) A- G4 b
3. public static void main(String [] args) {
1 b4 g( _- [) @4.
: ?1 }" m6 ~( N' S9 C0 z A8 x5. System.out.println("The variable is " + x);
& n* R/ ^2 H" W! V x! c4 S6. }
" m: [( n0 ]- ]) P& r5 y% Q" K! S7. }
" L# a( P4 E% J# |. Y! I( k以下哪个选项中的代码可以使程序编译通过/ U) S) `0 C3 e5 y' }* Z: f" g
A. 把“int x;”放在程序的第2行
. O8 C+ F" e! e% _B. 把“int x;”放在程序的第4行" O! r/ F7 H+ O# l8 I
C. 把“int x=5;”放在程序的第2行
- D" |9 g, }& S% F$ RD. 把“int x=5;”放在程序的第4行' Y$ A) ?+ F5 J& [ k) @
E. 把“static int x;”放在程序的第2行9 B& g! I$ p2 Q% q
* V5 z; j& [/ [/ M3 V
* [9 U5 K+ Z; F( S$ _9 N4 A% W& \5 g3 i
7. 读程序(双选):
, N; K, M, g# A4 {0 n' B1. public class Child extends Parent{! u( U ?) k& a7 ?1 D8 F
2. int a, b, c;
$ [ h6 T! p3 u6 N2 ]3. public Child(int x, int y) {9 J: G; c/ X. y5 ]) S
4. a = x; b = y;
: t- M' @2 C; M) m: |5 g* n6 }- [5. }
" C* r: A; A$ ~* u6. public Child(int x) {# s+ B- o4 {( B% e" n
7. super(x);1 R9 Q# o4 K' L4 P- i: B/ w3 ]
8. }
+ v7 o+ h8 i2 S3 I' Y9. }4 u8 i. @* c' S
为了能使该类编译通过,下面那个构建器必须出现在父类中:4 E! Y* U7 |2 r5 c) w" e
A. public Parent(int m,int n)0 R1 M }8 q' E+ `! S, u" [7 h0 M; O3 X
B.public Parent(int m)
7 B! y, s* `' J1 ^4 v, O* v cC.public Parent()
% Y N, n! G% ~6 w5 l3 kD. public Parent(int m,int n, int o)
( |8 D {$ r5 Z) }8. 读程序:
$ c) a3 z# M+ H7 V% W! H5 V6 S9 C! W Q1. public class Derive extends Base{- K( o6 b( p, o7 N
2. public static void main(String argv[]){
# [" y( \+ q: n& j$ W3. Derive a = new Derive();9 m0 z( A9 b* r5 q
4. a.method2();9 @* z( g9 |$ V! b
5. }
4 i+ Y; c; y+ L9 G! F; {, Q1 H2 d( f6. public void method1(){; O8 {$ Q+ C- ^3 z3 a# t$ h
7. System.out.println("method 1");6 ^( E; M% L+ [: _- B
8. } & I# }, f; L- J( d! |0 c1 E
9. public void method2(){, O5 g" I' Q: a
10. method1();
/ ^' Z" t& j& X% i. N. P11. }+ L6 O- f, W0 n' r$ ^
12. }
$ a' u& w$ C- _: {13. abstract class Base{
2 P% ~9 n, {$ o/ Q, \14. abstract public void method1();
* k3 f" n( \$ t+ l$ N' S! o15. public void method3(){" M, v! `9 N* N% ?8 a6 d0 x
16. System.out.println("method 3");
5 M: W% L0 b i3 y. K" ?/ u( w17. }8 G9 d1 b3 r: W( W6 q2 R
}
|8 n' `8 u, }& W# b' R. K当编译和运行该类时,将出现什么:
' d0 j# D/ c8 t5 v/ R! R" aA. 程序输出“method 1”! y; p6 g) E$ e& Q
B. 程序输出“method 3”5 |) @' P/ H1 l) i2 x' K4 x, m: c8 k
C. 程序因为第15行而出现编译错误! W( U9 ?: Y, l# q8 t; b
D. 程序因为第15行而出现运行错误8 Q' ]8 v" M/ F7 B
9. 以下哪个接口的定义是合法的:
8 N* L( Y5 \8 F- A, DA. public interface A {int a();}
4 s% D' }, I P$ w. O: U5 x: y, AB. public interface B implements A {}( B9 D; h4 F+ w0 O
C. interface C {int a;}% f, C6 `2 f4 @/ C. e# u
D. private interface D {}
3 J, m9 s6 |. d( ~4 }8 c10. 读程序:
. m5 r/ c- @ F* A; N) y1. //File SuperClass.java0 V2 e; J. d- |. l- f: {
2. package MyPackage;, g! \' `9 Q1 S3 D O
3. class SuperClass{
4 o* Q, N2 l& T' q) ?' U$ Z" U+ p7 B4. void myMethod(){! ]& j4 q. S) C0 N7 h
5. System.out.println("SuperClass");
2 M; y- Y- @+ e( i5 X$ l5 C6. }8 k9 R4 X n$ F' ?6 E3 ~" u2 J
7. }4 h% Y2 @* j0 V3 F9 P
8. //File SubClass.java
$ R, G3 H6 g" V1 R9 F0 y. s- V. r9. public class SubClass extends SuperClass{
& C! e1 @* [( V* t' q6 U10. public static void main(String[] args) {5 m( _6 P, z( N! `
11. myMethod();
* g" u* a3 x; I- v8 w' I12. }
) ^, A; q1 J0 Y; K8 }' u13. }
. [( C# }+ X& ^ ?/ O2 ]" E! P# ]当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:
+ J8 L! N- d- q4 q" M# x8 `A. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”
1 j; S+ S( w1 _1 J. Q! l4 [B. 两个类都回出现编译时错误
3 u0 T! c0 Q1 U0 z" j, g( k bC. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误
' M+ Z1 h9 {5 r% [- AD. SuperClass.java可以编译通过,但SunClass会出现一个编译错误
, K9 r- F7 U; u9 \3 ~11. 读程序:, A6 L& i% z9 g( f1 ^+ `
1. public class Test{8 I- p* b n8 ?# k/ f
2. public static void main(String args[]){
* l! D5 G3 r) F! |3. int[] i = new int[5];# x9 K1 \) C, q; V0 V
4. System.out.println(i[5]);
/ p/ O! n; x( \' J2 C5. }
. L7 v' f) N) @! d I" ^6. }
# U6 S: g% z( c( g' w当编译和运行该类时,会:
h5 [1 V4 [; f3 JA. 出现编译时错误
' c6 C/ w) K; k: m# ^; H! YB. 出现运行时错误
2 W6 c0 N& Y( B3 y$ FC. 输出06 V( b6 I' _+ ^- l- R$ W x D
D. 输出null2 x# @, S6 W) P9 _; p: h( r# d
12. 以下哪个选项是正确的数组声明或初始化:6 K: X7 w' X& Q k9 X
A. Array sizes = new Array(4);
6 `1 e3 v9 u' B+ _/ _+ b3 m, PB. double [] sizes = {1, 2, 3, 4};
# Q9 H z/ H' W; @+ O$ Q# kC. int [4] sizes;
) L- u9 a6 |0 V- s5 KD. long sizes [] = new [4] long;
5 I, D/ l$ p" w13. 读程序:/ ]) U' e6 ]( x4 f4 W* ]
1. public class Initialize{
' F4 Y! q; I9 A6 ?# t2. int x = 200;
7 D1 W3 ]# r. F& ~, C3. static {+ |$ g9 g- _ w$ |+ D
4. int x = 100;8 Y* f) {# C' n) t- ?8 p
5. }6 D1 }5 Z4 l' t. H: F& g. s; V& |
6. public static void main(String[] args){
! N8 p/ L! G- L+ @, |$ ?0 G, }7. Initialize init = new Initialize();
( F+ _9 J U# _! E& H+ j8. System.out.println(init.x);8 Q9 ^( x. v6 i& {3 g# W& y* N
9. }9 a {: T5 B: a% F( r. j
10. }0 d$ {% P% q- i5 f7 r
当编译和运行该类时:
# ~9 X6 T8 B2 a* C3 u0 f" Y# W# {A. 程序因为第3行产生一个编译错误
( O; u" I( Z+ m7 S' A2 VB. 程序因为第4行产生一个运行时错误4 ^( g4 E: a" p# F$ R4 G
C. 程序输出100
- O3 l+ j# ]$ o ~( @3 LD. 程序输出2005 {1 _! o7 c5 V0 i, i
14. 读程序:
, j: x& o1 c8 F6 H- `; ]/ [& I1. public int abcd( char x) {' V. |7 j' s' c
2. if ( x <= 'M' ) {
) d" M' P1 D$ P4 O( U' R3. if ( x == 'D') % D) F3 v; Q9 l) W8 ~+ B: X s
4. return 2;' o, I$ b% N4 J
5. return 1;
3 |' i+ d% b1 Z. T3 F; M: c6. }
, G: M# [; l9 U# t# u& t& d4 `7. else if( x == 'S') return 3;
0 ~. y8 F. Y& v+ {! A# |8. else if( x == 'U') return 4;
" k6 h# T, U/ R9. return 0;
* x3 o8 z/ z/ u6 I! f. f10. }7 C- y, Y' l# ` v8 a% Y$ @2 P
在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1
6 ^& k- i i6 DA.‘X’2 X! h/ P7 I/ ^
B. ‘A’6 w0 m( t, T3 r0 a6 z; D! I5 ~
C.‘D’0 @9 Q) {; J1 S9 j6 a
D.‘Z’8 F _( T ?: x2 x5 B6 c
15. 读程序:
6 ~% l1 J6 d, [& N/ I" m1. class TryTest {+ Q! x+ q9 }7 t+ w- t5 U g& }( w# p
2. public static void main(String[] args) {; l8 V$ L1 ]9 v$ j
3. try{
' g4 Y. J/ @4 z" ~9 A6 s9 k8 u4. System.exit(0);) ^, q% Y7 q9 P' p$ X. L. w
5. }* K4 i4 Z& M6 ]- F; Q* |
6. finally {
; @+ S% E) X2 q* _& Q- {0 s7. System.out.println("Finally");% k* P8 J6 U2 ~. ?, |2 _, r
8. }
' u$ ] x& b5 Z# |0 e4 ?9. }
; M* R5 q4 _2 {! O8 Z0 n7 x/ I10. }1 W) ]6 r9 `" N* @! {- }
当编译和运行该类时,将:
# B4 G7 ~8 D6 v& fA.程序因为第4行出现编译错误& f: l. \" a6 h) D! f
B.程序因为第4行出现运行时错误
; W+ E+ C- ]# i& RC.程序因为第6行出现编译错误
! X, B; _# C5 oD.程序因为第6行出现运行时错误
& ~, {* u7 O9 ^/ C9 ^8 c. \E.程序输出“Finally”
0 N4 q. c$ |8 G7 C% @4 O4 [F.程序可以运行,但什么也不打印5 o( u' x% w& `1 `2 Z# d$ B
0 P4 c- N6 l1 F/ t, A8 V; R+ r7 V3 I
16. Given the following variable declarations:$ K( l" n5 t7 z' ^7 @
Boolean b1 = new Boolean(true);
" C- P; V Z0 ]+ ^3 w2 _Boolean b2 = new Boolean(true);$ e0 Q2 E# k( s7 B
Which expression is a legal Java expression that returns true?
4 r4 S5 T& r; X6 F& ^0 [! AA. b1==b2
1 _$ u, A( v/ `B. b1.equals(b2)( h. O/ |( W+ x* t
C. b1&&b2
6 x* W* J$ u$ |, e* u; e& y0 UD. b1||b2
+ b, N( Y1 x7 `2 c, yE. b1&b2* P7 a# n5 g8 Q8 h7 T$ F' D
F. b1|b2
' p: L$ R2 m1 U% w% q; O17. Given:
7 S: C" P/ D& I: l1. public class Employee {2 _8 M; K2 j6 L1 A/ |
2. 8 l. C4 E8 J9 d* q, H% A' o
3. }' J$ M- S! s" [' J# t
4. ; t6 k7 H1 b0 E) {/ F3 d
5. class Manager extends Employee{
) {7 J% o) {2 r# p6 c, K9 c6. }
; N- A* l$ {( W" ]3 zWhat is the relationship between Employee and the Manager?
: m) Q; h, K$ s4 @! B. s9 r+ pA. has a
* p0 ]- u$ H* b$ b8 k" c; nB. is a
8 z; K3 h/ z _$ a" ~. V! q; sC. both has a and is a
/ `3 h2 e( g* b2 |D. neither has a nor is a
) d5 P. l0 A/ R18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?# q/ f1 t8 s* D9 O+ j h8 a+ [
A. BorderLayout
! V2 P6 t; } y% Z- I9 @B. FlowLayout, B5 n6 q. Y. D* X `; A/ ]
C. CardLayout6 m1 [, }8 u3 z( J k
D. GridLayout
3 A7 u) W5 V/ z3 M3 v( M5 ?3 c19. Which of the following AWT components can generate an ActionEvent?3 o% E) W3 ~+ {6 ]' E9 v. C
A. java.awt.Button) [8 A6 @% @; r1 C8 |( _
B. java.awt.Panel
. }5 P& ]! E; @8 \; w" VC. java.awt.Canvas
' Y! R3 _. O8 B* I: q( H# Z: ZD. java.awt.Scrollbar
. S5 f# g; ?) U6 U20. What kind of Stream is the System.out object?
8 {: c5 Q( y A( F! l: @ @A. java.io.PrintStream
) m$ Z. B( s1 v+ NB. java.io.TerminalStream
- f1 I" q0 L+ X; C6 p! CC. java.io.FileWriter
" l: \# o* i Y9 U1 f! b' t% s. M( sD. java.io.FileWriter. P& Y7 w1 t5 m |3 [3 D
21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)2 q+ t( z+ g3 s+ J* |
A. java.awt.event.FocusEvent
S5 V# v& O2 ~+ f. hB. java.awt.event.KeyEvent
& D6 f! g/ y+ c6 i1 s* DC. java.awt.event.ItemEvent! c4 W8 {) |4 o z( e
D. java.awt.event.ActionEvent/ ^0 P+ |5 S% r% p9 F
22. Which of the following methods are static methods of the Thread class?(双选), t# h$ V) X5 r' [$ r* L4 f
A. sleep(ling time)' O& Z. K0 b! L: s6 L
B. yield()6 M7 v$ Y/ D' u, O3 X9 |. X
C. wait()* u/ A* c' Z7 n! o3 W5 X: s
D. notify()
( n* q" v/ d @/ J9 C1 e7 `6 r5 t2 s$ d23. Which method do you have to define if you implement the Runnable interface?
+ T9 b/ M) P+ S2 Y$ x6 ~. RA. run()1 z& i7 g& d7 m' N: m+ `
B. runnable()
) z" o: d8 B3 e/ R, wC. start()
( e# ]9 w9 O" K7 r8 ?) i( wD. init()
- ]# l. E$ v U" c) }E. main()
8 ~7 U! f5 Q Z/ a) C/ B24. Given:8 P* `6 x1 R4 @8 \1 i1 }
1. class MyThread extends Thread {
% o" ]( |2 [0 Q8 n; ~) v2.
/ L& g0 k& `+ @0 ]6 }* `9 d3. public static void main(String [] args) {$ |( b8 N+ S' {
4. MyThread t = new MyThread();
& b& v P, l; \% A; I5. t.run();
8 Z) M6 U: _, z$ h9 H; z6. }2 O. t- l$ J3 n& G0 Z) ^ Z
7. 1 g' F! f' o# U8 S: ~
8. public void run() {- t& E; a- M& e( h
9. for(int i=1 ; i<3 ; ++i) {
8 E) j1 e; C0 m2 q( t8 f! O10. System.out.print(i + "..");/ x# [1 o$ ?- [8 l! ~' h3 X
11. }
% h* \ a; t& g% {8 U0 u12. }2 O# @- k0 M* ^
13. }
* i9 r0 h* J0 ~( NWhat is the result of this code?C
% ]1 a8 D9 d; D* }, CA. This code will not compile due to line 4
' h. l% q. @: a Z9 ^B. This code will not compile due to line 5/ c1 t' g0 u% ?6 ^' g$ l
C. 1..2../ g8 b; F- a6 ~/ f, ^8 G7 q$ n y9 g
D. 1..2..3..1 A4 Y& k3 Q+ A
25. Given a local inner class defined inside a method code block, which of the following statements is correct?6 k+ a+ B, V* G( y& R
A. Any variables declared locally within the method can be accessed by the inner class& y- V1 v/ b* z
B. Only static variables in the enclosing class can be accessed by the inner class
. n9 C3 O& V7 c; PC. Only local variables declared final can be accessed by the inner class* i# P. j7 m- g( F& j8 m) A
D. Only static variables declared locally can be accessed by the inner class.
) K; Q; J7 h! q k6 T- o
. S; [4 b* s6 C/ [
& [6 K9 M2 D& R* y: ~! ^ t( j
; X% x3 A9 ?+ ` N26.public class SychTest{ ; a$ y7 J% c' ?) t8 c
private int x; * u: O5 [' |. T Y h
private int y; ' p; W* `: }) o- E$ {! W
public void setX(int i){ x=i;}
& g9 J( t. [3 [! I; s) A9 H public void setY(int i){y=i;} $ R" H9 X8 S6 n% d. h
public Synchronized void setXY(int i){ $ ~0 e0 k# I& Z" c
setX(i); & i2 h3 W3 B$ N4 W. ~
setY(i);
& S; `6 n' T9 @* {5 p- N8 | } 3 v( p; a* ^9 h2 z
public Synchronized boolean check(){
) I6 q* n( u( X. i1 Q. N return x!=y; # `( D) M: e" H z
}
+ A+ [% d. r" n, Q; M }
7 n+ F7 n5 g) z5 z, ~, T Under which conditions will check() return true when called from a different class? - k; J4 g7 a+ V9 x4 M
A.check() can never return true.
; h' b4 \! h" P7 T) o5 ^! w3 i/ L B.check() can return true when setXY is callled by multiple threads. 2 W7 `0 F" q- q+ S/ ^4 P
C.check() can return true when multiple threads call setX and setY separately.
+ ~9 O( I7 n& N, W4 ?: B D.check() can only return true if SychTest is changed allow x and y to be set separately. + Y3 x- ]4 R; f# y
6 I# Y8 V; \1 ^$ |* y' }/ v N
27. 1)public class X implements Runnable{
: o" a7 p2 U0 a/ U+ h# `7 f0 Y, ? 2)private int x; 8 Q: y5 Y% D6 I2 c7 ]6 o7 n3 I
3)private int y;
, I$ D# O$ ]/ z, R( z 4)public static void main(String[] args){ 3 q2 e/ D" J2 l' P
5) X that =new X();
- @3 O6 l6 I; ?- z: i/ Z* D/ V 6) (new Thread(that)).start();
" \3 E; I0 v( G8 o: _ 7) (new Thread(that)).start(); 6 y) v( Q- `$ B2 S8 A
}
5 K' a, s: r0 \( T4 t! }) q! F 9) public synchronized void run(){ 8 I U5 p8 v) O' b2 f
10) for(;;){
$ f* x8 W3 O O# W 11) x++;
+ C& L5 Z' P9 T7 ~! E 12) Y++;
0 b3 P% i0 X, M, N3 o+ L 13) System.out.println("x="+x+",y="+y); ! B: o7 o' R& K* c# x& S2 Y
14) } ( }- j8 U# a @! A4 I
15) } 7 r' O i- v+ R9 R; V4 B4 S1 Z
16) }
! k$ t1 T: w+ `& i% {1 B& I what is the result?
- H+ k: a7 B7 i# S4 q1 E3 N A.compile error at line 6
0 y6 D& w" U& U% Z7 T. C* ^- D B.the program prints pairs of values for x and y that are
) \' ^+ f# ~) K6 l5 N( H always the same on the same time " O1 @/ V3 y, J3 w# ~
/ A I0 E7 B% b3 b7 I
28.class A implements Runnable{
0 P- F! a7 Q( W, k/ j5 u int i; 8 b/ d+ j8 k2 M F
public void run(){
- E; Y& a1 v( X5 o6 I# W- S try{
* K+ E9 D1 O+ y! W Thread.sleep(5000); , U0 O* @2 c- g
i=10; 8 q# c6 B7 @; H
}catch(InterruptException e){} 1 y8 l$ j( [" d4 i( R
}
' @; _/ q& d" W' [$ N8 J" Y } - v) A" e* b0 r8 u! o
public static void main(String[] args){ . L/ A8 M* A- j( f, Z X( ]; t
try{ $ e# P3 M( c' o5 B3 f5 c5 A
A a=new A(); ( y( [1 Z9 j e# g
Thread t=new Thread(a); - U) a& Y0 a# i. {9 H
t.start(); $ z0 L8 V$ v' X* z. I
17)
* n* B; u; L% s4 o h1 E4 P0 f8 K int j=a.i;
$ ?1 {8 ~6 u& s$ A4 D 19)
6 c1 Y% r8 l2 V6 X% W+ J7 @ }catch(Exception e){}
^+ Z4 q8 ]7 p; k, r }
! q% o) U; @% N" u } $ X! ]/ z$ v# H- o7 O1 p
what be added at line line 17, ensure j=10 at line 19?
7 k, R6 S: r( F ~/ a A. a.wait(); B. t.wait(); C. t.join(); D.t.yield();
( c2 Y4 P ?8 O E.t.notify(); F. a.notify(); G.t.interrupt(); . O9 J3 g$ [' _) A9 l" u
29.1)public class X{ # O/ ` ^/ L# t* Z' a
2) public static void main(String[] args){
; T5 l i4 T( J: z5 h- @ 3) String foo="ABCDE"; % w1 I' A* @# L! I/ q
4) foo.substring(3);
' s! w0 F5 u+ F; M% L, L 5) foo.concat("XYZ"); : `0 M* |3 g* T: q+ S2 S
6) }
9 H, X1 l) m! C% C A7 Q$ W 7) } 5 X$ { x; x7 c* j
what is the value of foo at line 6?
# _' ?1 v7 O' k* E I30. public class Test{
% a' R4 R4 c, ^: D9 O2 z$ p1 S public static void main(String[] args){ # r# _2 Z9 E- }8 F" ?) T
StringBuffer a=new StringBuffer("A");
7 Q, C9 N8 H, _# \$ M7 H* @1 _ StringBuffer b=new StringBuffer("B");
# b3 t1 x# I$ q; @; e, T0 N operate(a,b);
1 Z( s" F' y( p7 }2 v System.out.pintln(a+","+b);
7 e% ?/ n, N8 z& F. D4 c, T }
, `/ P- Z* D* C& Z public static void operate(StringBuffer x, StringBuffer y){ " ^; c2 p! Q8 _! K
x.append(y);
. }0 A/ [1 {3 @, r* ] y=x; 3 U1 l% I, y. i! H2 p" q
}
2 W8 Y/ k. }( L }
f! J% g; n4 D/ w) d- I1 X what is the output? |