Hi..
This is a question from Dan Chisholm's single topic questions...
class MWC205 {
static void m1(StringBuffer s1) {
s1.append("B"); System.out.print(s1);
}
static void m2(StringBuffer s1) {
s1 = new StringBuffer("C"); System.out.print(s1);
}
public static void main(
String[] s) {
StringBuffer s1 = new StringBuffer("A");
m1(s1); m2(s1);
System.out.print(s1);
}}
What is the result of attempting to compile and run the program?
a. Prints: AAA
b. Prints: ABCA
c. Prints: ABCAB
d. Prints: ABCABC
e. Prints: ABCAC
f. Prints: ABABCABC
g. Compile-time error
h. Run-time error
i. None of the above
Answer : C ..
My doubt is that in method 'm1' , 'AB' is printed..
in method 'm2' , 'c' is printed...
And Stringbuffers are immutable.
So why is 'AB' printed as a result of the print statement in the main()
method?
Why cant it be "C"?
Please anyone reply me..
Thanks in advance..
Regards..
rajani.