1) For what value of flag the following program will ouput "003 Flag is false"
01: public class Base{
02:
03: private void
test() {
04:
05: boolean flag = false;
06:
07: if(flag==true){
08: System.out.println("001 Flag is true");
09: }
10: else {
11: if (flag=true)
12: System.out.println("002 Flag is true");
13: else
14: System.out.println("003 Flag is false");
15: }
16:
17: }
18:
19: static public void main(
String[] a) {
20: new Base().test();
21: }
22:
23: }
Select most appropriate answer.
a) true
b) false
c) 0
d) None
The answer according to them is d but when i compiled with flag = false in line 11 it gives the required output that 003 flag is false.
2)
public class Test {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Test.swap(a,b);
System.out.println("a is "+ a +"\nb is " + b);
}
static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");
b=a;
}
}
What will be the output?
Here output is onemore and two. But when u assign a to b in b=a why not b changed to onemore.
Please explain.