Exam 13: Recursion

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

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

(Multiple Choice)
5.0/5
(35)

Consider 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) // line #1 { Return 0; // line # } Else { Return (n + printSum(n)); //line #3 } } Which of the following statements is correct?

(Multiple Choice)
4.9/5
(41)

Why does the best recursive method usually run slightly slower than its iterative counterpart?

(Multiple Choice)
4.9/5
(43)

In recursion, the terminating condition is analogous to a loop _________.

(Multiple Choice)
4.8/5
(45)

The string "eat" has ____ permutations.

(Multiple Choice)
4.9/5
(33)

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 ____.

(Multiple Choice)
4.9/5
(32)

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

(Multiple Choice)
4.8/5
(43)

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 { Reurn base * power(base, exponent - 1); } }

(Multiple Choice)
4.8/5
(38)

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?

(Multiple Choice)
4.9/5
(40)

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?

(Multiple Choice)
4.8/5
(43)

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.

(Multiple Choice)
4.9/5
(39)

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

(Multiple Choice)
4.8/5
(45)

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

(Multiple Choice)
4.9/5
(29)

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

(Multiple Choice)
4.9/5
(30)

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

(Multiple Choice)
4.9/5
(40)

A recursive method without a special terminating case would _________

(Multiple Choice)
4.7/5
(38)

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 { _________ } Else { Return reverseIt; } }

(Multiple Choice)
4.8/5
(41)

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

(Multiple Choice)
4.7/5
(39)

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

(Multiple Choice)
4.8/5
(30)

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

(Multiple Choice)
4.8/5
(34)
Showing 81 - 100 of 110
close modal

Filters

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