The first call to mystery with the integer 1234 will print 1234 % 10. The β%β means remainder. The remainder of 1234 divided by 10 is 4 so the first thing printed must be 4.
This has a recursive call which means that the method calls itself when (x / 10) is greater than or equal to zero. Each time the method is called it prints the remainder of the passed value divided by 10 and then calls the method again with the result of the integer division of the passed number by 10 (which throws away the decimal part). After the recursion stops by (x / 10) == 0 the method will print the remainder of the passed value divided by 10 again.
The first call to mystery with the integer 1234 will print 1234 % 10. The β%β means remainder. The remainder of 1234 divided by 10 is 4 so the first thing printed must be 4.
The first call to mystery with the integer 1234 will print 1234 % 10. The β%β means remainder. The remainder of 1234 divided by 10 is 4 so the first thing printed must be 4.
Many digits are printed due to infinite recursion.
When the recursive call to mystery(1) occurs (the 4th call to mystery), the division of x /10 equals .01--this becomes 0 because this is integer division and the remainder is thrown away. Therefore the current call will be completed and all of the previous calls to mystery will be completed.
For the call mystery(5), n != 0 so the else statement is executed. This results in the next recursive call of mystery(4). This will continue until the call mystery(0) is executed. At this point, the value 1 will be returned. Then each call of mystery can return with the 3 * the result of the recursive call. So this method will compute 3 to the given power.
If you assume the purpose of the method is to compute n * 2, this is correct, but the product method does not do this. Be sure to trace the code to see what happens.
If you assume the purpose of the method is to compute n * n this is correct, but the product method does not do this. Be sure to trace the code to see what happens.
If you assume the purpose of the method is to compute n ^ n, this would be correct. But product does not do this. Be sure to trace the code to see what happens.
The first time the method is called, i is not equal to 0, so the method makes a recursive call to itself, with the value of 82/3 which equals 27 due to integer division. This is still not equal to 0, so the method calls itself with the first parameter equal to 9, then 3, then 1. Finally, the method is called with the first parameter of 1/3 which equals 0 due to integer division which throws away any decimal part. Each method call adds 1 to the result, except for the final call when i is equal to 0.
Infinite recursion would happen if the method never reached its base case where i is equal to 0. This would be true if the division could result in a constantly shrinking fraction, but integer division truncates the fractional portion of the division.