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...
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...
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 }
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...
3. Consider the mystery method given. public static int mystery ( int n) [ if (n == 0 ) { return 1; How do we get the values for recurse? else if (n%2 == 0 ) { int recurse = mystery ( n - 1); int result = recurse + n; return result; since n =5, we go to the else statement and do int recurse = mystery(5-1) which equals 4? why is 3 written? else { int recurse =...
Given the recursive method: public int someValue(List<Integer> A) { if (A.size() > 0) { int v = A.get(0); A.remove(0); return v + someValue(A); } else return 0; } What value does the method return if A contains 1 2 3 4 5? a. 0 b. 5 c. 10 d. 15
static public int[] Question() // retrieve an integer 'n' from the console // the first input WILL be a valid integer, for 'n' only. // then read in a further 'n' integers into an array. // in the case of bad values, ignore them, and continue reading // values until enough integers have been read. // this question should never print "Bad Input" like in lab // hint: make subfunctions to reduce your code complexity return null; static public int...