Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. How many times is the factorial method invoked for factorial(5) in the follow

ID: 3796091 • Letter: 1

Question

1. How many times is the factorial method invoked for factorial(5) in the following?

import java.util.Scanner;

public class ComputeFactorial {
/** Main method */
public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a non-negative integer: ");
    int n = input.nextInt();
    
    // Display factorial
    System.out.println("Factorial of " + n + " is " + factorial(n));
}

/** Return the factorial for a specified number */
public static long factorial(int n) {
    if (n == 0) // Base case
      return 1;
    else
      return n * factorial(n - 1); // Recursive call
}
}

A. 3

4

5

6

QUESTION 2

Analyze the following code:

public class Test {
public static void main(String[] args) {
    int[] x = {1, 2, 3, 4, 5};
    xMethod(x, 5);
}

public static void xMethod(int[] x, int length) {
    System.out.print(" " + x[length - 1]);
    xMethod(x, length - 1);
}
}

The program displays 1 2 3 4 6.

The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException.

The program displays 5 4 3 2 1.

The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

QUESTION 3

Analyze the following recursive method.

public static long factorial(int n) {
    return n * factorial(n - 1);
}

Invoking factorial(0) returns 0.

Invoking factorial(1) returns 1.

Invoking factorial(2) returns 2.

Invoking factorial(3) returns 6.

The method runs infinitely and causes a StackOverflowError.

QUESTION 4

What are the base cases in the following recursive method?

public static void xMethod(int n) {
    if (n > 0) {
      System.out.print(n % 10);
      xMethod(n / 10);
    }
}

n > 0

n <= 0

no base cases

n < 0

QUESTION 5

What is the return value for xMethod(4) after calling the following method?

static int xMethod(int n) {
if (n == 1)
    return 1;
else
    return n + xMethod(n - 1);
}

A. 12

11

10

9

QUESTION 6

Show the output of the following code:

public class Test1 {
public static void main(String[] args) {
    System.out.println(f2(2, 0));
}

public static int f2(int n, int result) {
    if (n == 0)
      return 0;
    else
      return f2(n - 1, n + result);
}
}

A. 0

1

2

D. 3

QUESTION 7

Fill in the code to complete the following method for checking whether a string is a palindrome.

public static boolean isPalindrome(String s) {
if (s.length() <= 1) // Base case
    return true;
else if _____________________________
    return false;
else
    return isPalindrome(s.substring(1, s.length() - 1));
}

(s.charAt(0) != s.charAt(s.length() - 1)) // Base case

(s.charAt(0) != s.charAt(s.length())) // Base case

(s.charAt(1) != s.charAt(s.length() - 1)) // Base case

(s.charAt(1) != s.charAt(s.length())) // Base case

QUESTION 8

How many times is the fib method invoked for fib(5) in the following code?

import java.util.Scanner;

public class ComputeFibonacci {
/** Main method */
public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(System.in);
    System.out.print("Enter an index for a Fibonacci number: ");
    int index = input.nextInt();

    // Find and display the Fibonacci number
    System.out.println("The Fibonacci number at index "  
      + index + " is " + fib(index));
}

/** The method for finding the Fibonacci number */
public static long fib(long index) {
    if (index == 0) // Base case
      return 0;
    else if (index == 1) // Base case
      return 1;
    else // Reduction and recursive calls
      return fib(index - 1) + fib(index - 2);
}

14

15

25

31

32

QUESTION 9

In the following method, what is the base case?

static int xMethod(int n) {
if (n == 1)
    return 1;
else
    return n + xMethod(n - 1);
}

n is 1.

n is greater than 1.

n is less than 1.

no base case.

QUESTION 10

Which of the following statements are true?

The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series.

The Fibonacci series begins with 1 and 1, and each subsequent number is the sum of the preceding two numbers in the series.

The Fibonacci series begins with 1 and 2, and each subsequent number is the sum of the preceding two numbers in the series.

The Fibonacci series begins with 2 and 3, and each subsequent number is the sum of the preceding two numbers in the series.

A. 3

B.

4

C.

5

D.

6

Explanation / Answer

1. C.5

5 times(factorial(5),factorial(4),factorial(3),factorial(2),factorial(1))

2. D.The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

No base condition to end recursion.

3. E. The method runs infinitely and causes a StackOverflowError.

4. A. n>0

5. C. 10

4+3+2+1

6. A. 0

7. A. (s.charAt(0) != s.charAt(s.length() - 1)) // Base case

8. D. 31

9. A. n is 1.

10. B. The Fibonacci series begins with 1 and 1, and each subsequent number is the sum of the preceding two numbers in the series.