Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Review Mr. Jacobs.

Similar presentations


Presentation on theme: "Recursion Review Mr. Jacobs."— Presentation transcript:

1 Recursion Review Mr. Jacobs

2 Open Ended public int mystery(int w){ if ( w < 0) return 0; // Line 1 int x = mystery(w-2); // Line 2 return w -x; // Line 3 } The removal of what line will cause infinite recursion? Answer = Line 1

3 Open Ended public int recur(int n){ if(n < 0) return 0; else return n + recur(n-3); } What is returned by the call recur(7)? Answer = 12

4 Open Ended public static int mystery(int n){ if(n > 20) return 10; return 2 * mystery(n+5); } What will result from the call mystery(25)? Answer = 10

5 Open Ended public static void doSomething(int n){ if(n>0){ doSomething(n-1); System.out.print(n); } } What will the call doSomething(3) print? Answer =

6 Open Ended public static void recur(String s){ if(s.length() > 0){ recur(s.substring(1)); System.out.print(s.substring(0,1)); } } public static void main(String[] args){ recur("Boxer"); } What does the code above print? Answer = rexoB

7 Open Ended public static int t(int n) { if(n ==1 || n==2) return 2 * n; else return t(n-1) - t(n-2); } For the method call t(5), how many calls to t will be made, including the original call? Answer = 9 Calls

8 Open Ended public static void recur(String s){ if(s.length() > 0){ recur(s.substring(1)); System.out.print(s.substring(0,1)); } } public static void main(String[] args){ recur("Rachel"); } What does the code above print? Answer = lehcaR

9 Open Ended public int recur(int n){ if(n = 2 || n =3) n = 2; return recur(n-1) - recur(n-2); } How many calls are made when recur(6) is called? Answer = 9 Calls

10 Open Ended public int recur(int n){ if(n <=0) return 0; // Line 1 int x = 2 * n; // Line 2 return x + recur(n-1); // Line 3 } Which line is the recursive call in method recur? Answer = Line 3

11 Final Jeapordy public static int recur(int x, int y, int z){ if(x<20) return z; x-=z; return y + recur(x-2, y, z); } public static void main(String[] args){ System.out.print(recur(25, 2, 3)); } What does the code above print? Answer = Infinite Recursion Occurs


Download ppt "Recursion Review Mr. Jacobs."

Similar presentations


Ads by Google