This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Doubt in Sun's 1.4 Practice xam

sekhar variam
Greenhorn
Posts: 15
Hi,,
I have a doubt in the following question from Sun's 1.4 Practice xam..Qn no 2

I think the value of x should be 2, and I verified it by running the code..But according to Sun, the answer is x=4..
Thanks,
Sekhar
[ Jess added UBB [code] tags to preserve whitespace. Check 'em out. ]
[ December 03, 2002: Message edited by: Jessica Sant ]

Arpana Rai
Ranch Hand
Posts: 93
There could be some mistakes in correct answer given.Correct ans. is '2' only.
regds
Arpana

William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6

?
Bill

david eberhardt
Ranch Hand
Posts: 158
the code else if (b = true) {
executes an assignment successfully and returns true

if the code had said else if (b == true) {
then we'd execute the last statement else {
x=4;
}

I'd say Sun goofed it up if the code example given is accurate.
[ December 03, 2002: Message edited by: david eberhardt ]

david eberhardt
Ranch Hand
Posts: 158
the code else if (b = true) {
I am curious:
is the if evaluating whther or not the assignment was successful ?
or what the value of b is after the assignment completes?
I can't narrow it dowm in the JLS yet.

Dirk Schreckmann
Sheriff
Posts: 7023
An assignment has as return value. For such a return value to work as the boolean test in an if-statement, the return value must be a boolean so the assignment must be a boolean.

david eberhardt
Ranch Hand
Posts: 158
Originally posted by Dirk Schreckmann:
An assignment has as return value. For such a return value to work as the boolean test in an if-statement, the return value must be a boolean so the assignment must be a boolean.

ok - so I guess that since b is a boolean type, the assignment depicted returns b, and the if evaluates b - after the assignment.
thanks

Craig Harding
Greenhorn
Posts: 1
I have done many tests with code that uses the assignment operator in this case instead of == .
I have concluded that in any situation where the var=true is used the variable is set to true and the code will meet the condition.
When var=false is used, the variable is set to false but the codition is never met.
I agree with Sun and there answer on this question.

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
Originally posted by Craig Harding:
I agree with Sun and there answer on this question.
But if you run it you will see that Sun is incorrect.

david eberhardt
Ranch Hand
Posts: 158
Originally posted by Thomas Paul:
But if you run it you will see that Sun is incorrect.

I agree.
I have gotten used to copying code examples presented here, such as the one that sekhar posted here to start this thread, and compiling and running them for myself to see what happens.
With sekhar's example, indeed it appears that the correct answer is different than what sekhar says that Sun said ....
you get the picture ... this is dependent upon sekhar having provided us with exact duplication of the code, etc.
As was mentioned by William Brogden above, maybe part of the code was presented to us in error - we don't know for sure so we go by what is presented to us.
Anyways, I use these opportunities to play with the code and see what happens.
I have learned alot by trying out my hand at answering questions, after digging into the books, etc ... and also receiving feedback from the experienced folks around here!
Thank you.
[ December 03, 2002: Message edited by: david eberhardt ]

david eberhardt
Ranch Hand
Posts: 158
Originally posted by Craig Harding:
I have done many tests with code that uses the assignment operator in this case instead of == .
I have concluded that in any situation where the var=true is used the variable is set to true and the code will meet the condition.

else if (b = true) {
x=2;
should lead you to agree that 2 is the right answer as sekhar suggested.

sekhar variam
Greenhorn
Posts: 15
I am sorry guyz...I guess i goofed it a bit...
The condition is if(b=false) not if(b=true)..I cross verified it once again today morning...But this discussion did clear one misunderstanding I had....
I thought that if(b=false) and if(b=true) both returned true because both the assignments were successful..But I never considered that b is assigned to false and then the if condition is evaluated...
Thanks all of you for the viewpoints....and sorry for the error...Sun's answer is right..
Thanks,
sekhar

david eberhardt
Ranch Hand
Posts: 158
sekhar,
Very good information update.
Yes, it now appears that the Sun answer is correct based upon this new information from you...

... however, the whole discussion was very good to review.
thanks for the update.

Bert Bates
author
Sheriff
Posts: 8898
5
You guys had me scared for a minute... ( I wrote that question )