aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes somebody help me this question(Operators) 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 "somebody help me this question(Operators)" Watch "somebody help me this question(Operators)" New topic
Author

somebody help me this question(Operators)

eric lee
Ranch Hand

Joined: Nov 04, 2002
Posts: 86
What is the result of attempting to compile and run the above program?
class F {
public static void main (String []s) {
int i = 1;
i += ~i - -i * ++i + i-- % ++i * i++;
System.out.print(i);
}
}
a. Prints: -1
b. Prints: 0
c. Prints: 1
d. Prints: 2
e. Prints: 3
f. Prints: 4
g. Runtime error
h. Compiler error
i. None of the above
Steve Schowiak
Greenhorn

Joined: Oct 21, 2002
Posts: 20
Compile and run it and see what happens!


I am not insane. The voices in my head told me so.
Sanju
Ranch Hand

Joined: Jul 15, 2002
Posts: 53
hi ..
Why ur asking this question...This is compile and run perfectly..result is 1..


Thank you,<br />Sanju
eric lee
Ranch Hand

Joined: Nov 04, 2002
Posts: 86
i mean, how to figure out answer without using computer, i am going to take a scjp exam in the furture. Thanks
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
The exam doesn't spend very much time on precedence. I think your sample is WAY harder than anything you'll find on the test concerning precedence, however you might find something like the following on the test...
given:
14. long test (int x, float y) {
15. // insert code here
16. }
which line of code inserted at line 15 won't compile?
A. return x;
B. return (long) x / y;
C. return (long) y;
D. return (int) 3.14d;
E. return ( y / x );
F. return x / 7;

this question comes from the cowgirl's upcoming cert book.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
to clarify the previous:
" which two lines of code - inserted independently, won't compile"
:roll:
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Bert B,E
Eric:
i += ~i - -i * ++i + i-- % ++i * i++;
A- += is changed to implicit cast and i + the right hand side:
i = (int) (i + ~i - -i * ++i + i-- % ++i * i++);
B- Precedence is made:
i = (int) (i + ~i - (-i * ++i) + (i-- % ++i * i++));
C- Evaluation of operands: (not sure if this happens before precedence or after)
i = (int) (1 + -2 - (-1 * 2) + (2 % 2 * 2));
D- Calculations:
i = (int) (1 + -2 - (-2) + (0 * 2));
E- More calculations:
i = (int) (1 + -2 - -2 + 0);
then i is assigned the result which is 1
HTH


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Thank you Alfred. Your explanation was great!


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Thanks Dan, sure you didn't need it though
eric lee
Ranch Hand

Joined: Nov 04, 2002
Posts: 86
Hi,Alfred Kemety, i still confuse your part c
------------------------------------------------
C- Evaluation of operands: (not sure if this happens before precedence or after)
i = (int) (1 + -2 - (-1 * 2) + (2 % 2 * 2));
is i not i like below?
i = (int) (1 + -2 - (-1 * 2) + (2 % 3 * 2));
thanks.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Eric,
Alfred's explanation is correct. A good way to prove it is to print the result of each step. Each should be the same.
The parenthesis in my explanation are a little different from Alfred's because I am trying to demonstrate that Java parses the expression from left to right. The JVM is not able to see the entire expression in a single look as we can.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Well, I guess you missed with the ++i and the i++, during the evaluation process, i++ is different from ++i.
if i = 2 and you write x = i++;
first the current value of i is inserted into the equation (2) then the value of i is increemented to be (3), so you end up with (x = 2 ;) and i is 3
BUT if i = 2 and you write x = ++i, then i is first increemented to be 3, THEN the value is inserted into the equation so you end up with (x = 3 ;) and i is also 3
that's why my evaluation on step C is right...
[ November 05, 2002: Message edited by: Alfred Kemety ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Eric and Alfred -
Alfred - you are right about my question! I swear to you guys - eric's question is way too hard. i passed the 1.4 beta (160) questions, and i helped write Sun's epractice exam (120 questions). My advice is, unless you have every other topic absolutely nailed down cold - you should be spending your study time in other areas - you know way more than you need to about precedence if your only goal is to pass the test.
On the other hand, it is a fascinating question - and I learned a lot watchingyou guys solve it -so from the aspect of curiousity for the sake of curiousity it's a great question!
eric lee
Ranch Hand

Joined: Nov 04, 2002
Posts: 86
Hi,Alfred Kemety & Alfred Kemety
i got clear now, thank for helping.
Eric
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Bert Bates:
...you should be spending your study time in other areas - you know way more than you need to about precedence if your only goal is to pass the test.

If I remember correctly, 52% is a passing score on the 1.4 version of the exam. I agree that the real exam doesn't put any emphasis on operator precedence. If you are short on time, then just skip operators entirely and it won't have much effect on your score. On the other hand, if you want to learn how Java actually evaluates expressions, then you will need to spend a little time solving a few.
eric lee
Ranch Hand

Joined: Nov 04, 2002
Posts: 86
Hi,Dan Chisholm
Thank for suggestion.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Originally posted by Dan Chisholm:

If you are short on time, then just skip operators entirely and it won't have much effect on your score.

Whoa Boy!
I gotta disagree big time -- there are a LOT of questions involving operators, and it might be tough to pass without understanding them, especially pre/post-increment/decrement.
But for precedence, Bert's right. We had a pretty solid agreement that we should NOT test much at all on precedence because the feeling was that everyone should just use parens and be done with it, and that it was mental overhead that programmers normally wouldn't need.
You can debate that decision all day if you want, but that's the story for the exam
With respect to precedence, the ones that matter are:
* anything related to casting
* String concatenation
* pre/post increment/decrement (but then, they have precedence built-in anyway)
Dan made a great point about learning more about how Java does stuff, but when I see a question like the one this thread started with, I just wonder how many exam candidates are eating up scarce brain bandwidth, when they have -- as we say -- bigger fish to fry. AFTER the exam, well then it's a different story.
*After* the exam is when you sit down and study all the extra stuff you want to understand. [yeah, right]
Or, in my case, you hold a sacrificial burning of your books and flashcards, and vow never to learn another thing as long as you live.
cheers,
Kathy
currently working on the "spare the brain" campaign, and dedicated to helping Java programmers and test-takers be as lazy as possible.


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: somebody help me this question(Operators)