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
The following method correctly adds two ints, returning their sum:
public int add(int a, int b)
{
return (b > 0) ?
add(a+1, b-1) : a;
}
(True/False)
4.8/5
(44)
For the questions below, recall the Towers of Hanoi recursive solution.
-The solution to the Towers of Hanoi has a(n) ________ complexity.
(Multiple Choice)
4.9/5
(42)
A recursive method without a base case leads to infinite recursion.
(True/False)
4.8/5
(43)
The following drawing is a line using the Koch snowflake design where order = 2. Show how it would appear with order = 3.


(Essay)
4.8/5
(41)
Which of the following recursive methods would execute approximately log ₂ n times for an initial parameter n?
(Multiple Choice)
4.7/5
(34)
Assume a function g(x) is defined as follows where x is an int parameter:
g(x) = g(x - 1) * g (x - 3) if x is even and x > 3
= g(x - 2) if x is odd and x > 3
= x otherwise
Write a recursive method to compute g.
(Essay)
4.9/5
(37)
Each time the order of a Koch fractal increases by one, the number of straight line segments
(Multiple Choice)
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;
}
-Aside from writing recursive methods, another way that recursion is often used is to define
(Multiple Choice)
4.8/5
(33)
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;
}
-If the method is called as patternRecognizer(x) where x is "aa", what will the result be?
(Multiple Choice)
4.9/5
(35)
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;
}
-If the statement a.substring(1, a.length( ) - 1) were changed to be (a.substring(1, a.length( )), then the method would
(Multiple Choice)
4.9/5
(38)
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;
}
-If the method is called as question1_2(8, 3), what is returned?
(Multiple Choice)
4.8/5
(30)
Traversing a maze is much easier to do iteratively than recursively.
(True/False)
4.9/5
(39)
Rewrite the following iterative method as a recursive method that computes the same thing. NOTE: your recursive method will require an extra parameter.
public int iterative1(int x)
{
int count = 0, factor = 2;
while (factor < x)
{
if (x % factor = = 0) count++;
factor++;
}
return count;
}
(Essay)
4.8/5
(27)
The Koch snowflake has an infinitely long perimeter, but it contains a finite area.
(True/False)
4.8/5
(38)
Consider the following recursive sum method:
public int sum(int x)
{
if (x = = 0) return 0;
else return sum(x - 1) + 1;
}
If the base case is replaced with "if (x = = 1) return 1;" the method will still compute the same thing.
(True/False)
4.8/5
(43)
Since iterative solutions often use loop variables and recursive solutions do not, the recursive solution is usually more memory efficient (uses less memory) than the equivalent iterative solution.
(True/False)
4.8/5
(47)
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;
}
-How many times is the factorial method invoked if originally called with factorial(5)? Include the original method call in your counting.
(Multiple Choice)
4.9/5
(36)
Assume page is a Graphics object. If the following method is called with drawIt(50, page), show what is displayed on the Graphics object page.
public void drawIt(int x, Graphics page)
{
if (x > 10)
{
page.setColor(Color.black);
page.drawRect(0, 0, x, x);
drawIt(x - 10, page);
}
}
(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 bar(a, 8);?
(Multiple Choice)
4.8/5
(39)
Showing 21 - 40 of 68
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)