Exam 13: Recursion

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

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?

(Multiple Choice)
4.9/5
(47)

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.

(Multiple Choice)
4.8/5
(28)

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?

(Multiple Choice)
4.8/5
(49)

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

(Multiple Choice)
4.9/5
(38)

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

(Multiple Choice)
4.8/5
(23)

Consider the problem of displaying a pattern of asterisks which form a triangle of height h, as shown below for h = 4: * ** *** **** *** ** * The problem can be solved with the recursive helper method shape below. The method takes two parameters: low and high. It first prints a row of asterisks corresponding to parameter low. If high is higher than low, it recursively generates a shape in which low is incremented by one, and then prints another row of low asterisks. Select a statement to complete method triangle, so that the helper method shape is invoked with the correct arguments in order to display a triangle with parameter height. Public static void shape(int low, int high) { If (high >= low) { AsterisksRow(low); If (high > low) { Shape(low + 1, high); AsterisksRow(low); } } } Public static void asterisksRow(int n) // Method to display a row of n stars { For (int j = 1; j < n; ++j) { System.out.print("*"); } System.out.println("*"); } Public static void triangle(int height) { If (height > 1) { _______________________ } }

(Multiple Choice)
4.9/5
(29)

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.

(Multiple Choice)
4.8/5
(37)

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(1,5)?

(Multiple Choice)
4.8/5
(41)

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

(Multiple Choice)
4.8/5
(44)

When a recursive method is called, and it does not perform recursion, what must be true?

(Multiple Choice)
4.9/5
(39)

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

(Multiple Choice)
4.8/5
(37)

Backtracking _____.

(Multiple Choice)
4.9/5
(30)

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?

(Multiple Choice)
4.9/5
(34)

Which of the following statements about palindromes is correct?

(Multiple Choice)
4.9/5
(32)

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 changed to this: If (width == 1) { return 1; } // new line #1-2 What will happen when this code is executed?

(Multiple Choice)
4.9/5
(40)

What is the purpose of a recursive helper method?

(Multiple Choice)
5.0/5
(30)

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) { _____________________ } Else { Answer = baseNum * calcPower (baseNum, exponent - 1); } Return answer; }

(Multiple Choice)
4.7/5
(41)

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?

(Multiple Choice)
4.9/5
(44)

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

(Multiple Choice)
4.8/5
(37)

Consider the problem of arranging matchsticks so as to form a row of rectangles, as shown below.----- |--|--|--- Complete the recursive method below, which is designed to return the number of matchsticks needed to form n rectangles. Public static int matchsticks(int rectangles) { If (rectangles == 1) // 1 square can be formed with 6 matchsticks { Return 6; } Else { Return ___________________________ } }

(Multiple Choice)
4.7/5
(45)
Showing 41 - 60 of 110
close modal

Filters

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