# Previous Exam Questions has me..

• 05-12-2011, 08:18 AM
maknib
Previous Exam Questions has me..
Looking at a past exam and a question has me stumped...

Given that x and y are both variables of type in, the statemennts:
y = x + x;
y += y+y;

are equivilent to

a) y= 2 * x;
b) y = 4 * x;
c) y = 6 * x;
d) y = 8 * x;

i have no idea how this works out.. all i can guess is A because there is 2 x's

if somoene could help and explain how this works it would be appreciated.

thanks
• 05-12-2011, 08:26 AM
sunde887
Is the question, what will the final value of y be after these two expressions? I'd like you to try to think through it and see what you think it is. First calculate y in the first statement, then use the second statement to calculate the changed y. Show us your work and we will help you out.
• 05-12-2011, 08:29 AM
maknib
Quote:

Originally Posted by sunde887
Is the question, what will the final value of y be after these two expressions? I'd like you to try to think through it and see what you think it is. First calculate y in the first statement, then use the second statement to calculate the changed y. Show us your work and we will help you out.

thats the point, i dont understand how to calculate it what i am given no values for x or y.
it just says that the first 2 statment is equivilent to the below somehow
• 05-12-2011, 08:32 AM
yellowledbet
set x equal to a number for instance 2

y = 2 + 2 = 4

plug in the values for y

y += 4 + 4 = 12 (you are summing the expression (4 + 4 = 8) with the value of y (4) because of the += operator

you should be able to figure out the rest
• 05-12-2011, 08:32 AM
sunde887
Let's make up our own values. Solve for x = 2. After solving for x is 2 you will have an answer and you can verify it by using different values of x, perhaps the do x= 3,4,5,6,7,etc.
• 05-12-2011, 08:32 AM
Junky
Quote:

Originally Posted by maknib
thats the point, i dont understand how to calculate it what i am given no values for x or y.
it just says that the first 2 statment is equivilent to the below somehow

OMG!

Just substitute any value for x and calculate the end value of y. Then see which of the 4 multiple choice answers gives the same answer.
• 05-12-2011, 08:35 AM
pbrockway2
The "rules" are tiresome but everything has to be defined! If the result you get doesn't correspond to your intuition the JLS section 15.26.2 Compound Assignment Operators might (or might not) prove useful.
• 05-12-2011, 08:36 AM
maknib
ooooo i think i got it...
y = 6 * x;

SO.... if x = 10;

y = x + x;
y += y+y;

is
y = 10 + 10 ...y = 20
20 + (20+20)... 20 + 40.. is 60

or..

y = 6 * 10;
• 05-12-2011, 08:38 AM
sunde887
• 05-12-2011, 08:38 AM
Junky
Yeah, most of the question had nothing to do with Java and lots to do with algebra. Did you not do algebra at school?
• 05-12-2011, 08:42 AM
maknib
Quote:

Originally Posted by Junky
Yeah, most of the question had nothing to do with Java and lots to do with algebra. Did you not do algebra at school?

I have not been at school or touched anything more than basic addition in maths for over 10 years.

Will do Sundae, thanks to you and the others for the help :)
as soon as i wrote my first reply i was like Oh right i get it now. been a long week studying 3 assignments due and full time work and exams next week.. brain dead hehe