Results 1 to 11 of 11
  1. #1
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default 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

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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

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

    Default

    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

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by maknib View Post
    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.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  8. #8
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    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;

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yup, good job. If you are done, please mark your thread solved. If you have more questions feel free to ask more.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

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

  11. #11
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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

Similar Threads

  1. next and previous canot be same
    By isme in forum New To Java
    Replies: 0
    Last Post: 12-15-2010, 01:05 AM
  2. Next and Previous Buttons
    By kbgriggs in forum New To Java
    Replies: 2
    Last Post: 08-12-2009, 02:08 PM
  3. Exam Practice Questions
    By AWPtic in forum New To Java
    Replies: 24
    Last Post: 04-18-2009, 03:50 PM
  4. Next and Previous Buttons
    By JavaNewb in forum New To Java
    Replies: 1
    Last Post: 05-09-2008, 01:23 AM
  5. SCBCD 5.0 Mock Exam Questions
    By kks_krishna in forum Reviews / Advertising
    Replies: 0
    Last Post: 04-02-2008, 08:26 AM

Posting Permissions

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