Question 111 pts
Given the method definition:
public int spin( int n ) {
if ( n <= 1 )
return 0;
if ( n % 2 != 0 )
return spin( n - 1 );
return n + spin( n - 2 );
}
What is the value of the following expression?
spin( 7 )
| 0 |
| 7 |
| 10 |
| 12 |
| 15 |
Flag this Question
Question 121 pts
What does the spin() method do?
public int spin( int n ) {
if ( n <= 1 )
return 0;
if ( n % 2 != 0 )
return spin( n - 1 );
return n + spin( n - 2 );
}
| Returns the sum of all positive numbers less than or equal to n. |
| Returns the sum of all odd positive numbers less than or equal to n. |
| Returns the sum of all even positive numbers less than or equal to n. |
| Returns the sum of every other number from n down to 2. |
| None of the above |
Flag this Question
Question 131 pts
What would happen if a recursive method were written with no base cases?
| a syntax error |
| the value 'null' would be returned |
| infinite recursion |
| None of the above |
Flag this Question
Question 141 pts
Given the following recursive method:
int something(int x){
if (x< 0) return 0;
if (x <10) return x;
return x % 10 + something (x/10);
}
What is the value of the following expression?
something( 273 )
| 3 |
| 273 |
| 0 |
| 12 |
SOLUTION:-


============================================================================================
Question 111 pts Given the method definition: public int spin( int n ) { if (...
Question 11 (3 points) What does the following recursive method determine? public boolean question 16(int[]a, int[] b. intj) { if (j == 2.length) return false; else if ( == b.length) return true; else return question 16(2, b.j+1): 3 returns true if b contains less elements than a, false otherwise returns true if b contains more elements than a, false otherwise returns true if a and bare equal in size, false otherwise returns true if a's element value is larger than...
public static List sumOfDistinctCubes(int n) Determine whether the given positive integer n can be expressed as a sum of cubes of positive integers greater than zero so that all these integers are distinct. For example, the integer n = 1456 can be expressed as sum 11 3 + 5 3 . This method should return the list of these distinct integers as a list [11, 5] with the elements given in descending order. If n cannot be broken into a...
Why might the following method have infinite recursion? public int infiniteRecursion(int n) { if (n > 0) { return infiniteRecursion(n) - 2; } else { return 0; } } Because the base case will never be true None of these are correct, there is no infinite recursion in this method Because there is no base case Because the recursive call does not move the parameter closer to the base case
hi, can someone explain this method please: public static int sumCapped(int nums[] , int x) { int sum=0; if(nums.length==0) { return Integer.MAX_VALUE; } for(int i=0;i<nums.length;i++) { if(sum+nums[i]<=x) // check if adding the succssive element return gives a values less than or equal to x { sum=sum+nums[i]; // if true then add } } return sum; // return the output }
Java, how would i do this
public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...
I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. * * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...
Given the function definition, which of the following are correct? int func(int n, double d) { int j = n; double sum = 0; while( j >= 0) { sum += d; -j; } return sum; } It compiles but computes none of these returns 7+2 returns 7! returns 7*2
Question 10 (3 points) Which of the following statement is not true? There is a recursive sum method as shown below. When sum (19) is called, summation of all odd numbers less than 19 will be calculated and returned public int sum(int x){ if (x == 0) return 0: else return sum(x-2) + x; The following code segment will throw a testException. This exception has been handled in the way that do nothing but to continue when this exception happens....
C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the + operator. //Pre : M and N are defined and N > 0. //Post: Returns M x N { int Prod; if (N == 1) Prod = M; //base case else Prod = M + Multiply(M, N - 1); //recursive step return Prod; } 2) Reverse #include <iostream.h> void Reverse(int N) //Displays string of length N in the reverse order //Pre : N...
Question 13 pts (TCO 4) Which of the following functions grows at a slower rate than the rest? n2 n log n n3 Flag this Question Question 23 pts (TCO 4) Algorithms can be described using pseudo-code. assignment and arithmetic operations. loops and decision statements. All of the above Flag this Question Question 33 pts (TCO 4) The running time of an algorithm is the time, in milliseconds, it takes to complete its execution. the running time of its implementation....