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.
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...
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)
Given the following code, what is output by the method call, mystery(6 * 8)? public static...
Given the following code, what is output by the method call, mystery(6 * 8)? public static void mystery (int x[]) {     System.out.println("A"); } public static void mystery (int x) {     System.out.println("B"); } public static void mystery (String x) {     System.out.println("C"); } A B C CA CB Which of the following is true about overloaded methods? Java cannot use a method's return type to tell two overloaded methods apart. Java cannot use a method's parameters to tell two overloaded methods apart....
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long...
Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long exponentiation(long x, int n) to calculate x^n. Note: In your function you can use only the basic arithmetic operators (+, -, *, %, and /). (b) What is the running time complexity of your function? Justify. (c) Give a number of multiplications used by your function to calculate x^63. Important Notes: • For the item (a): o You must add the main method in...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its input parameter, we'll say that a SubArray 9,8,7is that three integers, 9,8,7values appearing decreasing order one by one in the array. Return true if the array does contain any SubArray “987” Notice that any 3 integers that presenting a decreasing by one order must return True. (987, 876,765,654, 543, 432,321, 210 are all True) Call this method in your main function, and pass in...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT