Sample TextJava培训考题
& k1 K7 L) b6 H+ w6 d6 y! \ x* T( z' p1. 给出以下程序:: {& l K* k3 }; |' y
1. public class Colors {) g& H* A, O6 K1 n4 P
2. public static void main(String args[]) {
+ Y" Y9 N Q" r: ~$ t, T0 n# ?9 A3. int n = 1; I% D/ j7 [- {; ~) r
4. System.out.println("The Color is " + args[n]);' v0 j3 ~0 g. d/ ?
5. }/ J% \' l0 P2 l- n: }# j. c" Z
6. }" i. s: d# G' x5 o+ X% t
假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”
( t+ _- v9 w5 H) p* B r" @A. Colors red green blue yellow
$ v/ J. Q2 s# bB. java Colors blue green red yellow
- Q" h/ N* c2 Q/ z. pC. java Colors green blue red yellow' @9 r- x v& B) ~: J* A1 Y; c1 r
D. java Colors.class blue green red yellow
2 ]% ?( [; C: p6 g, xE. java Colors.class green blue red yellow- N7 `# R$ t Z7 N7 R
2. 读程序:' E. c: P) T3 d
1. public class Test{
! e7 v6 |0 d2 O2. public static void main(String args[]){
' k; [9 c+ ~; [9 l( B3. char c = '\u0009';
# g. Z" n8 m4 K4. if( c == 0x0009L). U" B/ R: O7 ~/ C/ [
5. System.out.println("Equal");
' ]* ~ r7 {/ t: m8 ?/ n6. else System.out.println("Not Equal");! b* X: B, {6 z( h; ]8 ^/ R; n
7. }
$ `% [: r1 |/ f8. }
- T( w, N7 i8 E# h/ ?$ U( T当编译和运行该类时,将会发生什么:$ _7 ^- `! \+ E' u4 P
A. 程序因为第4行将出现一个编译错误5 X* d$ M7 ~3 F" W0 Q5 L$ q
B. 程序因为第4行将出现一个运行错误
# ] ^. f1 T+ B! m. k& L+ J8 W* HC. 程序因为第3行将出现一个编译错; q2 b2 \0 y7 e% q
D. 程序输出”Equal"& Q. A5 y9 W; K
E. 程序输出"Not Equal”! r' P, ^* f A& ?; K' m
3. 读程序:
1 `. U5 ?+ l0 w* s( V9 `0 C1. public class StrEq{
* m* @ x" n5 u R4 x: K0 C" N1 X2. private StrEq(){
, Q6 @* a, E; t3. String s = "Bob";
; }/ N* ]0 b2 B; ^2 j4. String s2 = new String("Bob");
' f0 D# r+ N9 C9 v5. if(s == s2){+ f$ t* z. B3 b& J' c8 e
6. System.out.println("Equal");
- M0 f1 w, H X3 y9 M3 k* R7. }
7 j6 W. h3 H4 U9 Y7 _& j, p8. else{! J( K! Z" e. k% u, S: E& c
9. System.out.println("Not equal");: W( b, m7 E" ~: U; T
10. }
1 z1 R6 Q* J" a- T& y8 n2 ^6 W) W11. }3 k" R* A# g( c
12. public static void main(String args[]){
2 {+ F- |& J( I! J13. StrEq s = new StrEq();2 e" M8 K" p! K$ i q9 S v- I
14. }$ _1 I# Z) n2 g2 _8 y
15. }
+ _! |! X# I8 k2 b当编译和运行该类时,将会发生什么:
- L# c! c `+ U9 Q" U2 U1 bA. 程序能够编译,并且输出“Equal”
2 t# {$ ^$ [* h$ X: e$ \ V+ eB. 程序能够编译,并且输出“Not equal”' K1 c( }8 ]1 z9 Y; v: A8 D4 D6 O
C. 程序因为第2行导致一个运行时错误/ @( R/ A: q3 z- y
D. 程序因为第2行导致一个编译错误
' E/ w$ F4 I' t3 ^ g4. 以下那个赋值是非法的:. N3 Z6 K4 R7 k3 p" j: _& c& B
A. long test = 012;
7 p: F0 F0 p/ cB. float b = -412;
( S1 f6 \; ]% L, RC. int other = (int )true;
# ~% C0 v4 P; w, F% M: iD. double d = 0x12345678;
( }" }# b/ P1 fE. short s = 10;
7 T; T* L, x, r& U3 e! w5. 读程序:: q1 v: c; Q: s3 z. H- {
1. class CompareString{( F9 Z* x& A3 a/ q$ f) _( X
2. public static void main(String[] args) {
! Q$ ?/ W' F9 R1 j7 ?6 l/ C& @3. Integer x = new Integer(5);
' t$ o; [: I1 j% L% q4. Integer y = new Integer(5);
, U* k Z% w$ [5. Integer z = x;
?8 U' W1 Z0 _$ `* p; c" N! g" L6. if ( ______ )2 }: y' r% ]9 Q, F% u
7. System.out.println("true");
* X' k- D3 d# m% @) V8. }) N5 e, Q- Y3 Z. ~% C8 ?3 @
9. }0 U0 E* j/ u& Z" \5 F8 [
以下哪个选项填入第6行后,不能输出true:' i7 @7 n9 J9 A1 E$ y0 l
A. x==z
K. {! A$ x9 n. i! w/ {B. x==y; ~9 P2 \8 Y, T& d$ g1 e1 p' ]
C. y.equals(z)
$ ]# o/ m7 ]3 i& ^; T* lD. x.equals(y)( z2 M6 L7 e: l1 x! Y- R4 z2 o6 K
6. 读程序(双选):! j1 B2 Y: V Q$ Z8 h: f# ?( R
1. public class Declare {
. T! {% v9 A2 Q2.6 ^. H5 `3 v2 t$ J$ P- _
3. public static void main(String [] args) {
+ D( t( b& g7 Y4.% q$ @7 T, _7 `
5. System.out.println("The variable is " + x);
) R+ ?" a$ [ `% N6 @& ^6. }: L9 l6 U" p3 U* H' |( d
7. }
* G0 U3 ?# F5 M9 [2 F) ?以下哪个选项中的代码可以使程序编译通过. M' W5 O: t+ C2 V$ p- R. A
A. 把“int x;”放在程序的第2行
- d; O S& u- t! H4 HB. 把“int x;”放在程序的第4行
2 P. ~% ^0 o5 F' |- m7 U" wC. 把“int x=5;”放在程序的第2行2 Z# B, F: f% g+ s8 H
D. 把“int x=5;”放在程序的第4行0 H x7 w; N7 [7 g0 u9 E& {4 H( n
E. 把“static int x;”放在程序的第2行
% z) J3 K9 z& T9 u9 }
! D X1 X7 f$ T0 U" G- t; s- {* g2 W9 {* \* s5 ?0 q1 B/ X
1 c5 H" z/ q @; z: Z: T0 E7. 读程序(双选):
! V) o* Q- Y" S1. public class Child extends Parent{8 p' Y, V1 ~+ W+ w9 K
2. int a, b, c;7 f! ?! F# U# v' c4 [- N
3. public Child(int x, int y) {
4 W& i& S, L; h+ ]4. a = x; b = y;* e' z8 \# |! i2 R; B' M
5. }) O1 e, P) |4 \3 T: l6 X$ y( q
6. public Child(int x) {) ]# D& Y h) X% k. Q: Y$ N
7. super(x);# v0 a. g& _ A5 A
8. }* Y% {; k0 D3 C- c, p& m% V6 ^
9. }# F: _3 i2 Y+ o- d. _( O
为了能使该类编译通过,下面那个构建器必须出现在父类中:
- b/ M9 A" |% S/ K4 x; TA. public Parent(int m,int n)
% s& J0 z, @* ~+ a! o7 ^, `3 jB.public Parent(int m)
( Z1 | z& [$ q9 p, U; fC.public Parent()
8 i0 Q. M' ]# aD. public Parent(int m,int n, int o)- l2 L M: E7 s! ~
8. 读程序:
: U- S$ b5 G& }; {" x% k; D5 y' r/ A1. public class Derive extends Base{4 q% n5 y- i1 G3 F
2. public static void main(String argv[]){. t" ?- c6 g( ~( r
3. Derive a = new Derive();
& O1 h' S/ [" X& _# V4. a.method2();7 |( L( y& @) A- A5 j
5. }* |$ V. s+ i# m4 M" U0 G) i. Z0 B
6. public void method1(){% h0 \' v* H( Z0 {
7. System.out.println("method 1");% `* V) ~9 D# E; z2 c
8. } ) H* j* V( |. E* B- F+ _8 m
9. public void method2(){" p5 N& W) e" z) t" _- n0 N; H' b9 W
10. method1();
: d( Y. q3 l( |4 c9 U11. }0 M/ ~7 y, T1 d& t: N! I2 @0 p1 D
12. }" `* s& ^" m/ `9 u: z5 \. Z% N
13. abstract class Base{! a+ |, j4 v- E' R& I9 i
14. abstract public void method1();/ E9 Z9 K1 g/ T- |" I
15. public void method3(){
0 s5 Z8 S* ^4 N+ o$ G16. System.out.println("method 3"); I7 w( B1 u" g; m3 s
17. }) N1 T \, V2 U8 i; o
}% E% z& J: P7 v& ~
当编译和运行该类时,将出现什么:
7 k2 W* u3 I* W5 U$ m$ _, vA. 程序输出“method 1”7 @2 S! f$ g8 T; K
B. 程序输出“method 3”0 f% g# A& ]1 W% N3 _2 L2 }! f- C
C. 程序因为第15行而出现编译错误& Y6 g7 B, |% r0 _6 D! S6 k8 J
D. 程序因为第15行而出现运行错误
7 B% S: m! W1 e! k7 p7 H& J9. 以下哪个接口的定义是合法的:
/ c0 \8 t, k- w8 L* X1 U# ~A. public interface A {int a();}6 T! @5 S9 D. q% k, M
B. public interface B implements A {}. r9 b# K, C. f( |6 O& ?$ r
C. interface C {int a;}
7 h1 Q4 v) s9 K! T6 O mD. private interface D {}
, d1 t/ q0 F+ ]' {$ C1 l( G3 v10. 读程序:9 u# O+ t- H' e
1. //File SuperClass.java. @ b3 E8 h8 c2 ~
2. package MyPackage;% \0 k: ^% ~! X6 c; r0 m
3. class SuperClass{$ f H) t" I7 Y. \
4. void myMethod(){
( \6 a, K8 e S8 h: [5 D$ z, Q5. System.out.println("SuperClass");5 Q9 P' R( o3 ?, L" ^
6. }
0 t7 q |. A- B! u7. }3 \8 Q" T& z" ^1 r5 K
8. //File SubClass.java
- S7 t& r3 }9 y9 u/ y$ A3 e9. public class SubClass extends SuperClass{
2 z. C0 Y. F& w o3 R: Y* [3 s! S# [10. public static void main(String[] args) {) Y. B+ A0 @9 g7 C9 h3 f. O
11. myMethod();5 ` W; Q# k+ B/ c2 {5 n
12. }
0 \7 Z( W" h) z) y# u) Z: L" |5 e13. }
: K+ f( m t9 k0 |8 O9 D当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:
# _3 U' }0 V/ eA. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”7 n+ c4 F6 N: z, I
B. 两个类都回出现编译时错误
7 w. x( U A1 e( j6 m4 dC. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误
J6 k; a8 g/ s- f g/ N% OD. SuperClass.java可以编译通过,但SunClass会出现一个编译错误
}" W9 Y- u# v0 H# G* l3 l* U11. 读程序:2 ?& t! o$ c2 L' ?6 H: V, H* {) ?. k
1. public class Test{, R& B. d6 z$ \; S! b
2. public static void main(String args[]){9 `+ T) H6 n4 Y/ d
3. int[] i = new int[5];
. G% l: s/ w6 C w6 |4. System.out.println(i[5]);
' q! J* c1 ]0 o" o1 `8 ~$ g5. }
1 ?. t" A8 T$ Z6 G; `6. }
# ~! Q e6 p" N; Y& x& D当编译和运行该类时,会:$ N1 }5 @( C/ u4 N3 k$ C: E; U4 V
A. 出现编译时错误
, m0 G$ n7 M* R8 L) k/ iB. 出现运行时错误
+ K6 U/ Q. U) i5 q4 P9 W3 {C. 输出0
; O1 |* y4 f9 ND. 输出null
: r8 f4 L+ @! v5 k5 a( T# {1 O12. 以下哪个选项是正确的数组声明或初始化:) K, P( d% b1 X6 g" V
A. Array sizes = new Array(4);! b$ b% W2 |0 s0 I' b3 ~0 I
B. double [] sizes = {1, 2, 3, 4};
8 l4 @& R; y9 [* S, h% q. O& M4 w3 JC. int [4] sizes;
+ {4 R' }4 j' n- p9 D3 d2 g( K2 A7 ID. long sizes [] = new [4] long;
: W" I+ e3 X& R4 t13. 读程序:* K2 _& H) [' D c7 F2 ^+ ~
1. public class Initialize{
2 i" o3 y& \ _. I" l0 q2. int x = 200;
# I- c5 c y% C- k0 w3. static {
2 a& z' m! V4 n; \4. int x = 100;; Y% M( d) L$ I$ i
5. }
* u% ?9 W/ V( @8 m6. public static void main(String[] args){& u$ X2 ~0 V+ N4 h* |
7. Initialize init = new Initialize();+ g' J; O! U6 l3 ]! Q
8. System.out.println(init.x);
3 e( [" {& q1 w8 \# V& A9. }
2 n) D: p4 C9 {& \10. }' n. S, v7 R0 ^( L! ~! C2 t
当编译和运行该类时:
3 Z3 s; U! q" e$ Q. g2 iA. 程序因为第3行产生一个编译错误& j- l# A' }2 y/ _+ `2 h
B. 程序因为第4行产生一个运行时错误$ _/ o& a5 M0 G7 ~3 q) A9 k& j! u
C. 程序输出1003 I! w$ G L/ `# Z
D. 程序输出200
1 j4 E% j8 P, c+ d8 x14. 读程序:( k2 S4 N8 F$ k
1. public int abcd( char x) {, S1 W; r/ {! H) c
2. if ( x <= 'M' ) {
/ |, O) E- j% q0 H9 N7 U3. if ( x == 'D') ( R; P( y% L5 D* c' \
4. return 2;
5 L* S# W! m! b' V) z% K+ A9 u5. return 1;
6 S, h. F1 I; Z# K6. }
2 K0 C. {" W4 J0 T. _! w' F9 v7. else if( x == 'S') return 3;! q7 b3 h/ `8 `/ z1 q
8. else if( x == 'U') return 4;' t8 F$ v( R% ^* f3 b
9. return 0;
- {% ]0 V; J' { }10. }
3 ]1 V* y$ \) o& u. R( G在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回1* W% l D0 }& v5 W4 y3 U0 b
A.‘X’
4 [: c6 I0 `( o0 U1 U0 O! n. NB. ‘A’
% B4 I/ u; I8 M8 k. s: @- {# ?C.‘D’
+ N, c3 A, j, }; R* u$ bD.‘Z’
f0 R8 x6 Q5 w/ | a% M4 D15. 读程序:* G+ [ F$ c0 S* ~
1. class TryTest {
) P8 C5 r" B( i+ E2. public static void main(String[] args) {, a2 V. D/ ^ O: h$ ~8 r
3. try{: `. T! e9 R% ]/ ?' k% } ?5 q
4. System.exit(0);
/ W/ @. F' Z1 U! b: K4 i$ M5. }: H- v+ u- I, m1 h/ e
6. finally { I4 K: P/ Y3 Y& g3 R1 ~% `
7. System.out.println("Finally");
' T. l5 i! n% g4 t( \1 o( C/ e6 ?8. }( \) n7 e5 Z% B6 O8 J
9. }
1 v4 a% w0 r5 h) F! N10. }
# e% ~) @& | p当编译和运行该类时,将:# p. T: W3 A# W. @: Q/ U
A.程序因为第4行出现编译错误
0 ^6 N) E: r: aB.程序因为第4行出现运行时错误
2 L8 I6 ~: d# fC.程序因为第6行出现编译错误) w: g1 k z4 t% E4 y! t! |, n
D.程序因为第6行出现运行时错误
5 i9 [8 z, D8 ?0 @6 CE.程序输出“Finally”% O: `; j: C- L* {
F.程序可以运行,但什么也不打印/ @ A: e8 P! v0 K' U! k( n
* P \+ G$ Q) w9 _9 c0 e$ z5 ~
16. Given the following variable declarations:
0 j* M$ [- l' j K' XBoolean b1 = new Boolean(true);
' U- q" S$ s8 ]; ^Boolean b2 = new Boolean(true);4 _2 @! M5 `( Q! O) R* U/ M
Which expression is a legal Java expression that returns true?
2 H% S W' [8 `2 f, wA. b1==b2
+ b4 R3 j8 X* Y/ @# z* {0 Z+ [B. b1.equals(b2), C# Z! H+ t# `9 n0 B2 L8 O
C. b1&&b2
/ w3 o! x& O+ F7 \- c# o- a M' iD. b1||b2" B G9 Q, p; t
E. b1&b2
5 P) j+ j v' `( tF. b1|b2
+ o& J; T+ }4 a17. Given: " w$ o+ i" h9 w ?; s9 ? b$ e
1. public class Employee {
+ a( U" | o+ C2. 2 P9 X6 Y9 L2 L* {2 ?3 x
3. }; d4 l4 k0 [! f7 A# B
4. - Q" ]3 ]7 |5 u5 V' B* b
5. class Manager extends Employee{: J, B2 u( C, t/ d1 k
6. }, I, c# M% Q) }+ @2 i
What is the relationship between Employee and the Manager? O- ]0 ~1 B. C9 ^
A. has a( B- h% S2 A- Z% m
B. is a
1 y$ Z! O( _/ T* R- Y8 eC. both has a and is a2 N% F0 c$ W2 h( j1 n5 R+ N
D. neither has a nor is a
$ t |$ S1 ~' F' c) p z5 |18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?! V7 U x, ` T$ N
A. BorderLayout
, b' j; |, G. G. d& l1 BB. FlowLayout8 p% ]* N8 o1 c: Z7 j7 i
C. CardLayout4 I- x4 C2 Q. t5 i2 U$ O, |0 H
D. GridLayout& c7 p- Q3 M K# O: z+ q( o- V; G
19. Which of the following AWT components can generate an ActionEvent?- H2 h8 I/ T" I; M9 Q' i
A. java.awt.Button
5 ]1 E( X8 \' B' j$ [9 ?+ u, ]B. java.awt.Panel
: F% S& B6 V) M% c iC. java.awt.Canvas! x! ]/ T/ I& M6 z P, C" n
D. java.awt.Scrollbar2 e' ]4 _# ~7 [# b5 ^8 z) e
20. What kind of Stream is the System.out object?: p7 S+ n* Q! w8 `$ J, a1 T4 X! @
A. java.io.PrintStream
2 k2 D& s( p9 N U: q! VB. java.io.TerminalStream
( g- D# @( [) u! {C. java.io.FileWriter; G m/ a8 }$ m6 f0 L0 x7 ?
D. java.io.FileWriter8 l8 }' R8 N2 E' a; } a
21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)7 y+ }3 i; K- n+ D: y/ c8 G
A. java.awt.event.FocusEvent- r6 j. ~8 T& k* I6 L" T
B. java.awt.event.KeyEvent" S. S r! W g0 ^6 H0 u
C. java.awt.event.ItemEvent
2 t, x2 ~- V1 ]5 \D. java.awt.event.ActionEvent8 U; m/ v+ w: b( b! ^+ c3 M9 i' t3 U
22. Which of the following methods are static methods of the Thread class?(双选)* _. ?$ S( y0 `6 J& ^; J
A. sleep(ling time)
/ v9 d* t/ R8 [1 J/ iB. yield()
% d( h% ?7 M t N) gC. wait(); ]3 R& h/ m, t/ B
D. notify()" V/ D6 A6 S" \! M# x! Q
23. Which method do you have to define if you implement the Runnable interface?6 p7 A [, j9 R6 W* }3 H+ C
A. run()6 r8 J3 @* ]; E1 J0 q
B. runnable()
4 S9 _% o7 A' @ J/ [1 nC. start()
6 k! m6 r3 Q. e3 a- _6 XD. init()' B5 l6 i1 j: j/ H9 S/ P
E. main()# B$ h& J6 f# `$ L/ _% v$ O
24. Given:2 H0 y3 E% e8 f R! \- r
1. class MyThread extends Thread {
* T! z) \; N4 K! h: H2. ) a7 y3 B" b% ^; y) e' t
3. public static void main(String [] args) {
* H" ?& l; a& [; D2 G% j7 p- k4. MyThread t = new MyThread();" Z7 N5 }- k) b b! g" T; z
5. t.run();9 c* [. B( z4 V- G
6. }
, |( [6 [# c6 L" @8 X2 f% A O, o7. $ ^, @! D( I3 G5 I
8. public void run() {, Z5 T0 g& V: c7 H+ _6 J" q
9. for(int i=1 ; i<3 ; ++i) {
, ?6 k; J& q7 {10. System.out.print(i + "..");4 f T% b: b% v$ y- Y" u
11. }8 X+ S# `4 b/ L9 u+ X
12. }
E+ m! g' M4 A0 ]: A! T13. }
3 p; S2 C1 a4 g) N% CWhat is the result of this code?C
S6 d. {: L) L: U0 U6 x4 Q4 KA. This code will not compile due to line 4) x k* C0 R& a: L+ J, C) U
B. This code will not compile due to line 5( f5 O0 N! ^* H' e0 ?, W6 C
C. 1..2..
1 G: K2 }+ }; Y. ~# t" mD. 1..2..3..
6 W; i. E! p/ {; A25. Given a local inner class defined inside a method code block, which of the following statements is correct?
# K2 p6 n% k: |5 jA. Any variables declared locally within the method can be accessed by the inner class/ m$ V8 K( B7 @3 Y2 ~- L
B. Only static variables in the enclosing class can be accessed by the inner class
6 }' @1 A ^2 w6 p! {2 b! VC. Only local variables declared final can be accessed by the inner class
, X) S8 c3 D, q9 V' X7 cD. Only static variables declared locally can be accessed by the inner class.: L8 z/ V0 c, E# B, J4 F5 t$ H
2 x' N4 i, l" X) q
" P2 s: S$ n6 F0 {" J
y+ ^6 X$ g; Y, D4 q* Z* T+ N26.public class SychTest{
7 f# R% w9 X9 s1 E6 v' ~ private int x;
& _% D9 O8 e$ v private int y;
: b3 D* [& u1 Q2 r7 w. e/ P6 B& G public void setX(int i){ x=i;} - B& y% d7 q0 f! ~
public void setY(int i){y=i;}
7 m1 [/ s' `; T2 F public Synchronized void setXY(int i){
& ?/ }4 w1 f' ?, I9 v. A. Q" b setX(i); : q" w [. R" T5 f; \) H
setY(i); ) f! o# L7 d6 R
}
2 G/ H/ K6 w* Z2 f+ d public Synchronized boolean check(){
) @6 ?& r" q- G! z' q return x!=y;
/ ~4 Z1 {" h4 r$ R8 _# t }
. }2 w; A& F2 O7 x3 T* l }
# H/ E, c3 I. t Under which conditions will check() return true when called from a different class?
! }6 y2 c, j1 V; ^9 S7 m& O A.check() can never return true.
4 ]; }" o. h' g3 R; l0 R1 P B.check() can return true when setXY is callled by multiple threads. 6 M* _2 q$ E) W+ P1 e3 Y
C.check() can return true when multiple threads call setX and setY separately.
" j+ J0 }4 c6 c! ~5 d( ]% V D.check() can only return true if SychTest is changed allow x and y to be set separately. ; d' @7 G3 @ _9 c& n
9 }3 ~( T6 [5 C6 y
27. 1)public class X implements Runnable{ ; C# g7 u' c% B$ I2 V) i
2)private int x; 9 Y+ ^! O: ^, W: A; {8 O2 T$ f
3)private int y; : L- T T, T# K1 ]3 w% _
4)public static void main(String[] args){ 9 F0 r+ ?$ _3 d1 n* J# v% u
5) X that =new X(); ' |5 d# z" S! O. D/ F
6) (new Thread(that)).start(); " a$ ~1 F1 S9 ~; ^4 P3 k
7) (new Thread(that)).start();
/ H9 a! c8 ?* F! O9 U; A+ c+ l0 H } 6 O& f; Y" G" k* b/ N
9) public synchronized void run(){ + h# o, A7 R% _
10) for(;;){ $ ^5 m) e% v+ Q# L2 s. D- K
11) x++;
5 I6 C' ~% n, M, b# ]3 i/ ?9 J 12) Y++;
' G. u- T. i6 s. f7 }% V6 T3 N3 t$ W 13) System.out.println("x="+x+",y="+y);
, u3 ~ o4 W" S3 D4 q0 r0 a 14) }
- i! s' T1 i- N& _8 g/ A' P 15) }
1 J0 O& Z. y M! n) { 16) }
* J1 S6 ^/ B" D- @# v6 z what is the result?
0 @% w$ l+ l8 A+ F8 a) k1 |0 j' O A.compile error at line 6
7 D8 s, S$ x% C2 L, C B.the program prints pairs of values for x and y that are / x" a7 R8 c& ]
always the same on the same time / [- @" a6 H: o4 F" S) t& L
: z) |/ [: T( Y& V
28.class A implements Runnable{ K# u3 d) m8 e
int i;
8 W7 F+ _' D7 q. E9 R9 d& j* x public void run(){ ' S6 q) B- W) X z7 S
try{ 8 J, s/ e6 D9 H% K8 i
Thread.sleep(5000);
* B+ C; p3 ~# C$ c4 ]% n9 Y9 V& d+ O i=10; ( h# ]" h6 G N3 e2 v, D
}catch(InterruptException e){}
) E" _3 [, H' R+ @6 N }
8 H) t9 d% Q) Y/ I } 7 W. R: M* m& \) v; ~- n" H" V4 N
public static void main(String[] args){
' x: d% d; ~5 G5 P$ @3 j" x. s i try{ 3 p* i" {, I1 i- x7 y; G% c& q
A a=new A();
+ C- g1 f" y d. `% ?1 e Thread t=new Thread(a);
3 r/ {0 A: d5 ], |: t; J t.start(); . E+ ~( L" y; T/ U y4 L
17)
0 W4 Y$ C$ i( y/ y! v' f1 ^& A int j=a.i;
: Y2 p9 a, q0 V" o" E; a 19)
) ^6 W8 z: N% a! `( t( v9 N }catch(Exception e){}
1 K+ Y0 n6 C! I$ T }
0 q u( i4 I$ Z" R }
" l) Y" f- D" u3 t2 x3 ^7 Q6 F- r what be added at line line 17, ensure j=10 at line 19? J" ?4 u0 H; m8 ]8 t0 a0 r
A. a.wait(); B. t.wait(); C. t.join(); D.t.yield();
8 G1 ^* w- S- ~, A E.t.notify(); F. a.notify(); G.t.interrupt();
) f2 Z! O8 \$ V2 y- t29.1)public class X{
& p B3 V2 Q$ f/ j ~. | 2) public static void main(String[] args){
3 T" T6 Z& @8 C) p/ |4 ? 3) String foo="ABCDE";
6 Q( k N1 X. _/ }5 o 4) foo.substring(3); 6 P& Z+ D, v9 ^0 l, o o9 \, I- G
5) foo.concat("XYZ");
% ]* S. k: |" R 6) } - a5 L5 v! _2 F# _+ U* B- s3 [
7) }
* A& [( v3 f" X! f) ^ what is the value of foo at line 6? : e( S ~8 e6 `
30. public class Test{
3 V7 E+ g; F0 M7 K4 Y public static void main(String[] args){ 8 j+ u8 d/ G8 I6 q; d e
StringBuffer a=new StringBuffer("A"); 4 U( x; k5 h+ f! A+ ~
StringBuffer b=new StringBuffer("B"); 1 j# v& C k9 e# }- N- b* Q5 c
operate(a,b); ; w, c+ F; B; a) \. |' T' n2 D
System.out.pintln(a+","+b);
1 `$ @; l# ?$ v( C+ | } 1 s; M2 Z/ G @3 V+ a6 j. J3 t# o
public static void operate(StringBuffer x, StringBuffer y){ . w1 r$ O' u1 a7 K1 o
x.append(y); % H5 p; j% J' f" I
y=x;
. F8 f5 G- ^6 n. w }
7 Q Q, E9 r6 v% j) N } / P" v& W0 b' [+ F
what is the output? |