aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Operator C Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's Operator C Exam

Saniya Ansari
Ranch Hand

Joined: Sep 30, 2002
Posts: 48
class T {
public static void main(String args[]) {
String a = "1";
byte b = 2;
short c = 3;
char d = 4;
int e = 5;
float f = 2;
a += b *= c += d *= e += f;
System.out.print(a);
}
}

The answer is 162 .. cant' figure out why its right associative and how can one rewrite it..
And wont the float=2.0 give a compile time error coz it should be float = 2.0f.. but ignoring that still can someone help!
secondly..
class C {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
public static void main(String s[]) {
int i = 1;
m(m(++i) - m(i++) + ~m(-i) * -m(~i));
}
}
The answer is 2, 2, -3, -4, 8... i can go upto -3 but how do we get -4. coz for -m(~i) we send the value ~3 which is 0.
Where am i going wrong..??


SCJP 2
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Saniya:
This code might help you. I guess in a multiple assignment situation, it starts from right most first and then goes left. I have broken the code to make the output clear. It reassign all the original values before printing next stage:

[ October 07, 2002: Message edited by: Barkat Mardhani ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Saniya:
For your second question, run following code and observe the result. Note that post-script (i++) and pre-script(++i) operators actually change the value of i. However uniry operator (-i) and (~i) do not change the actual value of i. They only send modified value to the called method.

[ October 07, 2002: Message edited by: Barkat Mardhani ]
[ October 07, 2002: Message edited by: Barkat Mardhani ]
[ October 07, 2002: Message edited by: Barkat Mardhani ]
Saniya Ansari
Ranch Hand

Joined: Sep 30, 2002
Posts: 48
Thanks Barkat.. i think it was really helpful!
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
I just updated the remark associated with the answer.

The compound assignment operator is right associative so the expression is evaluated from right to left. The result is a String value that can be calculated using the following expression. "1"+(2*(3+(4*(5+2))))

Is this new remark an adequate explanation?


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

Joined: Aug 05, 2002
Posts: 787
But Dan for following statement:
m(m(++i) - m(i++) + ~m(-i) * -m(~i));
Why is it not executing third and fourth m() calls first as * has more priority over + or -?
Saniya Ansari
Ranch Hand

Joined: Sep 30, 2002
Posts: 48
HI Barkat..
I think its becoz parenthesis have a higher precedence than any operators.. so first the parenthesis are evaluated and then only others.
Dan i think it explains it much better! Thanks!
[ October 08, 2002: Message edited by: Saniya Ansari ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's Operator C Exam