**This book is now obsolete** Please use CSAwesome instead.

# 12.10. Medium Multiple Choice QuestionsΒΆ

These problems are similar to those you will see on the AP CS A exam.

- 1441
- The first call to
`mystery`

with the integer 1234 will print 1234 % 10. The '%' means modulus or remainder. The remainder of 1234 divided by 10 is 4 so the first thing printed must be 4. - 43211234
- 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. - 3443
- The first call to
`mystery`

with the integer 1234 will print 1234 % 10. The '%' means modulus or remainder. The remainder of 1234 divided by 10 is 4 so the first thing printed must be 4. - 12344321
- The first call to
`mystery`

with the integer 1234 will print 1234 % 10. The '%' means modulus or 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.

12-8-1: Given the following method declaration, which of the following is printed as the result of the call `mystery(1234)`

?

1 2 3 4 5 6 7 8 9 10 11 | ```
//precondition: x >=0
public static void mystery (int x)
{
System.out.print(x % 10);
if ((x / 10) != 0)
{
mystery(x / 10);
}
System.out.print(x % 10);
}
``` |

You can step through the code using the Java Visualizer by clicking on the following link: Q-11-7-1.

- 243
- 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. - 0
- This can never be 0 because the stopping condition returns 1 when you call
`mystery(0)`

- 3
- This would only be true if you called
`mystery(1)`

- 81
- This would be true if you called
`mystery(4)`

- 27
- This would be true if you called
`mystery(3)`

12-8-2: Given the following method declaration, what value is returned as the result of the call `mystery(5)`

?

1 2 3 4 5 6 7 | ```
public static int mystery(int n)
{
if (n == 0)
return 1;
else
return 3 * mystery (n - 1);
}
``` |

You can step through the code using the Java Visualizer by clicking on the following link: Q-11-7-2.

- 1
- The value 1 will only be returned when the initial call to product is less than or equal to 1.
- 10
- 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. - 25
- 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. - 3125
- 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. - 15
- The result from
`product(5)`

is`5 * product(3)`

which is 3 * product(1) which is`1`

so the answer is`1 * 3 * 5 = 15`

.

12-8-3: Given the following method declaration, what value is returned as the result of the call `product(5)`

?

1 2 3 4 5 6 7 | ```
public static int product(int n)
{
if (n <= 1)
return 1;
else
return n * product(n - 2);
}
``` |

You can step through the code using the Java Visualizer by clicking on the following link: Q11-7-3.

- 8
- This would be true if it was
`f(6)`

not`f(5)`

. - 3
- This would be true if it was
`f(4)`

not`f(5)`

. - There is no result because of infinite recursion.
- This method will stop when
`n`

equals`0`

or`1`

. - 5
- This is the Fibonacci method which returns
`0`

for`0`

and`1`

for`1`

and`Fibonacci(n-1) + Fibonacci(n-2)`

for the rest of the numbers. - 0
- This would be true if it was
`f(0)`

not`f(5)`

.

12-8-4: Given the following method declaration, what value is returned as the result of the call `f(5)`

?

1 2 3 4 5 6 7 8 | ```
public static int f(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else return f(n-1) + f(n-2);
}
``` |

You can step through the code using the Java Visualizer by clicking on the following link: Q11-7-4.