Question

In: Computer Science

Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...

Consider the following recursive method in Java

public static int mystery(int n)

{

  if (n == 0)

  return 1;

   else   

return 4 * mystery (n - 1);  

}

What is the output of  mystery (3) using the code segment above

Show your work on your trace file

Solutions

Expert Solution

Step1

mystery (3) 3 is passed to mystery() method

public static int mystery(int n) n store the value 3 n = 3

{

  if (n == 0) n value is not 0, False execute else part

  return 1;

   else   

return 4 * mystery (n - 1); 4 * mystery(3-1) ===> 4 * mystery(2), again calling mystery() method, passing 2

}

Step2

public static int mystery(int n) n store the value 2, n = 2

{

  if (n == 0) n value is not 0, False execute else part

  return 1;

   else   

return 4 * mystery (n - 1); 4 * mystery(2-1) ===> 4 * mystery(1), again calling mystery() method, passing 1

}

Step3

public static int mystery(int n) n store the value 1, n = 1

{

  if (n == 0) n value is not 0, False execute else part

  return 1;

   else   

return 4 * mystery (n - 1); 4 * mystery(1-1) ===> 4 * mystery(0), again calling mystery() method, passing 0

}

Step4

public static int mystery(int n) n store the value 0, n = 0

{

  if (n == 0) n value is 0, True execute if block part

  return 1; return 1 to step3 mystery(0)

   else   

}

4 * mystery(0);

= 4 * 1

4 return to step2 mystery(1)

4 * mystery(1)

= 4 * 4

16 return to step1 mystery(2)

4 * mystery(2)

4 * 16

= 64

NOTE: If you have any doubt, please let me know through comments; I will surely revert back to you.

Please give a up vote .....
Thank you...


Related Solutions

(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
1) Consider the following Java method. Which term best describes what this method computes? static int...
1) Consider the following Java method. Which term best describes what this method computes? static int doSomething(int[] a) {     int b = a[0];     for (int c : a) if (b > c) b = c;     return b; } a. average b. maximum c. minimum d. sum e. transpose 2) Consider the following Java program, what starts on line 2? 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4        ...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;        for (int i = 2; i < n; i++){          if (n % i == 0)             return false; }        return true; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length) return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT