aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Sun's  1.4 Practice xam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Sun Watch "Doubt in Sun New topic
Author

Doubt in Sun's 1.4 Practice xam

sekhar variam
Greenhorn

Joined: Nov 09, 2001
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 ]

SCJP4(96%)
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
There could be some mistakes in correct answer given.Correct ans. is '2' only.
regds
Arpana


SCJP1.4(91%)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Are you sure that is a single equals sign in

?
Bill
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
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

Joined: Jul 02, 2002
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

Joined: Dec 10, 2001
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
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

Joined: Dec 03, 2002
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

Joined: May 05, 2000
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
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

Joined: Jul 02, 2002
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.

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

Joined: Nov 09, 2001
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

Joined: Jul 02, 2002
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

Joined: Oct 14, 2002
Posts: 8829
    
    5
You guys had me scared for a minute... ( I wrote that question )


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Sun's 1.4 Practice xam