Question

9, What does this program print? package javaapplication210; public class JavaApplication210 public static void main(Stringllargs) system out.printfu Result is: %dn, mystery(-5, -9)): llor System.out printf Result is: %din, mystery(-4,-8) lior System out print Result is: %din, mystery 7) llo public static int mystery( int a int b) if (b 1) return a else return at mystery( a, b 1);


0 0
Add a comment Improve this question Transcribed image text
Answer #1

The program shows the run time error :

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%d'

Modified code :

public class HelloWorld{

public static void main(String []args){
System.out.printf("Result is:"+mystery(-5,-9)+"\n"); // calling function
System.out.printf("Result is:"+mystery(-4,-8)+"\n");   // calling function
System.out.printf("Result is:"+mystery(-6,-7)+"\n");   // calling function
}
public static int mystery(int a,int b)
{
if(b==1)
return a;
else
return a+mystery(a,b+1);
}
}

Outptput :

- Default Term +Browser sh-4.39 javac Javapplication210.java sh-4.3$ java -Xmx128M -Xmsl6M Javapplication210 Result is:-55 Re

If the arguments are (-5,-9).

  if(b==1)

It runs from b value -9 to 1, the value of b incremented until b==1

Therefore it runs for 11 times.

So the value is -5 is added to the varible, thus the values are like -5, -10, ........-55 (Here 10 times it calls)

return a+mystery(a,b+1);

Similarly

(-4,-8)

It runs for 10 times. The values are -4, -8, -12......

(-6,-7)

It runs for 9 times. The values are -4, -8, -12......

Add a comment
Know the answer?
Add Answer to:
What does this program print? package javaapplication210; public class JavaApplication210 { public static void main(String[]args){ System.out.printf("Result...
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
  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • 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"); } }

  • Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[]...

    Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[] args) { // sort doubleArray into ascending order char [] A = {'g', ', 'y', 'a', 'e','d' }; Arrays.sort( A); for (char value: A) System.out.printf("%2C", value); System.out.printf("\n"); char [] A Copy = new char[ 3 ]; System.arraycopy( A, 2, A Copy, 0, A Copy.length); for(char value: A Copy) System.out.printf("%2C", value); System.out.printf("\n"); int location = Arrays.binary Search(A Copy, 'y'); if(location >=0) System.out.printf("y Found at element...

  • 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);

  • Java, how would i do this public static void main(String[] args) { int n = 3;...

    Java, how would i do this public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...

  • What are the errors in this ? public class Mystery { public static void main(String[] args)...

    What are the errors in this ? public class Mystery { public static void main(String[] args) { double initialSavings = 10000; double interestRate = 0.05; double currSavings = 0; int i;    System.out.println("\nAnnual savings 5 years: "); currSavings = initialSavings; for (i = 0, i < 5, ++i); currSavings = (currSavings * interestRate); System.out.println("$" + currSavings); }    }

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • mystery (numi, num2) ? public static void main(String[] args) int numl = 7; int num2 =...

    mystery (numi, num2) ? public static void main(String[] args) int numl = 7; int num2 = 13; int result = mystery (numi, num2); } public static int mystery (int firstNum, int secondNum) { firstNum = firstnym * 3; secondNum = secondnum * 2; return firstNum + secondNum; } numl: A num2: A int secondNum) firstNum = firstNum * 3; secondNum = secondNum * 2; return firstNum + secondNum; numl: A/ num2: result: A Previous Page Next Page

  • 7. Consider the following recursive function: package javaapplication293; public class JavaApplication293 public static void main Stringl...

    7. Consider the following recursive function: package javaapplication293; public class JavaApplication293 public static void main Stringl args) recFun(7) public static void recFun int u) if (-= 1 ) System.out. print("Stop! "); else System.out.print("Go "); recFun(u-1);

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
ADVERTISEMENT