Question

In: Computer Science

For each call to the following method, indicate what console output is produced: public void mystery2(int...

For each call to the following method, indicate what console output is produced:

public void mystery2(int n) {
    if (n <= 1) {
        System.out.print(n);
    } else {
        mystery2(n / 2);
        System.out.print(", " + n);
    }
}
mystery2(1);
mystery2(4);
mystery2(16);
mystery2(30);
mystery2(100);

Solutions

Expert Solution

Solution:

Here in the given mystery2() function it is checking whether the n value is less than or equals to 1 if yes print(n) value otherwise call mystery2() by dividing n with 2 . Here after the function call the print statement will execute.

when we call mystery2(1) it will print the value 1 because n<=1 is becomes true so it will print 1.

when we call mystery2(4) it will print 1,2,4

because here n=4 so n<=1 becomes false so it will call mystery2(n/2) = mystery2(2) after execution of mystery2(1) it will print value 4

so in next step n=2 so n<=1 becomes false so it will call mystery2(2/2)= mystery2(1) after execution of mystery2(1) it will print value 2

Here n=1 so n<=1 becomes true so it will print value 1 and executes the print statements after the previous recursive calls.

n Condition func call remaining statements need to execute
mystery2(4)
4 4<=1 false mystery2(4/2) print(n) =4
2 2<=1 false mystery2(2/2) print(n)=2
1 1<=1 true print(n)=1

i.e 1,2,4

when we call mystery2(16) output is 1,2,4,8,16

so

n Condition func call remaining statements need to execute
mystery2(16)
16 16<=1 = false mystery2(16/2) print(n) = 16
8 8<=1 false mystery2(8/2) print(n) =8
4 4<=1 false mystery2(4/2) print(n) =4
2 2<=1 false mystery2(2/2) print(n)=2
1 1<=1 true print(n)=1

when we call mystery2(30) output is 1,3,7,15,30

n Condition func call remaining statements need to execute
mystery2(30)
30 30<=1 = false mystery2(30/2) print(n) = 30
15 15<=1 false mystery2(15/2) print(n) =15
7 7<=1 false mystery2(7/2) print(n) =7
3 3<=1 false mystery2(3/2) print(n)=3
1 1<=1 true print(n)=1

when we call mystery2(100) output is 1,3,6,12,25,50,100

n Condition func call remaining statements need to execute
mystery2(100)
100 100<=1 = false mystery2(100/2) print(n) = 100
50 50<=1 false mystery2(50/2) print(n) =50
25 25<=1 false mystery2(25/2) print(n) =25
12 12<=1 false mystery2(12/2) print(n)=12
6 6<=1 false mystery2(6/2) print(n)=6
3 3<=1 false mystery2(3/2) print(n)=3
1 1<=1 true print(n)=1

Note:Here remaining statements will execute from bottom to top. Also print statement includes commas.

mystery2(1) = 1                                                                                                                             

mystery2(4) = 1, 2, 4                                                                                                                       

mystery2(16) = 1, 2, 4, 8, 16                                                                                                                

mystery2(30) =1, 3, 7, 15, 30                                                                                                               

mystery2(100) = 1, 3, 6, 12, 25, 50, 100  

Note: if you have any queries please post a comment thanks a lot.. always avaiable to help you...


Related Solutions

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)
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....
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
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)
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
Create a class called FibGenerator with 3 methods: public int nthFib(int n). This method should call...
Create a class called FibGenerator with 3 methods: public int nthFib(int n). This method should call computeFibRecurse(n). private int computeFibRecurse(int n), which should recurse (that is, call itself) unless n is 1 or 2. If n is 1 or 2, the method should return 1. A main method that prints “STARTING”, then constructs a FibGenerator generator and then calls nthFib(), passing in interesting values. To look into this problem, you’re going to use software to analyze software. Add an instance...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
Which cohesion does this method belong to? Public void sampleMethod(int flag) { Switch (flag) { Case...
Which cohesion does this method belong to? Public void sampleMethod(int flag) { Switch (flag) { Case ON: // code Case OFF: // code Case CLOSE: //code } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT