Exam 12: Recursion
Exam 1: Introduction65 Questions
Exam 2: Data and Expressions77 Questions
Exam 3: Using Classes and Objects58 Questions
Exam 4: Writing Classes56 Questions
Exam 5: Conditionals and Loops37 Questions
Exam 6: More Conditionals and Loops36 Questions
Exam 7: Object-Oriented Design76 Questions
Exam 8: Arrays70 Questions
Exam 9: Inheritance71 Questions
Exam 10: Polymorphism70 Questions
Exam 11: Exceptions68 Questions
Exam 12: Recursion68 Questions
Exam 13: Collections68 Questions
Select questions type
For the questions below, use the following recursive method.
public int question1_2(int x, int y)
{
if (x == y) return 0;
else return question1_2(x-1, y) + 1;
}
-The following method should return True if the int parameter is even and either positive or 0, and false otherwise. Which set of code should you use to replace ... so that the method works appropriately?
Public boolean question3(int x) { ... }
Free
(Multiple Choice)
4.8/5
(32)
Correct Answer:
C
The recursive method to solve the Towers of Hanoi is usable only if the parameter for the number of disks is 7 or smaller.
Free
(True/False)
4.8/5
(31)
Correct Answer:
False
Demonstrate how factorial(4) is computed given the following recursive method for factorial:
public int factorial(int n)
{
if (n > 1) return factorial(n - 1) * n;
else return 1;
}
Free
(Essay)
4.9/5
(36)
Correct Answer:
factorial(4) = factorial(3) * 4
factorial(3) = factorial(2) * 3
factorial(2) = factorial(1) * 2
factorial(1) = 1
factorial(2) = 1 * 2 = 2
factorial(3) = 2 * 3 = 6
factorial(4) = 6 * 4 = 24
Write a recursive method called numSegments(int order) that yields the number of line segments in a Koch snowflake of order "order."
(Essay)
4.8/5
(44)
Rewrite the following iterative method as a recursive method that returns the same String.
public String listOfNumbers( )
{
String s = "";
for (j = 1; j<10; j++)
s += j;
return s;
}
(Essay)
4.8/5
(44)
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, 2, 0);?
(Multiple Choice)
4.9/5
(34)
Recursion is a popular programming tool but beginning programmers tend to shy away from it. Why do you suppose this is True?
(Essay)
4.9/5
(50)
For the questions below, recall the Towers of Hanoi recursive solution.
-If there are 2 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
(40)
For the questions below, refer to the following recursive factorial method.
public int factorial(int x)
{
if (x > 1) return x * factorial (x - 1);
else return 1;
}
-What is returned if factorial(3) is called?
(Multiple Choice)
4.7/5
(37)
What does the following recursive method determine?
Public boolean question16(int[ ]a, int[ ] b, int j)
{
If (j = = a.length) return false;
Else if (j = = b.length) return True;
Else return question16(a, b, j+1);
}
(Multiple Choice)
4.8/5
(34)
It always is possible to replace a recursion by an iteration and vice versa.
(True/False)
5.0/5
(41)
For the Towers of Hanoi problem, show how many moves it will take to solve the problem with 5 disks, 6 disks, 7 disks, 8 disks, 9 disks, and 10 disks.
(Essay)
4.8/5
(46)
What is wrong with the following recursive sum method? The method is supposed to sum up the values between 1 and x (for instance, sum(5) should be 5 + 4 + 3 + 2 + 1 = 15).
Public int sum(int x)
{
If (x = = 0) return 0;
Else return sum(x - 1) + x;
}
(Multiple Choice)
4.8/5
(37)
Why is the following method one which has infinite recursion?
Public int infiniteRecursion(int n)
{
If (n > 0) return infiniteRecursion(n) + 1;
Else return 0;
}
(Multiple Choice)
4.9/5
(39)
For the questions below, use the following recursive method.
public int question1_2(int x, int y)
{
if (x == y) return 0;
else return question1_2(x-1, y) + 1;
}
-Calling this method will result in infinite recursion if which condition below is initially True?
(Multiple Choice)
4.8/5
(40)
For the questions below, recall the Towers of Hanoi recursive solution.
-Which of the following methods would properly compute the value of x % y (x mod y) recursively, assuming that x and y not negative numbers?
(Multiple Choice)
4.8/5
(22)
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
-Which of the following grids would be the result after traverse has completed all of its recursive calls?
(Multiple Choice)
4.8/5
(37)
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, 2, 9);?
(Multiple Choice)
4.9/5
(41)
Showing 1 - 20 of 68
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)