Question

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 > 100) {
        System.out.print(n);
    } else {
        mystery2(2 * n);
        System.out.print(", " + n);
    }
}

Sound F/X

mystery2(113);
mystery2(70);
mystery2(42);
mystery2(30);
mystery2(10);
0 0
Add a comment Improve this question Transcribed image text
Answer #1

whenever the value of n is smaller than 100 in that case the function again calls itself by doubling the value of n , as the value of n crosses 100 the n value gets printed and no more mystery2 is called . Due to backtracking the value of n which were called from else condition gets printed

public void mystery2(int n) {
    if (n > 100) {
        System.out.print(n);
    } else {
        mystery2(2 * n);
        System.out.print(", " + n);
    }
}

mystery2(113);  

Output produced: 113

mystery2(70);

Output produced: 140, 70

mystery2(42);  

Output produced: 168, 84, 42

mystery2(30);  

Output produced: 168, 84, 42

mystery2(10);

Output produced: 160, 80, 40, 20, 10

Add a comment
Know the answer?
Add Answer to:
For each call to the following method, indicate what console output is produced: public void mystery2(int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Recursive Tracing. For each call to the following method, indicate what value is returned: public static...

    Recursive Tracing. For each call to the following method, indicate what value is returned: public static int mystery(int n) { if (n < 0) { return -mystery(-n); } else if (n == 0) { return 0; } else { return mystery(n / 10) * 10 + 9 - (n % 10); } Call Value Returned mystery(0) mystery(5) mystery(13) mystery(297) mystery(-3456) } Can any one help me with it?

  • output What is the output of the following: class Access public int x; private int y...

    output What is the output of the following: class Access public int x; private int y public void cal(int x, int y) { this.x = x + 1; this.y = y; } public void print() { System.out.print(""+y); } } class AccessSpecifier { public static void main(String args[]) { Access obj = new Access(); obj.cal(2, 3); System.out.print(obj.x); obj.print(); } } 33 Compilation error 23 None of the available choices Runtime error

  • What is the Java output? Part One: class Driver { public static void main(String[] args) {...

    What is the Java output? Part One: class Driver { public static void main(String[] args) { int a = 5; int b = 3; if (a < b || a * 2 < b) System.out.print(a - b); System.out.print(b + a); } } Part Two: class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } }

  • [4] 8 points (2 points for each part) use file Mystery.txt For each call below to...

    [4] 8 points (2 points for each part) use file Mystery.txt For each call below to the mystery method, write the output that is produced, as it would appear on the console: public static void mystery (int x, int y) { int s = 0; while (x > 0 && 2 * y >= x) { System.out.print(s + " "); y = y - x; x--; s = s + x; } System.out.println (s); } A. mystery (-2, -6); B....

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • JAVA 5) What is the output of the following code? int a = 70; boolean b...

    JAVA 5) What is the output of the following code? int a = 70; boolean b = false; if(a >= 70) { System.out.print(1); if(b==true) { System.out.print(2); } } else { System.out.print(3); if(b==false) { System.out.print(4); } } System.out.print(5); 6) What is the output of the code above using these initial values? int a = 43; boolean b = false; 7) The following method is SYNTACTICALLY correct (meaning it will compile). True or false? public boolean method() { int value = 5;...

  • What is printed by running the following code? public static void main(String[] args) { int[] nums...

    What is printed by running the following code? public static void main(String[] args) { int[] nums = {2, 3, 4}; int n = 5; changeMe1(n, nums); System.out.print( n ); System.out.print(nums[0]); changeMe2(n, nums); System.out.print( n ); System.out.print(nums[0]); } public static void changeMe1(int number, int[] list) { number++; list[0]++; } public static void changeMe2(int number, int[] list) { number = 9; list = new int[1]; list[0] = 99; }

  • What output is produced by the following program? public class MysteryNums public static void main(String[] args)...

    What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);

  • what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int...

    what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int w 0; int x; int y 5; float z = 6.1 System.out.print("Enter y: "); x keyboard.nextint); System.out.println('y'); System.out.println(x); System.out.println(w*3); x- x+(int)z; System.out.println(x); 0 System.out.println(u); System.out.,println(u); System.out.println"x In" + y); System.out.print(y + z); ) liclosing main method 1 liclosing class header

  • What is the output of the following program? public class Contacts Contact contacts; private void...

    What is the output of the following program? public class Contacts Contact contacts; private void addContact Contact b) if( contactsnull) contactsb; else Contact tempcontacts; contacts b; b.nexttemp; private void showContactList) Contact tempcontacts; while( tempnull) f System.out.print( temp.name + ", " ); temp temp.next; public static void main( String [] args) Contacts myContacts new Contacts ) myContacts.addContact( new Contact("Peter")); myContacts.addContact( new Contact( "Paul"); myContacts.addContact( new Contact("Mary"); myContacts.showContactList); class Contact public String name; public Contact next; public Contact( String t ) f...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT