Question
QUESTION 1 Consider the following code snippet for recursive addition: int add (int I , int j ) { //assume I >=0 If (I ==0)
QUESTION 1
Consider the following code snippet for recursive addition: int add (int I , int j )
{
//assume I >=0
If (I ==0)
{
Return j;
}
Else
{
Return add(i-1, j+1);
}
} Identify the terminating condition in this recursive method.
A) if (i == 0) B) return j C) return add(i - 1, j + 1) D) there is no terminating condition
QUESTION 2: In recursion, the recursive call is analogous to a loop ____.
A) call B) iteration C) termination D) return
QUESTION 3: In recursion, the non-recursive case is analogous to a loop ____. A) call B) iteration C) termination D) condition
QUESTION 4: Recursion will take place if any of the following happen:
I method A calls method B, which calls method C II method A calls method B, which calls method A III method A calls method A A) I B) I and II C) II D) II and III
QUESTION 5: Consider the recursive method shown below:
public static int strangecal (int bottom, in top) { if (bottom > top) { return -1; } else if (botton == top) { return 1; } else { return bottom * strangeCalc(bottom + 1, top); } }
What value will be returned with a call to strangeCalc(2,3)? A) 1 B) 2 C) 6 D) 24
QUESTION 6: Consider the recursive method shown below:
public static int strangecal (int bottom, in top) { if (botton > top) { return -1; } else if (bottom == top) { return 1; } else { return bottom * strangeCalc(bottom + 1, top); } }
What value will be returned with a call to strangeCalc(4,7)? A) 1 B) -1 C) 120 D) 840
QUESTION 7: A palindrome is a word or phrase that reads the same forward or backward. Consider the following code snippet:
public boolean palindrome (String string) { return isPal(string, 0, string.length()-1); } private boolean isPal(String string, int left, int right) { if (left >= right) { return true; } else if (string.charAt(left) == string.charAt(right)) { return isPal(string, left +1, right - 1); } else { return false; } }
What does the condition statement if(left >= right) { return true;} refer to? A) An empty or one-character string is considered a palindrome. B) The string is not a palindrome. C) It cannot be determined if the string is a palindrome. D) You have reached the middle of the string.
QUESTION 8: Complete the following code snippet, which is intended to be a recursive method that reverses a String value:
public static String reverseIt(String s) { if (s.length() <=1) { ___________ } else { return reverseIt(s.substring(1)) + s.charAt(0); } } A) return s; B) return 0; C) return s.charAt(0); D) return s.substring(1);
QUESTION 9: Given the following class code:
public class RecurseSample { public static void main (String[] args) { recurse(3); } public static int recurse(int n) { int total = 0; if (n == 0) { return 0; } else { total = 3+ recurse(n - 1); } System.out.println(total); return total; } }
What values will be printed? A) 1, 3, and 6 B) 1, 3, 6, and 9 C) 3, 6, and 9 D) 3, 6, 9, and 12
QUESTION 10: In recursion, the terminating condition is analogous to a loop _________. A) call B) iteration C) termination condition D) initialization condition
QUESTION 11: Given the following class code:
public class RecurseMore { public static void main (String[] args) { recurse(4); } public static int recurse(int n) { int total = 0; if (n == 0) { return 0; } else { total = 4 + recurse(n -2); } System.out.println(total); return total; } }
What values will be printed when this code is executed? A) 0, 4, and 8 B) 4 and 8 C) 4 D) 8
QUESTION 12: Consider the recursive method myPrint shown in this code snippet:
public void myPrint(int n) { if (n<10) { System.out.print(n); } else { int m = n % 10; System.out.print(m) myPrint(n / 10); } }
What does this method do? A) Prints a positive int value forward, digit by digit. B) Prints a positive int value backward, digit by digit. C) Divides the int by 10 and prints out its last digit. D) Divides the int by 10 and prints out the result.
QUESTION 12: ____ recursion can occur when a recursive algorithm does not contain a special case to handle the simplest computations directly. A) Mutual B) Non-mutual C) Terminating condition D) Infinite
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started