part ii:
function f(3)
f(3) --> g(4)
g(4) --> f(6) (r=1 ,f(n+2))
f(6) --> g(3) (n%2==0 ,g(n/2))
g(3) --> f(1) (r=0 ,f(n/3))
f(1) will return 0
call stack diagram:

part 2
no function f wont terminate on all inputs
for e.g
for n=4
function f(4)
f(4) will call g(2)
g(2) will have r=2 so f(2*2) i.e f(4)
f(4) will call g(2)
g(2) will have r=2 so f(2*2) i.e f(4)
so it repeats so its function f(n) will not terminate for all inputs
We were unable to transcribe this image
b) Consider the following code. public static int f(int n) if (n == 1) return 0;...
3. Consider the mystery method given. public static int mystery ( int n) [ if (n == 0 ) { return 1; How do we get the values for recurse? else if (n%2 == 0 ) { int recurse = mystery ( n - 1); int result = recurse + n; return result; since n =5, we go to the else statement and do int recurse = mystery(5-1) which equals 4? why is 3 written? else { int recurse =...
1. public int function(int x, int n) { if (n == 0) return 1; return x * function(x, n -1); } function(3,3) - What is the expected output? 3 12 9 27 2. int fun(int x) { if(x == 0) return 1; else return fun(x - 1); } fun(4) 18 1 24 4 3. Which one of the following calls results 6? int mystery(int n){ if (n == 1) return 1; else return n * mystery(n - 1); } mystery(3)...
Given the following code: public static void foo3(String s) { if (s.length() >0) { System.out.print(s.charAt(s.length() -1)); foo3(s.substring(0, s.length() -1)); } } What is the output of: foo3(“”); 2, You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n) //line 9 { if (n = = 0) return 1; else System.out.println(n* foo(n-1) ); } //line 15 At compile time, you get the following error: Text.java: 15: missing return statement } ...
what is wrong with the following code? public class EightBall { private static Scanner scanner = new Scanner(System.in); private static Random rnd = new Random(); public static String getAnswer(int category, int answer) { if (category >= 74) { if (answer == 0) { return "As I see it, yes." ; } else if (answer == 1) { return "Signs point to yes." ; } else if (answer == 2) { return "Outlook good." ; } else if...
public class Test { private static int i =0; private static int j =0; public static void main(String[] args) { int i = 2; int k = 3; { int j =3; System.out.println("i + j is : " + i + j); } k = i + j; System.out.println("K is " + k ); System.out.println("K is " + j); } } why is the output i + j = 23 K =2 K =0 Please explain a step by step...
must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...
---> JAVA PROGRAM Public Static int EXX (int n,int x){ If (x==0){ return 1; } else If (n==1) { return 1; } else { return (n* EXX(n,x-1)); * what is the output when (5,0) *what is the output when (8,1) what is the output when (4,3)
1. t(n) is the runtime of following function, public static int f4(int [] a, int start, int end){ int ans = 0; if (start >= end) ans = a[start]; else { int mid = (start + end) / 2; int x = f4(a, start, mid); int y = f4(a, mid + 1, end); print(a, start, end); //print each element in a from start to end if (x < y) ans = x; else ans = y; } return ans; }...
Consider the following method: Linel: public static int mystery(int n) { Line2: if (n < 10) { ine3: return n; Line4: } else { Line5: int a = n/10; Line 6: int b = n % 10; Line 7: return mystery(a + b); Line 8: } Line 9: } What is the result of the following call? System.out.println(mystery(648)); 18 8 12
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...