Recursion Questions

  1. int getSumBetween(int a, int b) // precondition a <= b - get sum between a and b inclusive
  2. int getSumBetween(int a, int b) // no precondition
  3. int sum(int n) // returns sum between 1 and n
  4. int factorial(int n) // returns factorial ex. 5 factorial is 120 because 5*4*3*2*1 = 120
  5. void printcharacters(String n) // prints characters of the Sting each one on new line
  6. public void reverse(String na) // prints String in reverse
  7. bool isthere(int ar[], int n) // is number in array // call a helper function from this with another parameter
  8. void print(int ar[], int n) // print array - n sent up initially as 0
  9. void printdigits(int n) // prints digits one at a time
  10. int sumofdigits(int n) // returns sum of digits
  11. int sumofOddNumbers(int n) // return sum of the odd numbers // precondition n > 0 ; n may not be odd to start
  12. Given boolean isPrime(int n) is written and works Write a function that will return the sum of the prime numbers between 1 and n int sumOfPrimes(int n)
  13. int sum(int ar[]) // returns sum of numbers in array // call a helper function from this with another parameter
  14. Write a function that will find the greatest common factor for 2 numbers using theorem. It says the GCF(a,b) == GCF(b,a) if a is initially less than b. The GCF(a,b) == a if b == 0. The GCF(a,b) == GCF(b,a%b) otherwise.
    public int findGCF(in a,int b)
  15. void test(int n) {
    if (n > 0) {
    System.out.println(n);
    test(n-1);
    System.out.println(n);
    }
    }
    What would test(4) print?
  16. Write a recursive function to perform exponentiation
    return x^m, assuming m >= 0
    public int exp(int x, int m) {
    }
  17. Given the following method declaration, which of the following is printed as the result of the call mystery(1234)?
    //precondition: x >=0
    public void mystery (int x)
    {
    System.out.print(x % 10);
    if ((x / 10) != 0)
    {
    mystery(x / 10);
    }
    System.out.print(x % 10);
    }
    a. 1441
    b. 43211234
    c. 3443
    d. 12344321
    e. Many digits are printed due to infinite recursion.
  18. Given the following method declaration, what value is returned as the result of the call product(5)?
    public int product(int n)
    {
    if (n <= 1)
    return 1;
    else
    return n * product(n - 2);
    }
    a. 1
    b. 10
    c. 25
    d. 3125
    e. 15
  19. Given the following method declaration, what value is returned as the result of the call f(5)?
    public static int f(int n)
    {
    if (n == 0)
    return 0;
    else if (n == 1)
    return 1;
    else return f(n-1) + f(n-2);
    }
    a. 8
    b. 3
    c. There is no result because of infinite recursion.
    d. 5
    e. 0
  20. Given the following method declaration, what will redo(82, 3) return?
    public int redo(int i, int j)
    {
    if (i==0)
    return 0;
    else
    return redo(i/j, j)+1;
    }
    a. 5
    b. 4
    c. 6
    d. 7
    e. The method never returns due to infinite recursion.
  21. Given the following method. What is the output when m1(5) is called?
    public int m1 (int a)
    {
    if (a == 1)
    return 10;
    else
    return 10 + m1 (a - 1);
    }
    a. 50
    b. 20
    c. 60
    d. 10
    e. 30