Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

somebody help me this question(Operators)

 
eric lee
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile and run it and see what happens!
 
Sanju
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ..
Why ur asking this question...This is compile and run perfectly..result is 1..
 
eric lee
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to clarify the previous:
" which two lines of code - inserted independently, won't compile"
:roll:
 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Alfred. Your explanation was great!
 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dan, sure you didn't need it though
 
eric lee
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Alfred Kemety & Alfred Kemety
i got clear now, thank for helping.
Eric
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Dan Chisholm
Thank for suggestion.
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic