• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Sun's 1.4 Practice xam

 
sekhar variam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that is a single equals sign in

?
Bill
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

If I read you correctly, then your statement above applied to:
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys had me scared for a minute... ( I wrote that question )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic