• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

sargamock2

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Line 11 says flag=true and hence the value of flag = false is changed to flag =true. Hence, the if condition is true and hence the o.p will be 002 flag is true. Hence the answer d(none)
Hope that helps
Ashish
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not a Java Guru, but I'll take a guess at your second question.
When you perform a method, you only pass references to variables - not the original variables. And then you don't automatically pass the values back to the originating method.
While you're performing method swap(), you have variables 'a' and 'b' -- but these are NOT the same as the 'a' and 'b' used in your main() method. They're just copies, and never get returned to the main() method.
Here, this might help:
main()
a = one
b = two
swap()
copy of a (also named a) = one
copy of b (also named b) = two
THEN:
copy of a = onemore
copy of b = copy of a = one more
THEN you return to main() -- but the copies (a and b) are gone. And you have only your original a and b which are STILL:
a = one
b = two
Someone please correct me, if I'm wrong about this....
Susan

[This message has been edited by Susan Delph (edited May 10, 2001).]
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Susan,
I am sorry you are wrong! When you a pass an object to a method,
no doubt, you pass it by refernce,but whatever changes you make in the function gets reflected in the ORIGINAL COPY.
So the answer to Second question is
a= "One more"
b= "Two"
Well, whenever you have a doubt please rely on the BEST TEACHER..
Yeah the COMPILER.
Ravindra Mohan.
 
Susan Delph
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So then... why isn't b = one more?
I added some println's to the code:
This is the output I got:
In swap: a= One, b= Two
End of swap: a= One more, b= One more
a is One more
b is Two

Does it have something to do with the append() method, versus using b=a?
Susan

[This message has been edited by Susan Delph (edited May 11, 2001).]
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Somebody clear that why is not b onemore. Somehow I could not make it out with the discussions given???
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is my understanding...Correct me if I am wrong.
When you pass an object to a method, a copy of the reference to that object will be passed. However, the copy of the reference is still pointing to the original object.
1.a = "one" //reference a is pointing to the string "one"
2.when you pass a to a method, a "copy of reference a" is made and it is given to the method. Remember, it is also pointing to "one"
So, when we say, a.append in a method, we are actually appending the object that "copy of reference a" is currently referring to..in this case it is the same as "original reference a i.e "one".
3.But, when you say b=a, you are actually making the "copy of reference b" point to value in a. However your "original reference b" is still pointing to "two" . Now your "copy of reference b" and your "original reference b" are pointing to two different things.
So, when you print the value whatever the "original reference b" is pointing to will be printed..that is "two" in this case.
Drawing a diagram to visualize this helps.
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madhu,
Thanks. It is clear now. Heard real exam is stressing more on Sbuffers and strings.
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi madhu,
If u want to make it more clear,do one thing,rather than assigning a to b assign null to b even then your orignial function will show u the value "two".The reson being that u have changed the reference to null but the original reference or u can say alias is still there which is pointing to the object and therefore shows u the value "two".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic