Exam 12: Recursion

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

For the questions below, consider the following representation of grid and the maze code from Chapter 11. Grid: 1 1 1 1 1 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 Code: public boolean traverse(int row, int column) { if (valid(row, column)) { boolean done = false; grid[row][column] = TRIED; if (row == grid.length - 1 && column == grid[0].length - 1) done = True; else { done = traverse(row + 1, column); if (!done) done = traverse(row, column + 1); if (!done) done = traverse(row - 1, column); if (!done) done = traverse(row, column - 1); } if (done) grid[row][column] = PATH; } return done; } Assume valid returns True if row and column are >= 0 and <= the grid's row length or column length and the entry at this position = = 1. And assume TRIED = 3 and PATH = 7 -Assume at some point in processing, grid's row 0 has become 3 3 3 1 1 1 0 0. Which direction will next be tried?

(Multiple Choice)
4.8/5
(41)

If one were to create a Towers of Hanoi puzzle with four towers instead of three, with rules changed appropriately, it should be possible to create a recursive solution for the puzzle where one of the constraints is that at some point in the solution all of the disks must reside on each of the four towers.

(True/False)
4.9/5
(42)

As identified in the text, some algorithms execute approximately 2ⁿ operations if the original parameter or size of input is n. Compare the two values n and 2ⁿ by giving a table for n = 1, 5, 10, 20, 30, and 40.

(Essay)
4.8/5
(37)

We can define a list of int values recursively as: a list_item, followed by a comma, followed by a list where a list_item is any int value.

(True/False)
4.8/5
(44)

The following method recognizes whether a String parameter consists of a specific pattern and returns True if the String has that pattern, false otherwise. Use this recursive method to answer the questions below. public boolean patternRecognizer(String a) { if (a == null) return false; else if (a.length( ) = = 1 | | (a.length( ) = = 2 && a.charAt(0) = = a.charAt(1) ) ) return True; else if (a.length( ) = = 2 && a.charAt(0) != a.charAt(1) ) return false; else if (a.charAt(0) == a.charAt(a.length( ) - 1)) return patternRecognizer(a.substring(1, a.length( ) - 1)); else return false; } -Which String below would result in patternRecognizer returning True?

(Multiple Choice)
4.9/5
(34)

The Euclidean algorithm for calculating the greatest common divisor (gcd) of two integers a and b is: "If a is a nonnegative integer, b is a positive integer, and r = a mod b, then gcd(a,b) = gcd(b,r). Write a recursive method that uses the Euclidean algorithm to calculate the gcd.

(Essay)
4.8/5
(42)

As identified in the text, some algorithms execute only (approximately) log₂n operations if the original parameter or size of input is n. Compare this to an algorithm that executes n times by providing a table demonstrating the values of n and log₂n for n = 1, 10, 100, 1000, 10,000, 100,000 and 1,000,000.

(Essay)
4.8/5
(41)

Provide a definition for the terms as they relate to programming: recursion, indirect recursion and infinite recursion.

(Essay)
5.0/5
(31)

Describe how to solve the Towers of Hanoi problem using 4 disks (that is, write down move for move how to solve the problem).

(Essay)
4.8/5
(26)

The following method recognizes whether a String parameter consists of a specific pattern and returns True if the String has that pattern, false otherwise. Use this recursive method to answer the questions below. public boolean patternRecognizer(String a) { if (a == null) return false; else if (a.length( ) = = 1 | | (a.length( ) = = 2 && a.charAt(0) = = a.charAt(1) ) ) return True; else if (a.length( ) = = 2 && a.charAt(0) != a.charAt(1) ) return false; else if (a.charAt(0) == a.charAt(a.length( ) - 1)) return patternRecognizer(a.substring(1, a.length( ) - 1)); else return false; } -A recursive algorithm is superior to an iterative algorithm along which of the following criteria?

(Multiple Choice)
4.7/5
(37)

Rewrite the following iterative method as a recursive method that computes the same thing. NOTE: your recursive method will require an extra parameter. public String reversal(String x) { int y = x.length( ); String s = ""; for (int j = y-1; j >=0; j--) s += x.charAt(j); return s; }

(Essay)
4.9/5
(41)

The following method lacks a base case. public int noBaseCase(int x) { if (x > 0) return noBaseCase(x - 1) + 1; else return noBaseCase(x - 2) + 2; }

(True/False)
4.8/5
(40)

The following two methods will both compute the same thing when invoked with the same value of x. That is, method1(x) = = method2(x). public int method1(int x) { if (x > 0) return method1(x - 1) + 1; else return 0; } public int method2(int x) { if (x > 0) return 1 + method2(x - 1); else return 0; }

(True/False)
4.8/5
(47)

For the questions below, assume that int[ ] a = {6, 2, 4, 6, 2, 1, 6, 2, 5} and consider the two recursive methods below foo and bar. public int foo(int[ ] a, int b, int j) { if (j < a.length) if (a[j] != b) return foo (a, b, j+1); else return foo (a, b, j+1) + 1; else return 0; } public int bar(int[ ] a, int j) { if (j < a.length) return a[I] + bar(a, j+1); else return 0; } -What is the result of calling foo(a, 3, 0);?

(Multiple Choice)
4.7/5
(45)

The Koch fractal of order 1 is

(Multiple Choice)
5.0/5
(27)

For the questions below, recall the Towers of Hanoi recursive solution. -If there are 6 disks to move from one Tower to another, how many disk movements would it take to solve the problem using the recursive solution?

(Multiple Choice)
4.9/5
(43)

What does the following method compute? Assume the method is called initially with i = 0 Public int question9(String a, char b, int i) { If (i = = a.length( )) return 0; Else if (b = = a.charAt(i)) return question9(a, b, i+1) + 1; Else return question9(a, b, i+1); }

(Multiple Choice)
4.9/5
(49)

The game of high-low is one where one person selects a number between 1 and 100 and a user tries to guess it by guessing a number and being told if the guessed number is the right number, too low or too high. The user repeats guessing until getting the correct answer. A logical user will always guess at the midpoint of the possible values (for instance, the first guess is 50 followed by either 25 or 75, etc). Write a recursive method to play the game of high-low by having the computer guess a mid point. The method should receive three parameters, the number itself, the lowest value in the range and the highest value in the range and return the number of guesses that it took to guess the right number.

(Essay)
4.9/5
(46)

For the questions below, consider the following representation of grid and the maze code from Chapter 11. Grid: 1 1 1 1 1 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 1 0 0 0 1 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 Code: public boolean traverse(int row, int column) { if (valid(row, column)) { boolean done = false; grid[row][column] = TRIED; if (row == grid.length - 1 && column == grid[0].length - 1) done = True; else { done = traverse(row + 1, column); if (!done) done = traverse(row, column + 1); if (!done) done = traverse(row - 1, column); if (!done) done = traverse(row, column - 1); } if (done) grid[row][column] = PATH; } return done; } Assume valid returns True if row and column are >= 0 and <= the grid's row length or column length and the entry at this position = = 1. And assume TRIED = 3 and PATH = 7 -Define the magnitude of a number as the location of the decimal point from the left of the number (that is, if a number has 4 digits followed by the decimal point, it will have a magnitude of 4). 100 would then have a magnitude of 3 and 55,555.555 would have a magnitude of 5. A partial recursive method is given below to compute a positive int parameter's magnitude. Which answer below is needed to complete the method? Public int magnitude(double x) { If (x < 1) return 0; Else return _______; }

(Multiple Choice)
4.8/5
(43)

Some problems are easier to solve recursively than iteratively.

(True/False)
5.0/5
(36)
Showing 41 - 60 of 68
close modal

Filters

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