225b. Consider the following code: What will be printed?) I& a6 S; Y7 D9 J
public class Scope{
) m* P& I- i& \private int i;) Z; x+ ^% T* j `8 l
public static void main(String arg[]){
8 `8 k; [2 Q, e* V5 [( HScope s = new Scope();
) r# n% U) Y. s( x7 ^- Es.amethod();
4 r* h1 \$ U3 e/ i- E}
. c: M" U3 L! V+ H# Y5 w2 ppublic void amethod(){
# X8 z& T0 A1 r+ ^+ MSystem.out.println(i);1 g7 E& G) n2 v+ @6 Y$ H
}
7 ~ B' {9 h. o. v}/ ?+ @2 K5 F* p) Z4 Z r. P0 }2 P
A. 0
, r& Y* w# T9 n% p/ y3 v, l4 QB. null
C3 L) l0 o0 ?( Y& R- TC. Error. Non-static variable i cannot be referenced from a static context.6 O& n5 H: Z' N& L$ p
D. Error. Variable i may not have been initialized.
0 [* }/ [9 l' J9 C. aA. (check)
0 a' b' T, }% z5 }, C226. Consider the following code: What will be printed?
4 P: w/ D5 F' L- h6 upublic class StringBuf{
% k- _5 |9 K4 p2 H6 x- ]% N' m1 Dpublic static void main(String args[]){
0 C" z5 S8 y; d6 V2 q+ A( d1 H% fStringBuffer sb = new StringBuffer("abc ");
+ L' i* g. y0 ~2 E% j6 k7 YString s = new String(" a b c");
7 I4 h* D& ] r, N1 IString st = "ABCDE";% q: u6 W. w( M; b9 E
sb.append("def ");//nu är s = abc def
2 j$ Q3 G* _! X# jsb.insert(1, " zzz ");//nu är s = a zzz bc def
3 l: \0 j! I4 B- r e9 _6 @String i = s.concat(st);
3 k! E# C6 z: P5 {8 s3 w5 e; Ds = s.trim();
0 x2 X0 r7 j+ |/ P: b0 W+ ySystem.out.println(s);& i$ v0 h( p3 W7 I
System.out.println(sb);" a6 Z/ Y W! ^; C+ X4 {) {
System.out.println(st.indexOf("C"));, p" J8 r* e0 d& M
System.out.println(st.indexOf('A'));
9 [) R3 Q! ~' S5 m; w. LSystem.out.println(st.indexOf('A', 2));
# d/ x$ K3 Z9 {: q7 K3 F" ~( FSystem.out.println(st.indexOf('G'));
% _' N5 ?. L2 ^! i; Q8 N, Q- _+ rSystem.out.println(i);- w+ b+ p& g+ [' P1 I) T
}
% P/ n; G6 U: G! s) x. s9 h6 Z}
* f( x7 y; e( b* W2 z+ ~0 y3 @- s7 _What will be printed?
1 T! A2 f3 P; R7 m, W! u- o* }A.) Z; T3 ], l1 o4 d: C# \- N I0 r( g
abc6 u( ~2 m0 M" t2 w
azzz bc def- Z6 q2 d: ?( w( _0 `& x# ]0 v
2
' I9 h. L$ Q4 d2 {- J06 M9 Z' K" g) J; N
-1
2 V5 U) c6 m( c' n-1
: A8 e* z8 `6 Y* x5 r3 D4 Ka b cABCDE
3 k8 g! J+ F0 P' x( d( |) w$ @B.
; g' G5 u. Z; m- o: Ma b c# d/ a \" }" c6 y8 \7 h: F5 @
abc def zzz' {0 f: X# n% ~* P6 ]
2" J X" w" y Q
00 b( |, z* l( V5 v6 U, j6 k
-1' W! H" j9 x: w: m' N% u$ n- E- h
-17 Q/ n0 V: z; ~" P' u
a b c ABCDE
; j9 O/ Q7 b8 L$ o# X% EC.
4 L# F6 V6 o, A6 O" _* E3 G4 H$ vabc) b6 e6 c, X5 x2 j6 n6 ~/ L
abcdefzzz
Z' l1 f7 L% r: j. e8 G/ l2
* A m- Y: a0 {( R, N' i( L3 ~0
, b3 X1 B$ I2 u5 o( z% j8 L1
) ^: ^% A& `/ O$ U# d5 H-1' M5 M# G% P6 q A
a b c ABCDE) ?* y a' p/ a
D.9 B& @ t/ |' x4 d% O
a b c
3 b" c! n; V5 Z* v% L( M) I0 za zzz bc def
) n# W6 u' m' r$ R: c2
' P$ I7 e5 w, [02 r( a% q4 [" }: ?: V
-1
, |# f! D5 E) M5 P6 H-1% s6 P! C$ k4 f5 h/ ~. z7 J
a b cABCDE1 h/ K- E) f0 f6 V
D. |