0:00

Given the method defined below what does the following print: mystery(1234)?

public static void mystery (int x) {
   System.out.print(x % 10);

   if ((x / 10) != 0) {
      mystery(x / 10);
   }
   System.out.print(x % 10);
}
注意在递归前后各有一个print,故是先倒序再顺序。
4

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);
}
结果是3的五次方。
1

Given the following method declaration, what value is returned as the result of the call product(5)?

public static int product(int n)
{
   if (n <= 1)
      return 1;
   else
      return n * product(n - 2);
}
结果是\(5\times3\times1=15\)。
5

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);
}
f(2)1f(3)2f(4)3f(5)5
4


陈 欣

AADPS创始人

发表评论