# Thread: Previous Exam Questions has me..

1. Member Join Date
Nov 2010
Posts
90
Rep Power
0

## 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  Reply With Quote

2. ## 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.  Reply With Quote

3. Member Join Date
Nov 2010
Posts
90
Rep Power
0

##  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  Reply With Quote

4. Senior Member Join Date
Feb 2011
Location
Georgia, USA
Posts
122
Rep Power
0

## 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  Reply With Quote

5. ## 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.  Reply With Quote

6. ##  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.  Reply With Quote

7. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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.  Reply With Quote

8. Member Join Date
Nov 2010
Posts
90
Rep Power
0

## 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;  Reply With Quote

9. ##   Reply With Quote

10. ## Yeah, most of the question had nothing to do with Java and lots to do with algebra. Did you not do algebra at school?  Reply With Quote

11. Member Join Date
Nov 2010
Posts
90
Rep Power
0

##  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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•