Exam 13: Recursion

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

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

(Multiple Choice)
4.9/5
(44)

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?

(Multiple Choice)
4.8/5
(40)

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

(Multiple Choice)
4.9/5
(41)

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

(Multiple Choice)
4.9/5
(42)

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

(Multiple Choice)
4.8/5
(35)

Consider the recursive method myPrint 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 is printed for the call myPrint(821)?

(Multiple Choice)
4.9/5
(37)

Given the following class code: public class RecurseMore { Private static int total; Public static void main(String[] args) { System.out.println(recurse(4)); } Public static int recurse(int n) { Int total = 0; If (n == 0) { Return 0; } Else { Total = 4 + recurse(n - 2); } Return total; } } What values will be printed when this code is executed?

(Multiple Choice)
4.7/5
(39)

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

(Multiple Choice)
4.9/5
(34)

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

(Multiple Choice)
4.9/5
(35)

If a recursive method does not simplify the computation within the method and the base case is not called, what will be the result?

(Multiple Choice)
4.9/5
(35)
Showing 101 - 110 of 110
close modal

Filters

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