Chapter 13: Recursions

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

A palindrome is a word or phrase that reads the same forward or backward. Consider the methods palindrome and isPal shown below: 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; } } The method palindrome as shown here would be considered to be a ____ method. a. helper b. static c. recursive d. terminating

a. helper

Consider the following code snippet for calculating Fibonacci numbers recursively: int fib(int n) { // assumes n >= 0 if (n <= 1) { return n; } else { return (fib(n - 1) + fib(n - 2)); } } Identify the terminating condition. a. n <= 1 b. n < 1 c. fib(n - 1) d. fib(n - 1) + fib(n - 1)

a. n <= 1

Complete the following code snippet, which is intended to be a recursive method that will find the smallest value in an array of double values from index to the end of the array: public static double minVal(double[] elements, int index) { if (index == elements.length - 1) { __________________ } double val = minVal(elements, index + 1); if (elements[index] < val) { return elements[index]; } else { return val; } } a. return 0; b. return elements[index]; c. return 1; d. return elements[0];

b. return elements[index];

____ is a problem-solving technique that examines partial solutions, abandons unsuitable ones, and returns to consider other candidate solutions. a. Traceback b. Recursion c. Backtracking d. Debugging

c. Backtracking

Which statement(s) about recursion are true? I Recursion is faster than iteration II Recursion is often easier to understand than iteration III Recursive design has an economy of thought a. I and III b. I c. II and III d. II

c. II and III

In recursion, the recursive call is analogous to a loop ____. a. call b. termination c. iteration d. return

c. iteration

Consider the recursive method shown below: public static int strangeCalc(int bottom, int top) { if (bottom > 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. 840 d. 120

d. 120

Consider the code for the recursive method mysteryPrint shown in this code snippet: public static int mysteryPrint(int n) { if (n == 0) { return 0; } else { return (n + mysteryPrint(n-1)); } } What will be printed with a call to mysteryPrint (-4)? a. -10 b. 0 c. -22 d. Nothing - a StackoverflowError exception will occur

NOT a. -10 NOT c. -22

Given the following code snippet: public static int newCalc(int n) { if (n < 0) { return -1; } else if (n < 10) { return n; } else { return (n % 10) + newCalc(n / 10); } } What value will be returned when this code is executed with a call to newCalc(15)? a. 2.5 b. 6.5 c. 2 d. 6

NOT a. 2.5 NOT c. 2

Consider the following change to the PermutationGenerator class from the textbook. Instead of adding the removed character to the front of the each permutation of the simpler word, we will add it to the end. // Add the removed character to the end of // each permutation of the simpler word for (String s : shorterWordPermutations) { result.add(s + word.charAt(i)); } Consider the list produced by the modified PermutationGenerator. Which best describes this list? a. It is an incomplete list of permutations. b. It contains reversed permutations. c. It contains all permutations. d. It contains strings that are not permutations.

NOT a. It is an incomplete list of permutations. NOT d. It contains strings that are not permutations.

Consider the getArea method from the textbook shown below: public int getArea() { if (width <= 0) { return 0; } // line #1 if (width == 1) { return 1; } // line #2 Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } If line #1 was eliminated from the method, what would be the result when calling getArea? a. We would lose our only recursive case. b. A negative or zero width would cause problems. c. Nothing - the method would still work correctly. d. A positive width would reduce the correct area by 1.

NOT a. We would lose our only recursive case.

Question 2 1 pts Suppose we wrote a new version of method fib, called newFib. Compare newFib to the original fib shown below: public static long newFib(int n) { if (n <= 3) { return 1; } else { return newFib(n - 1) + newFib(n - 2) + newFib(n - 3); } } public static long fib(int n) { if (n <= 2) { return 1; } else { return fib(n - 1) + fib(n - 2); } } For which values of the integer n does newFib(n) always returns a value greater than fib(n)? a. any positive n b. n >= 3 c. n > 3 d. any value of n

NOT a. any positive n

Consider the getArea method from the textbook shown below: public int getArea() { if (width <= 0) { return 0; } // line #1 if (width == 1) { return 1; } // line #2 Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } Assume line #3 is changed to this: Triangle smallerTriangle = new Triangle(width + 1) When calling the getArea method on a Triangle object with width = 4, what result will be produced? a. area will only be incorrect for a triangle objects with width = 1 b. area for all triangles will be computed to be too low c. infinite recursion will occur for triangle objects with width >= 2 d. area for all triangles will be computed to be too high

NOT a. area will only be incorrect for a triangle objects with width = 1 NOT d. area for all triangles will be computed to be too high

Complete the following code snippet, which is intended to print out all permutations of the string generate by using a permutation generator object. public class PermutationGeneratorTester { public static void main(String[] args) { PermutationGenerator generator = new PermutationGenerator("generate"); ArrayList<String> permutations = generator.getPermutations(); for (String s : permutations) { ____________________ } } } a. generator.setPermutations(); b. generator.calculate(); c. generator.print(); d. System.out.println(s);

NOT a. generator.setPermutations();

Consider the following code snippet for recursive addition: int add(int i, int j) { // assumes 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. there is no terminating condition c. return j d. return add(i - 1, j + 1)

NOT a. if (i == 0) NOT d. return add(i - 1, j + 1)

Consider the getArea method from the textbook shown below. public int getArea() { if (width <= 0) { return 0; } // line #1 else if (width == 1) { return 1; } // line #2 else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } } Where is/are the terminating condition(s)? a. line #1 b. lines #1 and #2 c. line #4 d. line #2

NOT a. line #1

Consider the following recursive code snippet: public static int mystery(int n, int m) { if (n <= 0) { return 0; } if (n == 1) { return m; } return m + mystery(n - 1, m); } Identify the terminating condition(s) of method mystery? a. n <= 0 b. n == 1 c. n > 0 d. n <= 0 or n == 1

NOT a. n <= 0

Backtracking _____. a. never abandons a partial solution. b. starts from the end of the program and works backward to the beginning. c. starts with a partial solution and builds it up to get closer to the goal. d. explores only one path toward a solution

NOT a. never abandons a partial solution. NOT b. starts from the end of the program and works backward to the beginning.

Consider the method below, which displays the characters from a String in reverse order. Each character appears on a separate line. Select the statement that should be used to complete the method, so that it performs a recursive method call correctly. public static void printReverse(String word) { if (word.length() > 0) { ___________________________ System.out.println(word.charAt(0)); } } a. printReverse(word.length() - 1); b. printReverse(new String(word.charAt(1))); c. printReverse(word); d.printReverse(word.substring(1));

NOT a. printReverse(word.length() - 1); NOT b. printReverse(new String(word.charAt(1)));

Complete the code for the myFactorial recursive method shown below, which is intended to compute the factorial of the value passed to the method: public int myFactorial(int anInteger) { if (anInteger == 1) { ______________________ } else { return (anInteger * myFactorial(anInteger - 1)); } } a. return 0; b. return myFactorial(anInteger); c. return -anInteger; d. return 1;

NOT a. return 0;

Consider the getArea method from the textbook shown below. public int getArea() { if (width <= 0) { return 0; } // line #1 if (width == 1) { return 1; } // line #2 Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } Assume that line #2 is changed to this: if (width == 1) { return 2; } How would this affect calls to getArea? a. It would make no difference to any call. b. It would double every triangle area. c. It would add 1 to all calls except those where width <= 0. d. It would subtract 1 from all calls.

NOT b. It would double every triangle area.

Consider the getArea method from the textbook shown below. public int getArea() { if (width <= 0) { return 0; } // line #1 else if (width == 1) { return 1; } // line #2 else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } } Assume line #1 is replaced with this line: if (width <= 0) {return width;} What will be the result? a. The method will still return correct results for all non-negative width triangles. b. The method will return area to be one too high for all triangles. c. The method will return incorrect results for triangles with width equal to 0. d. The method will return incorrect results for triangles with width equal to 1.

NOT b. The method will return area to be one too high for all triangles.

Consider the getArea method from the textbook shown below: public int getArea() { if (width <= 0) { return 0; } // line #1 Triangle smallerTriangle = new Triangle(width - 1); // line #2 int smallerArea = smallerTriangle.getArea(); // line #3 return smallerArea + width; // line #4 } If line#1 was removed, what would be the result? a. The recursive method would correctly calculate the area of the original triangle. b. The recursive method would terminate when the width reached 0. c. The recursive method would cause an exception for values below 0. d. The recursive method would construct triangles whose width was negative.

NOT b. The recursive method would terminate when the width reached 0.

A palindrome is a word or phrase spelled which 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 method palindrome return? a. the Boolean value returned from the isPal method b. false c. true d. a palindrome not found exception

NOT b. false

Consider the recursive square method shown below. It takes a non-negative int argument. Then it recursively adds the number n to itself n times to produce the square of n. Complete the correct code for the square helper method. public int square(int n) { ____________________; } public int square(int c, int n) { if (c == 1) { return n; } else { return n + square(c - 1, n); } } a. return square(n, n) b. return square(n, n - 1) c. return square(n - 1, n) d. return square(n)

NOT b. return square(n, n - 1) NOT c. return square(n - 1, n)

Consider the recursive version of the fib method from the textbook shown below: public static long fib(int n) { if (n <= 2) { return 1; } else { return fib(n - 1) + fib(n - 2); } } How many total recursive calls (not counting the original call) to fib will be made from the original call of fib(7)? a. 12 b. 30 c. 32 d. 24

NOT c. 32

Consider the recursive method shown below: public static int strangeCalc(int bottom, int top) { if (bottom > 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(2,3)? a. 24 b. 1 c. 6 d. 2

NOT c. 6

What is the purpose of a recursive helper method? a. Shield the user of the recursive method from the recursive details. b. Speed up the execution. c. Add another base case. d. Eliminate the recursion.

NOT c. Add another base case.

Why does the best recursive method usually run slightly slower than its iterative counterpart? a. Testing the terminating condition takes longer. b. Each recursive method call takes processor time. c. Checking multiple terminating conditions take more processor time. d. Multiple recursive cases must be considered.

NOT c. Checking multiple terminating conditions take more processor time. NOT d. Multiple recursive cases must be considered.

Consider the method powerOfTwo shown below: public boolean powerOfTwo(int n) { if (n == 1) // line #1 { return true; } else if (n % 2 == 1) // line #2 { return false; } else { return powerOfTwo(n / 2); // line #3 } } How many recursive calls are made from the original call powerOfTwo(63) (not including the original call)? a. 0 b. 6 c. 4 d. 1

NOT d. 1

Consider the recursive version of the fib method from the textbook shown below: public static long fib(int n) { if (n <= 2) { return 1; } else { return fib(n - 1) + fib(n - 2); } } How many total recursive calls (not counting the original call) to fib will be made from the original call of fib(6)? a. 14 b. 20 c. 6 d. 12

NOT d. 12

Consider the iterative version of the fib method from the textbook shown below: public static long fib(int n) { if (n <= 2) { return 1; } long fold = 1; long fold2 = 1; long fnew = 1; for (int i = 3; i <= n; i++) { fnew = fold + fold2; fold2 = fold; fold = fnew; } return fnew; } How many iterations of the for loop will there be for the call fib(6)? a. 6 b. 4 c. 3 d. 5

NOT d. 5

Consider the getArea method from the textbook shown below: public int getArea() { if (width <= 0) { return 0; } // line #1 Triangle smallerTriangle = new Triangle(width - 1); // line #2 int smallerArea = smallerTriangle.getArea(); // line #3 return smallerArea + width; // line #4 } If line#1 was removed, what would be the result? a. The recursive method would terminate when the width reached 0. b. The recursive method would construct triangles whose width was negative. c. The recursive method would correctly calculate the area of the original triangle. d. The recursive method would cause an exception for values below 0.

NOT d. The recursive method would cause an exception for values below 0.

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 s.substring(1); c. return 0; d. return s.charAt(0);

NOT d. return s.charAt(0);

Consider the fib method from the textbook shown below: public static long fib(int n) { if (n <= 2) { return 1; } else { return fib(n - 1) + fib(n - 2); } } Computing the 7th fibonacci number, fib(7), recursively computes fib(6), fib(5), and fib(4) ___ times respectively. a. 1, 2, and 3 b. 3, 2, and 1 c. 4, 5, and 6 d. 6, 5, and 4

a. 1, 2, and 3

Consider the following code snippet: public static boolean isEven(int n) { if (n % 2 == 0) { return true; } else { return (isOdd(n)); } } public static boolean isOdd(int n) { if (n % 2 == 1) { return true; } else { return (isEven(n)); } } For any given value of n, what is the maximum number of function calls that could occur? a. 2 b. cannot be determined c. 0 d. 1

a. 2

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. 3, 6, and 9 b. 1, 3, and 6 c. 3, 6, 9, and 12 d. 1, 3, 6, and 9

a. 3, 6, and 9

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. 4 and 8 b. 4 c. 0, 4, and 8 d. 8

a. 4 and 8

Given the following code snippet: public static int newCalc(int n) { if (n < 0) { return -1; } else if (n < 10) { return n; } else { return (1 + newCalc(n / 10)); } } What value will be returned when this code is executed with a call to newCalc(5)? a. 5 b. 1 c. 1.5 d. 5.5

a. 5

The string "eat" has ____ permutations. a. 6 b. 8 c. 4 d. 2

a. 6

Suppose we wrote a new version of fib called newFib. What does the call newFib(6) return? public static long newFib(int n) { if (n <= 3) { return 1; } else { return newFib(n - 1) + newFib(n - 2) + newFib(n - 3); } } a. 9 b. 3 c. 7 d. 5

a. 9

Consider the getArea method from the book shown below. public int getArea() { if (width <= 0) { return 0; } // line #1 else if (width == 1) { return 1; } // line #2 else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } } Assume lines #1 and #2 were replaced with this: if (width == 1) { return 1; } // replacement for lines #1 and #2 What will happen when this code is executed? a. A negative or zero width would cause problems. b. Nothing - the method would still be correct. c. We would lose our only recursive case. d. A positive width would reduce the correct area by 1.

a. A negative or zero width would cause problems.

Which of the following statements about palindromes is correct? a. All strings of length 0 or 1 are palindromes. b. The empty string is not a palindrome. c. The string "rascal" is a palindrome. d. The string "I" is not a palindrome.

a. All strings of length 0 or 1 are palindromes.

Which of the following options could be used as a terminating condition for a recursive method that finds the middle character of a String with any number of characters? I the length of the String is 1 II first and last String characters match III the String is not empty a. I b. I, II and III c. II d. I and III

a. I

Consider the fib method from the textbook shown below: public static long fib(int n) { if (n <= 2) { return 1; // line #1 } else { return fib(n - 1) + fib(n - 2); // line #2 } } Assume line #2 is changed to this: else { return 2 * fib(n - 1) + 2 * fib(n - 2); } What effect will this change have? a. This will return 10 from fib(4) b. This will return 5 from fib(4) c. This will return 8 from fib(4) d. This will cause an exception on a call fib(4)

a. This will return 10 from fib(4)

Complete the following code snippet, which is intended to be a recursive method that will find the sum of all elements in an array of double values from index to the end of the array: // return the sum of all elements in arr[] public static double findSum(double arr[], int index) { if (index == 0) { return arr[index]; } else { _____________________ } } Assume that this method would be called using an existing array named myArray as follows: findSum(myArray, myArray.length - 1); a. return (arr[index] + findSum(arr, index - 1)); b. return (findSum(arr, index - 1)); c. return (arr[index] + findSum(arr, index + 1)); d. return (findSum(arr, index + 1));

a. return (arr[index] + findSum(arr, index - 1));

Complete the code for the recursive method printSum shown in this code snippet, which is intended to return the sum of digits from 1 to n: public static int printSum(int n) { if (n == 0) { return 0; } else { ______________________________ } } a. return (n + printSum(n - 1)); b. return (printSum(n - 1)); c. return (n - printSum(n - 1)); d. return (n + printSum(n + 1));

a. return (n + printSum(n - 1));

Insert the missing code in the following code fragment. This fragment is intended to recursively compute xn, where x and n are both non-negative integers: public int power(int x, int n) { if (n == 0) { ____________________ } else { return x * power(x, n - 1); } } a. return 1; b. return x; c. return x * power(x, n - 1); d. return power(x, n - 1);

a. return 1;

Complete the code for the recursive method shown below, which is intended to compute the sum of the first n integers: public int s(int n) { if (n == 1) { return 1; } else { _________________ } } a. return n + s(n - 1); b. return s(n) + n - 1; c. return n + s(n + 1); d. return n + (n - 1);

a. return n + s(n - 1);

Consider the getArea method from the textbook shown below: public int getArea() { if (width <= 0) { return 0; } // line #1 if (width == 1) { return 1; } // line #2 Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } Assume that line #3 is changed to this: Triangle smallerTriangle = new Triangle(width); This would cause infinite recursion for ____. a. triangles with width greater than or equal to 2 b. triangles with width equal to 1 c. triangles of any width d. triangles with width equal to 0

a. triangles with width greater than or equal to 2

A unique permutation is one that is different from any other generated permutation. How many unique permutations does the string "aaa" have? a. 0 b. 1 c. 2 d. 3

b. 1

Consider the following recursive code snippet: public int mystery(int n, int m) { if (n == 0) { return 0; } if (n == 1) { return m; } return m + mystery(n - 1, m); } What value is returned from a call to mystery(3,6)? a. 6 b. 18 c. 729 d. 3

b. 18

Given the following code snippet: public static int newCalc(int n) { if (n < 0) { return -1; } else if (n < 10) { return n; } else { return (1 + newCalc(n / 10)); } } What value will be returned when this code is executed with a call to newCalc(15)? a. 5 b. 2 c. 5.5 d. 2.5

b. 2

A unique permutation is one that is different from any other generated permutation. How many unique permutations does the string "bee" have? a. 4 b. 3 c. 2 d. 5

b. 3

Complete the recursive method below, which is designed to return the number of matchsticks needed to form n squares. public static int matchsticks(int squares) { if (squares == 1) // 1 square can be formed with 4 matchsticks { return 4; } else { return ___________________________ } } a. matchsticks(squares + 4); b. 3 + matchsticks(squares - 1); c. 4 + matchsticks(squares - 1); d. 4 * squares;

b. 3 + matchsticks(squares - 1);

How many recursive calls to the fib method shown below would be made from an original call to fib(4)? (Do not count the original call) public int fib(int n) { // assumes n >= 0 if (n <= 1) { return n } else { return (fib(n - 1) + fib(n - 2)); } } a. 2 b. 8 c. 4 d. 1

b. 8

If a recursive method does not simplify the computation within the method and the base case is not called, what will be the result? a. The recursion calculation will occur correctly regardless. b. Infinite recursion will occur. c. This cannot be determined. d. The terminating condition will be executed and recursion will end.

b. Infinite recursion will occur.

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. Divides the int by 10 and prints out the result. b. Prints a positive int value backward, digit by digit. c. Divides the int by 10 and prints out its last digit. d. Prints a positive int value forward, digit by digit.

b. Prints a positive int value backward, digit by digit.

Complete the code for the calcPower recursive method shown below, which is intended to raise the base number passed into the method to the exponent power passed into the method: public static int calcPower(int baseNum, int exponent) { int answer = 0; ________________________ { answer = 1; } else { answer = baseNum * calcPower (baseNum, exponent - 1); } return answer; } a. if (exponent == -1) b. if (exponent == 0) c. if (exponent != 1) d. if (exponent == 1)

b. if (exponent == 0)

Consider the getArea method from the textbook shown below: public int getArea() { if (width <= 0) { return 0; } // line #1 if (width == 1) { return 1; } // line #2 Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } Which line has the recursive case? a. line #3 b. line #4 c. line #2 d. line #1

b. line #4

Consider the mutually recursive methods below. Select the method call that could be used to generate the output sequence: A5 B4 A3 B2 A1 public static void methodA(int value) { if (value > 0) { System.out.print(" A" + value); methodB(value - 1); } } public static void methodB(int value) { if (value > 0) { System.out.print(" B" + value); methodA(value - 1); } } a. methodA(4); b. methodA(5); c. methodB(5); d. methodB(4);

b. methodA(5);

The method below generates all substrings of a String passed as argument. Assuming that the string contains no duplicate characters, select the statement to complete the method so that it prints all substrings correctly. public static void printSubstrings(String word) { if (word.length() > 0) { for (int j = 1; j <= word.length(); j++) // print substrings that begin with the first character { System.out.println(word.substring(0, j)); } _________________________________ // print substrings without the first character } } a. printSubstrings(word.substring(word.length() - 1)); b. printSubstrings(word.substring(1)); c. printSubstrings(word.substring(0)); d. printSubstrings(word.substring(word.length()));

b. printSubstrings(word.substring(1));

Complete the following code snippet, which is intended to be a recursive method that will find the sum of all elements in an array of double values from index to the end of the array: // return the sum of all elements in arr[] public static double findSum(double arr[], int index) { if (index == 0) { _____________________ } else { return (arr[index] + findSum(arr, index - 1)); } } Assume that this method would be called using an existing array named myArray as follows: findSum(myArray,myArray.length - 1); a. return arr[index + 1]; b. return arr[index]; c. return arr[index - 1]; d. return arr[1];

b. return arr[index];

Complete the following code snippet, which is intended to determine if a value is even or odd using mutual recursion: public static boolean isEven(int n) { if (n == 0) { return true; } else { return isOdd(Math.abs(n) - 1); } } public static boolean isOdd(int n) { if (n == 0) { _________ } else { return isEven(Math.abs(n) - 1); } } a. return isOdd(Math.abs(n)-1); b. return false; c. return isOdd(Math.abs(n)); d. return true;

b. return false;

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) { return s; } else { ________________________ } } a. return reverseIt(s.substring(0)) + s.charAt(1); b. return reverseIt(s.substring(1)) + s.charAt(0); c. return reverseIt(s.substring(1)) + s.charAt(1); d. return reverseIt(s.substring(0)) + s.charAt(0);

b. return reverseIt(s.substring(1)) + s.charAt(0);

Assume that recursive method search returns true if argument value is one of the elements in the section of the array limited by the firstIndex and lastIndex arguments. What statement can be used in main to determine if the value 7 is one of the elements in array values? public static boolean search(int value, int[] array, int firstIndex, int lastIndex) { if (firstIndex <= lastIndex) { if (array[firstIndex] == value) { return true; } else { return search(value, array, firstIndex + 1, lastIndex); } } return false; } public static void main(String[] args) { int [] values = { 4, 7, 1, 0, 2, 7 }; if (_________________________________ ) { System.out.println("7 is in the array"); } } a. search(7, values, 0, values.length) b. search(7, values, 0, values.length - 1) c. search(7, values, 1, values.length) d. search(7, values, 1, values.length - 1)

b. search(7, values, 0, values.length - 1)

In recursion, the non-recursive case is analogous to a loop ____. a. call b. termination c. condition d. iteration

b. termination

Consider the fib method from the textbook shown below. public static long fib(int n) { if (n <= 2) { return 1; } else { return fib(n - 1) + fib(n - 2); } } Calling fib(3) will trigger ___ recursive call(s) and execute the terminating condition ___ time(s), respectively. a. 1, 2 b. 1, 1 c. 2, 2 d. 2, 1

c. 2, 2

Consider the recursive version of the fib method from the textbook shown below: public static long fib(int n) { if (n <= 2) { return 1; } else { return fib(n - 1) + fib(n - 2); } } How many recursive calls to fib(2) will be made from the original call of fib(6)? a. 3 b. 4 c. 5 d. 2

c. 5

Given the following class code: public class RecurseSample { public static void main(String[] args) { System.out.println(recurse(3)); } public static int recurse(int n) { int total = 0; if (n == 0) { return 0; } else { total = 3 + recurse(n - 1); } return total; } } What values will be printed when this code is executed? a. 6 b. 1, 3, 6, and 9 c. 9 d. 3, 6, and 9

c. 9

Would switching the special case order affect the return value of the following method? public int mystery(int n, int m) { if (n == 0) // special case #1 { return 0; } if (n == 1) // special case #2 { return m; } return m + mystery(n - 1, m); } a. An exception will be thrown. b. Yes c. No d. It is impossible to tell.

c. No

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 is the purpose of the palindrome method? a. Return the palindrome to the calling method. b. Send the recursive isPal method its terminating condition. c. Provide the string, along with its first and last indexes to the recursive isPal method. d. Recursively call itself.

c. Provide the string, along with its first and last indexes to the recursive isPal method.

Consider the method below, which prints the digits of an arbitrary integer in reverse order, one digit per line. The method should print the last digit first. Then, it should recursively print the integer obtained by removing the last digit. Select the statements that should be used to complete the method. public static void printReverse(int value) { if (value > 0) { _____________________ // print last digit _____________________ // recursive call to print value without last digit } } a. System.out.println(value % 10); printReverse(value % 10); b. System.out.println(value / 10); printReverse(value % 10); c. System.out.println(value % 10); printReverse(value / 10); d. System.out.println(value / 10); printReverse(value / 10);

c. System.out.println(value % 10); printReverse(value / 10);

Consider the fib method from the textbook shown below: public static long fib(int n) { if (n <= 2) { return 1; // line #1 } else { return fib(n - 1) + fib(n - 2); // line #2 } } Assume line #1 is changed to this: if (n <= 2) { return n; } What effect will this change have? a. This will return 6 from fib(6) b. This will return 21 from fib(6) c. This will return 13 from fib(6) d. This will return 8 from fib(6)

c. This will return 13 from fib(6)

Consider the getArea method from the textbook shown below: public int getArea() { if (width <= 0) { return 0; } // line #1 else if (width == 1) { return 1; } // line #2 else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } } Assume the code in line #3 is changed to: Triangle smallerTriangle = new Triangle(width); This change would cause infinite recursion for which triangles? a. Triangles of any width. b. Those with width equal to 1. c. Those with width greater than or equal to 2. d. Those with width equal to 0.

c. Those with width greater than or equal to 2.

Consider the following recursive code snippet: public int mystery(int n, int m) { if (n == 0) { return 0; } if (n == 1) { return m; } return m + mystery(n - 1, m); } What parameter values for n would cause an infinite recursion problem in the following method? a. all n with n >= 0 b. n == 0 c. all n with n < 0 d. n == 1

c. all n with n < 0

Complete the code for the calcPower recursive method shown below, which is intended to raise the base number passed into the method to the exponent power passed into the method: public static int calcPower(int baseNum, int exponent) { int answer = 0; if (exponent == 0) { answer = 1; } else { _______________________________________ } return answer; } a. answer = baseNum * calcPower (baseNum, exponent); b. answer = baseNum * calcPower (baseNum -1, exponent - 1); c. answer = baseNum * calcPower (baseNum, exponent - 1); d. answer = baseNum * calcPower (baseNum -1, exponent);

c. answer = baseNum * calcPower (baseNum, exponent - 1);

Consider the following code snippet for recursive addition: int add(int i, int j) { // assumes i >= 0 if (i == 0) { return j; } else { return add(i - 1, j + 1); } } Identify the terminating condition in this recursive method. a. return add(i - 1, j + 1) b. return j c. if (i == 0) d. there is no terminating condition

c. if (i == 0)

Question 1 1 pts Complete the code for the myFactorial recursive method shown below, which is intended to compute the factorial of the value passed to the method: public int myFactorial(int anInteger) { _____________________________ { return 1; } else { return(anInteger * myFactorial(anInteger - 1)); } } a. if (anInteger * (anInteger - 1) == 1) b. if (myFactorial(anInteger) == 1) c. if (anInteger == 1) d. if ((anInteger - 1) == 1)

c. if(anInteger == 1)

Consider the getArea method from the textbook shown below. public int getArea() { if (width <= 0) { return 0; } // line #1 else if (width == 1) { return 1; } // line #2 else { Triangle smallerTriangle = new Triangle(width - 1); // line #3 int smallerArea = smallerTriangle.getArea(); // line #4 return smallerArea + width; // line #5 } } Where is/are the recursive call(s)? a. lines #1 and #2 b. line #2 c. line #4 d. line #1

c. line #4

The method below implements the exponentiation operation recursively by taking advantage of the fact that, if the exponent n is even, then xn = (xn/2)2. Select the expression that should be used to complete the method so that it computes the result correctly. public static double power(double base, double exponent) { if (exponent % 2 != 0) // if exponent is odd { return base * power(base, exponent - 1); } else if (exponent > 0) { double temp = ________________________ ; return temp * temp; } return base; } a. power(base, exponent / 2) * power(base, exponent / 2) b. power(base, exponent / 2) + power(base, exponent / 2) c. power(base, exponent) / 2 d. power(base, exponent / 2)

c. power(base, exponent) / 2

Complete the code for the myFactorial recursive method shown below, which is intended to compute the factorial of the value passed to the method: public int myFactorial(int anInteger) { if (anInteger == 1) { return 1; } else { ______________________ } } a. return ((anInteger - 1) * (myFactorial(anInteger))); b. return (anInteger * (myFactorial(anInteger))); c. return (anInteger * (myFactorial(anInteger - 1))); d. return ((anInteger - 1)*(myFactorial(anInteger - 1)));

c. return (anInteger * (myFactorial(anInteger - 1)));

Consider the helper method reversePrint, which uses recursion to display in reverse the elements in a section of an array limited by the firstIndex and lastIndex arguments. What statement should be used to complete the recursive method? public static void reversePrint(int[] array, int firstIndex, int lastIndex) { if (firstIndex < lastIndex) { ________________________________________ } System.out.println(array[firstIndex]); } public static void main(String[] args) { int [] numbers = { 4, 7, 1, 0, 2, 7 }; reversePrint(numbers, 0, numbers.length - 1); } a. reversePrint(array, firstIndex + 1, lastIndex - 1); b. reversePrint(array, firstIndex, lastIndex + 1); c. reversePrint(array, firstIndex + 1, lastIndex); d. reversePrint(array, firstIndex, lastIndex - 1);

c. reversePrint(array, firstIndex + 1, lastIndex);

Which of the following strings is a palindrome? a. "Test" b. "canal" c. "salami" d. "B"

d. "B"

Consider the recursive version of the fib method from the textbook shown below: public static long fib(int n) { if (n <= 2) { return 1; } else { return fib(n - 1) + fib(n - 2); } } How many more recursive calls to fib will be made from the original call of fib(7) than from the original call of fib(6) (not counting the original calls)? a. 1 b. 5 c. 2 d. 10

d. 10

Consider the recursive square method shown below that takes a non-negative int argument. public int square(int n) { return square(n, n); } public int square(int c, int n) { if (c == 1) { return n; } else { return n + square(c - 1, n); } } Assume that the last return statement is changed to this: return n * square(c - 1, n); What would a call to square(4) return? a. 16 b. 64 c. 4 d. 256

d. 256

Consider the following recursive code snippet: public int mystery(int n, int m) { if (n == 0) { return 0; } if (n == 1) { return m; } return m + mystery(n - 1, m); } What value is returned from a call tomystery(1,5)? a. 6 b. 1 c. 11 d. 5

d. 5

Consider the method powerOfTwo shown below: public boolean powerOfTwo(int n) { if (n == 1) // line #1 { return true; } else if (n % 2 == 1) // line #2 { return false; } else { return powerOfTwo(n / 2); // line #3 } } How many recursive calls are made from the original call of powerOfTwo(64) (not including the original call)? a. 4 b. 8 c. 2 d. 6

d. 6

Recursion does NOT take place if any of the following happen: I method A calls method B, which calls method C, which calls method B II method A calls method B, which calls method A III method A calls method B, B returns, and A calls B again a. I b. II c. I and II d. III

d. III

____ recursion can occur when a recursive algorithm does not contain a special case to handle the simplest computations directly. a. Terminating condition b. Mutual c. Non-mutual d. Infinite

d. Infinite

Consider the fib method from the textbook shown below: public static long fib(int n) { if (n <= 2) { return 1; // line #1 } else { return fib(n - 1) + fib(n - 2); // line #2 } } Assume line #1 is changed to this: if (n <= 2) { return 2; } How will this change affect the result of calling fib(7)? a. It will add 8 to the return from fib(7) b. It will add 2 to the return from fib(7) c. It will add 4 to the return from fib(7) d. It will double the return from fib(7)

d. It will double the return from fib(7)

Consider the method powerOfTwo shown below: public boolean powerOfTwo(int n) { if (n == 1) // line #1 { return true; } else if (n % 2 == 1) // line #2 { return false; } else { return powerOfTwo(n / 2); // line #3 } } What is the best interpretation of line #1? a. 1 is an invalid choice for n b. One is not a power of two c. Any multiple of one is a power of two d. One is a power of two

d. One is a power of two

When a recursive method is called, and it does not perform recursion, what must be true? a. All recursive case conditions were true. b. An exception will occur in the method. c. One recursive case condition was true. d. The terminating condition was true.

d. The terminating condition was true.

Complete the following code snippet, which is intended to be a recursive method that will find the smallest value in an array of double values from index to the end of the array: public static double minVal(double[] elements, int index) { if (index == elements.length - 1) { return elements[index]; } double val = __________________; if (elements[index] < val) { return elements[index]; } else { return val; } } a. minVal(elements, index - 1) b. minVal(index + 1) c. minVal(index - 1) d. minVal(elements, index + 1)

d. minVal(elements, index + 1)

A recursive method without a special terminating case would _________ a. not be recursive. b. be more efficient. c. end immediately. d. never terminate.

d. never terminate

Consider the method below, which implements the exponentiation operation recursively. Select the statement that should be used to complete the method, so that it handles the special case correctly. public static double power(int base, int exponent) { if (exponent == 0) { _______________ } else { return base * power(base, exponent - 1); } } a. return 1 * power(base, exponent - 1); b. return base; c. return 0; d. return 1;

d. return 1;

In recursion, the terminating condition is analogous to a loop _________. a. initialization condition b. iteration c. call d. termination condition

d. termination condition


Ensembles d'études connexes

12 PEDIATRIC SUCCESS ORTHOPEDIC DISORDERS

View Set

Physical Science Track In-class Notes

View Set

Med Surg I Exam 1 Practice Questions

View Set

Lesson 10: Airway Management, Chapter 10 - EMT

View Set

DRx 2 Cardiovascular Module MS 221

View Set