AP Computer Science - Recursion Worksheet 1 DIRECTIONS: Fill in each blank with
ID: 3735234 • Letter: A
Question
AP Computer Science - Recursion Worksheet 1 DIRECTIONS: Fill in each blank with the correct answer/output. Assume each statement in order and that one statement may affect the next statement. Some sections might print more than once. happens static void funl(int x out.print (x) funl (x-1) static void fun2 (int x) out.print(x)a ost print fun2 (x-1), out-print (x) a static int fun3lint x) if (xc1) return i else return xfun3 (x-2) a static int funil int x, int y) Y-2) return y else return fund ( x, y- 1] + x; AAAIMAM //zunner code in the main of another class line yaten out prántint Eun3(4 out.printini un43.6 A Compuer Scac Worksbetwww.coorExplanation / Answer
1)
fun1(5)->5 will be printed->call fun1(4)
fun1(4)->4 will be printed->call fun1(3)
fun1(3)->3 will be printed->call fun1(2)
fun1(2)-2 will be printed->call fun1(1)
fun1(1)->1 will be printed->call fun1(0)
since 0>=1 is false and terminates
so the final output is 54321
2)
The final output is 6543210
3)
fun3(4)=4+fun3(4-2)=4+fun3(2)=4+2+fun3(2-2)=4+2+fun3(0)=4+2+0(since 0<1)=6
so final output is 6
4)
fun4(3,6)=fun4(3,6-1)+3=(fun4(3,5)+3)+3=(fun4(3,4)+3)+3+3=(fun4(3,3)+3)+3+3+3=(fun4(3,2))+3+3+3+3
=2+3+3+3+3=14(since y=2)
the final output is 14
5)
fun4(4,2)=2 since y=2
the final output is 2