QUESTION 1 What are the base cases in the following recursive method? public sta
ID: 3794864 • Letter: Q
Question
QUESTION 1
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
1 points
QUESTION 2
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.
1 points
QUESTION 3
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.
1 points
QUESTION 4
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
1 points
QUESTION 5
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.
1 points
QUESTION 6
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
}
}
3
4
5
6
1 points
QUESTION 7
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.
1 points
QUESTION 8
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);
}
12
11
10
9
1 points
QUESTION 9
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);
}
}
0
1
2
3
1 points
QUESTION 10
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
1 points
Click Save and Submit to save and submit. Click Save All Answers to save all answers.
n > 0
n <= 0
no base cases
n < 0
Explanation / Answer
Question 3 - option 4 - The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.
Question 1 - option
Question 2 - option
Question 7 - option 5 - runs infinitely
Question 10 - option 2
Question 9 - option 1 - zero(0)
question 8 - option 3 - 10
question 6 - option 4 - 6
question 5 - option 1
question 1 - option 1
question 2 - option 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.