Sample TextJava培训考题
' t5 l6 O7 _: _6 X! K2 ~1. 给出以下程序:* _# j; g( }# x# o0 f" S3 f
1. public class Colors {
1 Y" }- r& | n9 C# [. j2. public static void main(String args[]) {5 R8 ^$ M+ k# e$ r3 I) H
3. int n = 1;
" P w5 M3 W; S7 A9 Q4. System.out.println("The Color is " + args[n]);7 e- t7 m6 Q) R. B. n7 M
5. }+ X/ n" b' g9 i: U0 @. a4 A: l, c
6. }
. i8 g/ [! P1 a假定程序已经编译通过,下面那个命令行可以输出“The Color is blue”; F' R' z7 R0 u. e0 o
A. Colors red green blue yellow
d' @2 [! }: `2 M9 K' I& I* H/ wB. java Colors blue green red yellow
1 u, Z$ a, f6 k# h" w9 w3 s+ yC. java Colors green blue red yellow1 \; S* @% O" A5 |- O$ }
D. java Colors.class blue green red yellow- y4 q6 D, g/ [3 i# c
E. java Colors.class green blue red yellow! M- y3 T3 j3 @' N @) d
2. 读程序:( C$ u: n( l/ r: h: l2 k
1. public class Test{
. h3 ?' O5 T; R8 Y- @( x& _' S: z9 {, l5 c( g2. public static void main(String args[]){
: @5 E5 {) `* j3. char c = '\u0009';
9 e! m) N1 ~& G6 ~4. if( c == 0x0009L)2 N: M4 k2 g4 ]' d5 u
5. System.out.println("Equal");1 c, k5 h# Y# c
6. else System.out.println("Not Equal");# L3 ~! A O4 ~! v: S E
7. }; }; h9 g; |+ f2 H8 ?0 y. N5 Y
8. }- ]; A+ h n- x. A+ e
当编译和运行该类时,将会发生什么:1 i* D5 s- {$ t0 w I6 @ J
A. 程序因为第4行将出现一个编译错误
! ^$ q3 `( R- e1 L4 h% \# JB. 程序因为第4行将出现一个运行错误0 g1 D, b5 T7 Y- U
C. 程序因为第3行将出现一个编译错
8 d7 B' j R# }6 I: o0 i5 jD. 程序输出”Equal"
! C7 ?! i' o# |5 g. o; UE. 程序输出"Not Equal”: Z: ^5 \8 u0 g+ z* b4 j ^
3. 读程序:
0 p' Z% z. N! i9 `* X2 M: O1. public class StrEq{* l7 n8 o6 Y1 K3 o9 c
2. private StrEq(){6 x# ~1 F$ E5 |/ l
3. String s = "Bob";0 [* Y6 z8 c e: N+ b) V; ~4 P9 K
4. String s2 = new String("Bob");
1 k6 o P9 v! o# j5 w r5. if(s == s2){) S& @3 p9 t' T) p9 |
6. System.out.println("Equal");
& V" c( o8 i( e7. }
3 E$ T- V( S. v5 r' S( I* Z' @) q! ]8. else{7 g; p) n" O( q. D3 O
9. System.out.println("Not equal");1 b" c$ \. v7 Z: A
10. }
- e9 s1 Y. H }' V. Q$ A11. }
0 w- y% ^ Z% e12. public static void main(String args[]){+ p7 l3 Q) |) }# c; u
13. StrEq s = new StrEq();2 O0 ?7 Z- v1 \$ t, c& Y) k
14. }% G5 m |/ g* ^
15. }
% p3 E1 m: N# x& a. l- |当编译和运行该类时,将会发生什么:5 H) d1 O6 P$ {; o* v/ K
A. 程序能够编译,并且输出“Equal”9 G4 R/ f u- e1 B+ q
B. 程序能够编译,并且输出“Not equal”9 v7 j' P, @2 Y) e: D
C. 程序因为第2行导致一个运行时错误$ X% C% Y; t' g& [
D. 程序因为第2行导致一个编译错误2 L. e, |7 u' t8 z
4. 以下那个赋值是非法的:% \) {/ {8 ?+ U: Q' @( K' M c% T
A. long test = 012;& l' ]# _8 h: S4 {* g9 g
B. float b = -412;
7 V2 I; z& q3 wC. int other = (int )true;1 [9 L" V) G$ N a5 c/ v6 j& u
D. double d = 0x12345678;5 P. Y+ u, g6 Q$ @0 }0 z, t
E. short s = 10;& O* w- B0 r- M* \; p
5. 读程序: D- \% L h. A8 G7 k$ A, Q$ z( n
1. class CompareString{
& H/ t- E- W# J6 {0 h& y( |2. public static void main(String[] args) {
4 e$ M! j' A7 E3 H3. Integer x = new Integer(5);
$ h. Z4 e/ w) H3 R" N4. Integer y = new Integer(5);
# O0 @2 W3 i8 C0 E6 a7 r5. Integer z = x;
+ w/ f$ N) o; k3 ~( A, y6 C6. if ( ______ )
5 g- \& n+ q# Y7. System.out.println("true");
: E7 a( r5 c) w+ l' o2 ]9 U8. }! U; {3 n6 o8 I' z$ m
9. }
' Z' F/ |4 g$ J& g; {以下哪个选项填入第6行后,不能输出true:: K0 _+ }+ {) w9 ]) n9 _' K. O
A. x==z
& i3 _9 f; Q" h8 C" WB. x==y3 l6 j% J, w3 b
C. y.equals(z), w/ o5 ?( O) o2 {, t9 B
D. x.equals(y)
$ N3 O/ @, h6 p, z E! y6. 读程序(双选):5 g; I% M+ {# K S2 k
1. public class Declare {( M5 d2 `. P$ Z [5 d
2.6 ?: J+ D# d) O x- u; b* a
3. public static void main(String [] args) {
7 P% o% d3 n) b4.3 b- h% f' H( r7 ~
5. System.out.println("The variable is " + x);3 x, |# z# a! Q6 W0 T
6. }: n' b& C5 Y( a0 w( k9 ]4 H
7. }
$ B( Z4 M# L9 e: F. V9 J以下哪个选项中的代码可以使程序编译通过: h: c$ e4 J k. v
A. 把“int x;”放在程序的第2行
1 z; ~) S/ O* jB. 把“int x;”放在程序的第4行
6 R. Z% F" V6 P8 f7 @- hC. 把“int x=5;”放在程序的第2行5 ]- Y# p. I& L$ F
D. 把“int x=5;”放在程序的第4行, {8 S7 }7 v# M/ w* b0 a) _
E. 把“static int x;”放在程序的第2行
* L) r6 r: {1 _' w+ Q1 f
( h7 F+ ]! J8 D, A3 L5 _2 U! e+ u/ {6 I: S6 U; ^
; E4 _- b3 @/ C* [8 d! _. z
7. 读程序(双选):
( V% [% @# y3 o. P7 h j a1 J# A4 r1. public class Child extends Parent{2 H" H' ~8 e6 D C6 a
2. int a, b, c;
9 b. O+ b% i; O2 W3. public Child(int x, int y) {# y, u$ s' R8 z @
4. a = x; b = y;
f" |' z" E D9 b) Q5. }
; s) J( o2 v }. W6 E5 N6. public Child(int x) {4 |3 G: `' X: j" z; X
7. super(x);
- d! S D& ~) q3 S+ A9 G8. }
) {/ S; Y. E" y0 A$ X/ h9. }; y. y! y; l6 D4 S# t- I
为了能使该类编译通过,下面那个构建器必须出现在父类中:7 V- C% Y9 ?$ w7 I# F. k" X
A. public Parent(int m,int n). _0 h. X9 |9 f; Y
B.public Parent(int m)
# E$ z* ~: I1 o! K% r* {4 K" M6 [C.public Parent()
9 {6 Q3 ]& i& N, kD. public Parent(int m,int n, int o)1 D2 ]7 {6 P7 v3 @+ a5 Y3 E# x
8. 读程序:
! x' M" m, x0 i9 L% l3 t) Q1. public class Derive extends Base{
1 P4 G. k* K2 \8 C3 O2. public static void main(String argv[]){
6 _ m/ s7 ]/ k3 d; U3. Derive a = new Derive();
; |+ {) o2 b$ y4 S2 d' w+ |5 H9 b4. a.method2();
. C# G. I4 x% K+ r5. }
/ S. x5 H1 s6 z* V# Y6. public void method1(){( f5 R$ J( c( ?, U6 {
7. System.out.println("method 1");! k# Y6 r2 R2 h) A5 U# c
8. }
+ {& g9 m: X/ V+ w6 x9. public void method2(){' ] p2 e; _- r8 [$ B
10. method1();
( w$ g2 x( [8 J% \: ~& @11. }, c+ n$ g/ I( q9 p
12. }
) r( |: A F0 F/ M! c13. abstract class Base{
; W) T1 T1 O7 v14. abstract public void method1();
+ a4 c! }3 g3 v- R! }3 X15. public void method3(){
& t* E- V5 g# T( T9 A, f16. System.out.println("method 3");0 x% t3 D) }7 Q4 g. z8 f/ L6 J
17. }
7 S0 _- C- w: _" N! J( l0 A4 K}6 A0 F$ h6 r6 R
当编译和运行该类时,将出现什么:+ N6 {/ n/ s$ ^. @* B0 E) c
A. 程序输出“method 1”; i/ |& d8 s/ F6 N
B. 程序输出“method 3”( B9 i. g4 E$ v" ?* b' q
C. 程序因为第15行而出现编译错误- p& |% u+ P$ g6 C1 @7 d% D
D. 程序因为第15行而出现运行错误 V; _1 J: C+ n, I
9. 以下哪个接口的定义是合法的:
: K& K3 P6 W, j; D2 L$ yA. public interface A {int a();}
) P$ [. K. F- Z- o! M# aB. public interface B implements A {}
/ s# ]. T; r, J0 B, z4 X, QC. interface C {int a;}1 w" m+ k4 R9 Z( ^
D. private interface D {}; h6 S1 p3 L9 ]6 q: f$ d8 v# y
10. 读程序: D$ B, x! ^) z0 Z M! v% m" P6 W
1. //File SuperClass.java3 ~ n, p6 Z6 U9 n1 A5 V
2. package MyPackage;: d5 j% l& P' Q% f5 O
3. class SuperClass{0 D5 n6 n L+ A" e* V& q9 [
4. void myMethod(){
6 O: t& u- u7 j/ [- O: u: h5. System.out.println("SuperClass");3 p3 b3 @3 w5 Z! N
6. }4 H6 C3 W/ J+ G$ R9 c u, s
7. }* c- I: Z l- T* ~
8. //File SubClass.java4 g n! k2 }6 Q3 h' m+ o
9. public class SubClass extends SuperClass{
7 ]' U) R- E, {+ b+ E$ E3 B10. public static void main(String[] args) {$ |5 W& _5 v. ^& U. e% G
11. myMethod();
, w5 i* X$ N4 y: p8 A2 u12. } e6 X8 I, B# H1 M$ S7 b
13. }+ w% m& Y/ N1 o+ R. W* f; y2 ?, J) ?
当SuperClass.java和SubClass.java在同一个目录下时,如果编译和运行SunClass.java则会出现什么情况:
* c- b# {; k( g0 i# E$ S; sA. 两个类都可以编译通过,并且SubClass运行时会输出“SuperClass”5 w8 L' y) X: v
B. 两个类都回出现编译时错误
) K7 S2 w$ Z" Q# N* IC. 两个类都可以编译通过,但SubClass会在运行时出现运行时错误
5 w& a% _2 @2 _- n7 z7 Q0 hD. SuperClass.java可以编译通过,但SunClass会出现一个编译错误+ N5 K1 G- ^- B: k1 L3 R7 L( Z/ { T
11. 读程序:6 H b5 y: k. G7 C
1. public class Test{
7 j. i8 ~/ H' B9 L+ h3 i3 `! Y2. public static void main(String args[]){- u2 g8 O2 j4 Z/ _/ F) k
3. int[] i = new int[5];
2 E0 ?9 w* v% ?; l( I9 r4. System.out.println(i[5]);8 B3 J% w8 P6 p
5. } V% m5 f% P+ z2 B9 m( I' ] M
6. }9 C# w; f0 x8 Z, X8 X
当编译和运行该类时,会:4 i x/ Q/ y5 s
A. 出现编译时错误4 B7 e( p9 e- J6 }( j$ k
B. 出现运行时错误* h+ q0 {+ [$ O& _" B# X8 H
C. 输出00 k& C$ `, V' M9 k/ L
D. 输出null
/ r5 B3 \5 G: G7 O" g12. 以下哪个选项是正确的数组声明或初始化:$ Q& W& i$ ~4 k2 O% \& h
A. Array sizes = new Array(4);
; J* f& @/ N' `! T& V' p. TB. double [] sizes = {1, 2, 3, 4};
3 u0 z+ A4 E! `% m8 B& u& D7 {C. int [4] sizes;
?2 F, e' e4 [( q: X% hD. long sizes [] = new [4] long;
. h' D- }! r& L" D13. 读程序:" k& h& \8 Q a! ? _2 c) E
1. public class Initialize{
7 T! @9 a1 @/ s( B! D/ `- X2. int x = 200;; t, L) S/ J- f( K8 {8 _* H u
3. static {
- N* Z' d% u2 ]5 s! b+ H4. int x = 100;6 \( f+ ]6 ^) i
5. }( ]. @8 z' e& u ~7 |
6. public static void main(String[] args){/ J; k- E! ]3 J' @5 B+ O( U
7. Initialize init = new Initialize();3 C- `5 E2 b% h: i, j) t$ H
8. System.out.println(init.x);: A! S& y/ L" q# M% K
9. }" ^0 p4 o) t0 }/ Y; Z
10. }. [+ M* U& M4 ^+ M( {5 u
当编译和运行该类时:6 B9 w' @; X2 T( Q' N5 t9 ~
A. 程序因为第3行产生一个编译错误' i) Q6 |8 R( A+ v
B. 程序因为第4行产生一个运行时错误
9 F Y& ~* p" N2 d1 z0 N" E! J( @C. 程序输出100
3 }9 e4 P# ~3 m$ U8 E/ wD. 程序输出200
' h# T0 P Q6 S, A14. 读程序:
- [% e# X9 A0 l- |0 [1. public int abcd( char x) {
* A$ q1 J' j+ M) t# U2. if ( x <= 'M' ) {
3 ^' _. r% e8 P" C3. if ( x == 'D') # o U; u/ |& D! [
4. return 2;
; h! u3 h2 K6 K- ]+ ~5. return 1;
' `' Q& U/ Q @4 M/ ]6. }
/ D& t) U$ h/ {$ V4 Z4 q. g( _4 r5 ?7. else if( x == 'S') return 3;
1 V& N$ n$ g0 c% f, H5 q" O! t8 H8. else if( x == 'U') return 4;% ^: _, B/ ^+ \( o' U
9. return 0; V+ Z. b7 k+ v3 O$ a
10. }
' L, h( ?3 C8 |3 j; ~7 i在调用abcd(char x)函数时,下面哪个选项作为参数时,会返回12 x/ _( ~! e( ^( w
A.‘X’; i. p9 r3 w4 C
B. ‘A’
- c8 g) L8 u: ~, B6 y7 cC.‘D’
; R Y1 m% w( y, A( k3 OD.‘Z’
! {! i& O+ x! j& e+ M3 s15. 读程序:) y2 A0 \# I9 r% R" H
1. class TryTest {
# i8 D" r6 K$ C* d# V3 Z9 j2. public static void main(String[] args) {/ h1 D) n& U" e4 a N0 `: v
3. try{8 n T2 t% d. n! o0 _
4. System.exit(0);
: `( X, @, u7 ~ [' S5. }
; _; t+ F+ @0 x- Y1 u6. finally {
+ n1 V2 H. t# K1 S0 L7. System.out.println("Finally");! n( ^- s# L. x1 k9 x
8. }* Y" `0 d- F7 n+ A; c0 x# e
9. }8 {/ q# c( P: i
10. }
3 I$ `3 t; F9 l: [ `, l当编译和运行该类时,将:$ f7 {: |" k1 |- H3 t( h. l
A.程序因为第4行出现编译错误
7 w/ Q$ K0 E* ? iB.程序因为第4行出现运行时错误! X0 v( C4 S5 D t' w$ Q
C.程序因为第6行出现编译错误
: X3 ^) ?& s- A2 n0 T0 F+ VD.程序因为第6行出现运行时错误" T4 |. O* R+ j- Z! N ^
E.程序输出“Finally”, G* s5 J8 d n# w9 }
F.程序可以运行,但什么也不打印
6 |9 D1 t6 J/ d
/ w0 r# a6 O! ~6 Z% W# w16. Given the following variable declarations:1 _2 j. x0 ?7 l/ }% _5 W2 u) y* U
Boolean b1 = new Boolean(true);
$ ~( ^3 p# [8 o, o4 S" k. nBoolean b2 = new Boolean(true);. ?8 M" e& ]1 a8 |1 J4 l, ?
Which expression is a legal Java expression that returns true? ' m. Z- ~% b5 P# Z. L2 L
A. b1==b28 f( ~! t$ j! s1 e) }3 k
B. b1.equals(b2)4 K# ~7 e; L+ x# j# y
C. b1&&b2: A' y% W1 ^' |& ?
D. b1||b2
+ i/ I9 I- u/ VE. b1&b28 ^2 N% I) n+ O; @/ K8 y4 F3 f
F. b1|b2" Y8 Z2 e& Y3 ]; L, a
17. Given:
0 t8 L% y+ G9 T q, O! L. Q1 p1. public class Employee {4 ^. z7 P y S9 u
2.
4 R% Q$ h1 W z0 [+ T% H3 }3. }& z/ y) e0 v* x$ n2 R
4.
" Q, G( T4 n( F* ?8 T0 T5. class Manager extends Employee{6 }3 s8 N* j( q" j
6. }; A+ e6 U) L$ P; r: a
What is the relationship between Employee and the Manager?' N L8 ?8 y8 V8 T
A. has a8 V. \# U/ l4 P8 a/ ^# c+ v2 [7 }
B. is a
. G0 k8 I/ A8 p/ SC. both has a and is a- K* O3 q4 a: w3 R- R
D. neither has a nor is a
7 t$ I7 J; e8 b- H7 j5 a7 F18. Which layout manager arranges components from left to right, then top to bottom, centering each row as it moves to the next?/ }2 s' e7 k& m0 M' K
A. BorderLayout
: w( O' C& _) O {& JB. FlowLayout
# ^# j- g' |1 m" {3 sC. CardLayout7 f# a- B' k6 \4 Z0 R) c
D. GridLayout( i; D) C, `! `( f3 n* G9 w, {2 @
19. Which of the following AWT components can generate an ActionEvent?- e4 A9 D. Q$ [
A. java.awt.Button
) k1 L$ s4 w f' @/ G( R+ P8 c* BB. java.awt.Panel
6 E! s% f* `- ?2 FC. java.awt.Canvas) d+ l0 h" ^/ y& Z! J3 P$ r& f
D. java.awt.Scrollbar; {- o- `8 U/ O! T- b' f3 P
20. What kind of Stream is the System.out object?
5 ?5 ~ @1 @9 Z2 a) y/ ^5 QA. java.io.PrintStream7 x, \4 G8 P$ l s" z
B. java.io.TerminalStream
/ n. @) @5 r1 |$ ~7 OC. java.io.FileWriter
' W! J0 O1 W. j* p! A. `! d4 uD. java.io.FileWriter3 x% h: r1 v/ s) U9 v [
21. Which of the following events has a matching adapter class that implements the appropriate listener interface?(双选)( r2 V# D0 m5 ]5 s
A. java.awt.event.FocusEvent
$ C4 y, A! g3 J0 B' Q6 c3 S/ A$ bB. java.awt.event.KeyEvent
) A! J# H, X @- d( _C. java.awt.event.ItemEvent
& D m! t X- c& l1 o' bD. java.awt.event.ActionEvent
! B3 \: r2 h; I$ j5 k22. Which of the following methods are static methods of the Thread class?(双选)
6 n- K5 ]8 ?6 ^A. sleep(ling time)
4 o4 g& g, o. @7 f- p' `3 a5 b0 v. WB. yield()0 O, ~0 B8 ^0 O# C! Y
C. wait(), g' J1 ]& G0 N9 |& o. ^9 k6 e+ f
D. notify() K) z& l3 g4 Q1 U/ I+ a
23. Which method do you have to define if you implement the Runnable interface?( m8 p! m: d P
A. run()9 t- x; e6 ]- s1 L3 S9 P- [4 [) N" I
B. runnable()4 T1 @6 ` a$ `7 R6 j4 x
C. start()7 |& d4 `0 |. L3 \- @, }- q7 ?1 W
D. init()
" z+ v3 ^: s! \: O6 nE. main()
) q, [; }, C8 u/ J+ L. S24. Given:
/ a' P2 ~: }6 }3 {1. class MyThread extends Thread {
" {) |1 Y; e* g; S* e2. # j- y4 h* G2 p
3. public static void main(String [] args) {' L. `+ Z. B+ N5 t& l/ f: v
4. MyThread t = new MyThread();( B* w! r- D% M. @' O& m- g
5. t.run();
- |" D1 p: e' L" O6. }
. \+ k, p, b; R% {) P) p1 F" D7. ; u8 z% U0 n- b, ^. u- l7 X, D
8. public void run() {$ T6 l( s5 B; y! l! e* Q
9. for(int i=1 ; i<3 ; ++i) {9 J) {% p2 k. D% f) S- g0 z
10. System.out.print(i + "..");; e5 z" c9 u y1 |& {
11. }
' X% y+ }9 f% Y9 d9 J* C9 p12. }5 s: `5 |. X4 a
13. }
! i% y, X3 E3 jWhat is the result of this code?C( b5 U! \0 \5 ^6 R9 S& O* c
A. This code will not compile due to line 4! N1 t5 i0 s7 |5 g; r: b+ N
B. This code will not compile due to line 5" W3 p( w0 k0 c. c) m
C. 1..2..
: X, X8 t- W$ w8 L1 ^. ~D. 1..2..3..
; H: Y9 z2 d# ?$ [( n5 G25. Given a local inner class defined inside a method code block, which of the following statements is correct?( h2 y8 r: J! r B* i W5 x# _
A. Any variables declared locally within the method can be accessed by the inner class
( {3 V) \0 }1 t- Z( ZB. Only static variables in the enclosing class can be accessed by the inner class+ n! j: s4 G2 j. Y5 ]4 x- E
C. Only local variables declared final can be accessed by the inner class
1 p' O( @! r) s& hD. Only static variables declared locally can be accessed by the inner class.
7 x' ]' U0 }: j# \- Y
9 H, s' q& X7 C3 D' Q
* G/ K# c& t0 ~2 u& p! e" r: O- ~; r _8 `: L/ S* P) ~7 Q9 m
26.public class SychTest{ # o1 @/ o4 g; _2 `4 W. F
private int x;
[* j d/ e- ?% _/ {. U private int y; ( Y X2 C- w3 c' R) {
public void setX(int i){ x=i;} 9 j6 L% W" G6 M8 F( w
public void setY(int i){y=i;} - r5 K5 p. ~0 k# I
public Synchronized void setXY(int i){ ; q! I: ^3 Y2 ^. P/ |! o' Q
setX(i); $ R: ?9 T, s9 {8 a! G
setY(i);
/ c1 `; r8 @' b: d" a: i }
) [) E" Q$ k: e, c* p public Synchronized boolean check(){ * k; z) I, [% F, u) b5 U A
return x!=y;
4 ^; \. i* M- h' N% H } Z2 N2 z1 s9 y# [
} * F$ Q$ J; g F8 U5 r# Q, h) ?9 @+ A0 @
Under which conditions will check() return true when called from a different class? ' b/ t. y- l+ S$ v- l
A.check() can never return true.
1 N% u# u% Z: ]) H& }# g& l B.check() can return true when setXY is callled by multiple threads.
$ m; } O' {# A& S C.check() can return true when multiple threads call setX and setY separately. $ {. ` o8 d, [0 |
D.check() can only return true if SychTest is changed allow x and y to be set separately.
5 e% V5 X9 l: h, ?' F1 C4 x1 P6 N
27. 1)public class X implements Runnable{ : g* |; v3 U0 O7 X( F. i
2)private int x;
* E; a5 R% \# g8 e8 h 3)private int y;
. p( |4 t" a8 j4 H0 h; m 4)public static void main(String[] args){ " ]2 H; Z7 t' t% R, X7 ?- W) v" Y
5) X that =new X();
) z& h! i$ I, y) G7 J) u! p 6) (new Thread(that)).start(); % B% s' J% x0 H& [3 {# P& d8 R
7) (new Thread(that)).start();
2 V- i- G2 e" Z6 v9 K0 D# p d } ; g) P6 y# }. V$ U0 ~& o
9) public synchronized void run(){ * M+ z( f- S& m' B
10) for(;;){
- w3 J; p; y; d( Z) z3 l5 u8 v 11) x++; 9 s; a# N3 ~5 [7 n* @
12) Y++; : O$ C) _6 H# u& M
13) System.out.println("x="+x+",y="+y); 2 c* _8 q+ R) h6 ^3 Y1 c2 ]7 R, @
14) }
" j, T9 s: W, ?+ Y* s8 l3 x 15) } ' ~1 y$ [; e3 B5 O" i, K7 k
16) } 0 x! Q1 b) A3 l& D0 a3 U
what is the result?
1 H+ w7 g1 s# d- t0 W A.compile error at line 6
4 g5 C& C$ p1 n1 E# I B.the program prints pairs of values for x and y that are 8 Q3 d- Q" i5 |" O
always the same on the same time
4 a. `* V1 Y) R D; A$ u3 Q# x9 A/ ]
5 G' R+ z* s6 S5 U. r28.class A implements Runnable{
) q ^8 J; x* `8 u* a int i;
; Q* R( _7 x5 x; L1 C public void run(){
5 Q& D1 k1 g7 z try{
) F+ U( I) t% j) u5 x- M3 J- r Thread.sleep(5000); : k) d) _! n: [# e- [
i=10; 4 y% O2 N4 p$ F! R
}catch(InterruptException e){} 8 g6 J/ r* ?. i2 u0 a2 p4 r2 j+ q
} ( m- h( N8 A$ g4 h* H8 L
} # P' B$ O: [! A% x
public static void main(String[] args){ ) M7 \; L. @4 C* n! @
try{
5 A E; Q* t% B A a=new A(); % ?# B; u: |3 Z3 }; z
Thread t=new Thread(a);
9 I4 W5 A: {, _, X- ~ H- R4 Z t.start(); * Y. K6 n c) [% O2 ], E; F
17) + ^6 |% ~6 {4 { i+ j4 ~+ H
int j=a.i;
3 r: _. t T8 U' R" I) m 19)
9 N7 H$ \) [! B4 J6 `1 [ }catch(Exception e){} ' Q1 P/ L" a k3 N2 n5 F
} 7 I; J# _2 R0 w+ D: F
}
" s4 x4 K/ Q4 u9 I2 H what be added at line line 17, ensure j=10 at line 19?
) t' w$ G& \# N4 K6 q0 O A. a.wait(); B. t.wait(); C. t.join(); D.t.yield();
6 K2 Z6 ~2 r8 T6 j E.t.notify(); F. a.notify(); G.t.interrupt(); 9 `: ^& f* ~+ U+ v, f$ B8 C9 ]
29.1)public class X{ 4 h: B) s4 m( S, E
2) public static void main(String[] args){
# j* H$ B, ]2 Z( ?1 C3 V 3) String foo="ABCDE"; ( `! T* U) B" J8 i* u
4) foo.substring(3); 5 G% N& ^1 ]( X( ?
5) foo.concat("XYZ");
; [9 H, T6 e- Q( i6 d 6) } ) q# z7 y- c3 d+ x4 C! P9 \* z' y
7) }
7 H5 W( v# a+ m- x what is the value of foo at line 6? $ v. _3 `4 b& y2 |% C" G. j
30. public class Test{
, v3 e$ {$ v7 O d* ] public static void main(String[] args){ * ]5 j7 t6 N( R) }' h
StringBuffer a=new StringBuffer("A");
" F) z! e I8 y; Z# q" P StringBuffer b=new StringBuffer("B"); / f8 _) F" w$ X7 W) ~
operate(a,b);
- I+ A; ]; y# V4 F# h$ F System.out.pintln(a+","+b); ) B$ H- t9 B6 |9 d* }9 U/ q
}
0 z$ H* b+ ]6 q$ ?" F# ~& [" |3 S public static void operate(StringBuffer x, StringBuffer y){ ) E1 M/ _' p0 w' [
x.append(y); 0 d) ?& j% r* T$ d! _
y=x;
5 P$ U8 A0 c; u; b' W0 J+ z1 x } 0 h4 ]9 b. H5 b! Q4 c
} 4 ?& N3 ?+ d1 @6 o
what is the output? |