Computer Programming II Quiz8

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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.

2, 2

Consider the method below, which prints the digits of an arbitrary positive 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 } }

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

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 isPal as shown here would be considered to be a ____ method.

recursive helper

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

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.

1, 2, and 3

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

10

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, including the original call?

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 total recursive calls (not counting the original call) to fib will be made from the original call of fib(7)?

24

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

256

Consider the problem of arranging matchsticks so as to form a row of squares, as shown below for three squares and ten matchsticks. - - - | | | | - - - 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 ___________________________ } }

3 + matchsticks(squares - 1);

Consider 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) // line #1 { return 1; // line #2 } else { return (anInteger * myFactorial(anInteger - 1)); // line #3 } } If you set a breakpoint on line #2 and then call myFactorial(4), how many calls to myFactorial will be visible on the debugger's call stack when the program suspends for the breakpoint?

4

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

6

Consider the square method shown below that takes a non-negative int argument: public int square(int n) { return squareHelper(n, n); } public int squareHelper(int c, int n) { if (c == 1) { return n; } else { return n + squareHelper(c - 1, n); } } Assume that the last return statement in the squareHelper method is changed to this: return squareHelper(c - 1, n); What would a call to square(7) return?

7

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

8

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

A terminating condition was true.

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?

An empty or one-character string is considered a palindrome.

Which problem-solving technique examines partial solutions, abandons unsuitable ones, and returns to consider other candidate solutions.

Backtracking

Which statement is true about backtracking?

Backtracking starts with a partial solution and builds it up to get closer to the goal.

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

Each recursive method call takes processor time.

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

I only

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.

II and III only

Which of the following executions represent mutual recursion? I method E calls method T, which calls method F II method E calls method T, which calls method F, which calls method E III method F calls method F

II only

Which of the following statements about recursion is correct?

In many cases, a recursive solution may be easier to understand and to implement than an iterative solution.

____ recursion can occur when a recursive algorithm does not contain a special case to handle the simplest computations directly.

Infinite

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

Infinite recursion will occur.

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. public static ArrayList<String> permutations(String word) { ArrayList<String> result = new ArrayList<String>(); if (word.length() == 0) // line #1 { result.add(word); // line #2 return result; // line #3 } else { for (int i = 0; i < word.length(); i++) // line #4 { String shorter = word.substring(0, i) + word(substring(i + 1); // line #5 ArrayList<String> shorterPermutations = permutations(shorter); // line #6 for (String s : shorterPermutations) // line #7 { result.add(word.charAt(i) + s); // line #8 } } return result; // line #9 } } If line #8 is changed to add the removed character to the end of each permutation of the shorter word, which change best describes the returned list? result.add(s + word.charAt(i)); // line #8 revised

It contains all permutations.

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. How does the permutations method simplify its input for the recursive call? public static ArrayList<String> permutations(String word) { ArrayList<String> result = new ArrayList<String>(); if (word.length() == 0) // line #1 { result.add(word); // line #2 return result; // line #3 } else { for (int i = 0; i < word.length(); i++) // line #4 { String shorter = word.substring(0, i) + word(substring(i + 1); // line #5 ArrayList<String> shorterPermutations = permutations(shorter); // line #6 for (String s : shorterPermutations) // line #7 { result.add(word.charAt(i) + s); // line #8 } } return result; // line #9 } }

It finds permutations of shorter words formed by removing the ith character.

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. What special cases for the simplest values are used by the permutations method to terminate the recursion? public static ArrayList<String> permutations(String word) { ArrayList<String> result = new ArrayList<String>(); if (word.length() == 0) // line #1 { result.add(word); // line #2 return result; // line #3 } else { for (int i = 0; i < word.length(); i++) // line #4 { String shorter = word.substring(0, i) + word(substring(i + 1); // line #5 ArrayList<String> shorterPermutations = permutations(shorter); // line #6 for (String s : shorterPermutations) // line #7 { result.add(word.charAt(i) + s); // line #8 } } return result; // line #9 } }

It terminates the recursion when it encounters the empty string.

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

No

Consider the code for the recursive method mystery shown in this code snippet: public static int mystery(int n) { if (n == 0) { return 0; } else { return (n + mystery(n-1)); } } What will be printed by the statement System.out.println(mystery(-4));?

Nothing - a StackoverflowError exception will occur

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?

One is a power of two

If recursion does not have a special terminating case, what error will occur?

Stack overflow

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?

The method will still return correct results for all non-negative width triangles.

Which of the following is NOT true about debugging a recursive method by setting a breakpoint on the line containing a return statement?

You cannot debug a recursive method with the debugger.

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? Correct Answer

all n with n < 0

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.

i == 0

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

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

line #4

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. Which line contains the recursive call in the permutations method? public static ArrayList<String> permutations(String word) { ArrayList<String> result = new ArrayList<String>(); if (word.length() == 0) // line #1 { result.add(word); // line #2 return result; // line #3 } else { for (int i = 0; i < word.length(); i++) // line #4 { String shorter = word.substring(0, i) + word(substring(i + 1); // line #5 ArrayList<String> shorterPermutations = permutations(shorter); // line #6 for (String s : shorterPermutations) // line #7 { result.add(word.charAt(i) + s); // line #8 } } return result; // line #9 } }

line #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 } } Where is/are the terminating condition(s)?

lines #1 and #2

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

methodA(5);

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?

n <= 0 or n == 1

The method below implements the exponentiation operation recursively by taking advantage of the fact that, if the exponent n is even, then xn = (x n/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, int 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; }

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

return (anInteger * (myFactorial(anInteger - 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)); } }

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

return elements[index];

Consider the square method shown below that takes a non-negative int argument. Complete the code for the square method so that it correctly calls the squareHelper method to produce the square of n. public int square(int n) { ____________________; } public int squareHelper(int c, int n) { if (c == 1) { return n; } else { return n + squareHelper(c - 1, n); } }

return squareHelper(n, n)

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

reversePrint(array, firstIndex + 1, lastIndex);

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

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


संबंधित स्टडी सेट्स

Capítulo 7: Qué te gusta comer?

View Set

Mobility, Clotting, Transfusion Quiz

View Set

Quiz #5 - Persepolis and Monsieur Ibrahim

View Set

Chapter 3 Part 2 Cells the Living Units

View Set

Business Law Consideration Chapter 6

View Set

Writing and Argumentative Essay about the Nobel Prize in Literature 100%

View Set