Exam 13: Recursion

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

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 { ______________________ } }

Free
(Multiple Choice)
4.9/5
(39)
Correct Answer:
Verified

C

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)?

Free
(Multiple Choice)
5.0/5
(41)
Correct Answer:
Verified

B

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?

Free
(Multiple Choice)
4.8/5
(39)
Correct Answer:
Verified

A

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)?

(Multiple Choice)
5.0/5
(44)

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?

(Multiple Choice)
4.8/5
(44)

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) { ____________________ } } }

(Multiple Choice)
4.8/5
(37)

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?

(Multiple Choice)
4.8/5
(40)

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?

(Multiple Choice)
4.7/5
(30)

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

(Multiple Choice)
4.8/5
(36)

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?

(Multiple Choice)
4.8/5
(32)

What is required to make a recursive method successful? I special cases that handle the simplest computations directly II a recursive call to simplify the computation III a mutual recursion

(Multiple Choice)
4.8/5
(33)

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"); } }

(Multiple Choice)
4.9/5
(45)

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)); } }

(Multiple Choice)
4.8/5
(38)

A unique permutation is one that is different from any other generated permutation. How many unique permutations does the string "aaa" have?

(Multiple Choice)
4.9/5
(37)

____ is a problem-solving technique that examines partial solutions, abandons unsuitable ones, and returns to consider other candidate solutions.

(Multiple Choice)
4.9/5
(32)

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.

(Multiple Choice)
4.7/5
(44)

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; }

(Multiple Choice)
4.9/5
(43)

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?

(Multiple Choice)
4.9/5
(32)

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);

(Multiple Choice)
4.8/5
(38)

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 left >= right refer to?

(Multiple Choice)
4.8/5
(33)
Showing 1 - 20 of 110
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)