Section 18.2 Check Point Questions6 questions
18.2.1
What is a recursive method? What is an infinite recursion?
18.2.2
How many times is the factorial method in LiveExample 18.1 invoked for factorial(6)?
18.2.3
Show the output of the following programs and identify base cases and recursive calls.
(a) public class Test { public static void main(String[] args) { System.out.println( "Sum is " + xMethod(5)); } public static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); } } (b) public class Test { public static void main(String[] args) { xMethod(1234567); } public static void xMethod(int n) { if (n > 0) { System.out.print(n % 10); xMethod(n / 10); } } }
18.2.4
Write a recursive mathematical definition for computing 2 n for a positive integer n.
18.2.5
Write a recursive mathematical definition for computing x n for a positive integer n and a real number x.
18.2.6
Write a recursive mathematical definition for computing 1 + 2 + 3 + ... + n for a positive integer n.