Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Practice MT2: Q8

+6 votes
What is the answer to question 8 about the mystery method?

Can you trace out the values of the variables along the way?
asked Jan 25, 2015 in (Winter 14) by Forrest Stonedahl (100 points)

1 Answer

+2 votes

We start the program wondering what the variable a is. we pass in 2 and 5 as the parameters for the method "mystery" which will return an integer.

When we run "int a = mystery(2,5);" what the mystery method returns will be set to the variable 'a'

2 is passed in as 'x' and 5 is passed as 'y'..... x then gets one added to it so now x = 3... y now become 5 + 3 so y = 8. the system prints "3 8"

next, the program returns x which is 3 and sets that value to 'a'.

a is now 3...

we then run "int b = mystery(a +3, a - 2)" this will pass in 6 as x and 1 as y for the mystery method. What is returned will be assigned to b...

x++ adds 1 to x, so x is now 7. y then becomes 4 after we add 3 to 1.

the system then prints "7 4" and assigns 7 to 'b'

b is now 7. and the system has printed:

.............

3 8

7 4

................

we then print 'a' a space then 'b' 

so we now have

.............

3 8

7 4

3 7

............

we then set x to 5 and y to 9.. THESE ARE DIFFERENT THAN THE X AND Y IN THE "MYSTERY METHOD"!!!!

we then run "y = mystery(y, x);" which passes in 9 for x and 5 for y. what is returned will be set to 'y' in the main method.

x++ adds 1 to x so now x = 10.

y = y + 3 adds 3 to 5 so now y = 8

the system prints "10 8" then sets 'y' to 10.

we then print "5 10"

our final system print is:

................

3 8

7 4

3 7

10 8

5 10

.............

answered Jan 26, 2015 by Ethan Blatti (100 points)
Looks good...
...