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, 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 bar(a, 0);?
(Multiple Choice)
4.8/5
(42)
Describe the difference(s) between the following two sets of code, neither of which reaches a terminating condition.
public void forever1( )
{
while (True);
}
public void forever2( )
{
forever2( );
}
(Essay)
4.9/5
(32)
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
-If traverse is first called with traverse(0, 0); what will the first recursive call of traverse be?
(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 condition defines the base case for this method?
(Multiple Choice)
4.9/5
(28)
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(0) is called?
(Multiple Choice)
4.9/5
(35)
The following method correctly multiplies two ints so long as both are non-negative:
public int mpy(int a, int b)
{
return (b > 0) ?
a + mpy(a, b-1) : 0;
}
(True/False)
4.9/5
(38)
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;
}
-What is a fractal?
(Multiple Choice)
5.0/5
(40)
Showing 61 - 68 of 68
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)