Medium Multiple Choice QuestionsΒΆ

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

11-7-1: Given the following method declaration, which of the following is printed as the result of the call mystery(1234)?

//precondition:  x >=0
public void mystery (int x)
{
   System.out.print(x % 10);

   if ((x / 10) != 0)
   {
      mystery(x / 10);
   }
   System.out.print(x % 10);
}






11-7-2: Given the following method declaration, what value is returned as the result of the call mystery(5)?

public static int mystery(int n)
{
   if (n == 0)
      return 1;
   else
      return 3 * mystery (n - 1);
}






11-7-3: Given the following method declaration, what value is returned as the result of the call product(5)?

public int product(int n)
{
   if (n <= 1)
      return 1;
   else
      return n * product(n - 2);
}






11-7-4: Given the following method declaration, what value is returned as the result of the call f(5)?

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);
}