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)
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...
in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
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....
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) { }
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT