hello quizlet
Home
Subjects
Expert solutions
Create
Study sets, textbooks, questions
Log in
Sign up
Upgrade to remove ads
Only $35.99/year
chapter 13 quiz
Flashcards
Learn
Test
Match
Flashcards
Learn
Test
Match
recursion
Terms in this set (20)
given the following code snippet:
public static int new calc(int n)
{
if (n < 0)
{
return -1;
}
else if (n < 10)
{
return n;
}
else
{
return (n % 10) + new calc(n / 10);
}
}
what value will be returned when this code is executed with a call to new calc(15)?
6
consider the get area method from the textbook shown below.
public int get area()
{
if (width <= 0) { return 0; } // line #1
else if (width == 1) { return 1; } // line #2
else
{
triangle smaller triangle = new triangle(width - 1); // line #3
int smaller area = smaller triangle.get area(); // line #4
return smaller area + 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.
a palindrome is a word or phrase that reads the same forward or backward. consider the methods palindrome and is pal shown below:
public boolean palindrome(string string)
{
return is pal(string, 0, string.length() - 1);
}
private boolean is pal(string string, int left, int right)
{
if (left >= right)
{
return true;
}
else if (string.char at(left) == string.char at(right))
{
return is pal(string, left + 1, right - 1);
}
else
{
return false;
}
}
the method is pal as shown here would be considered to be a ____ method.
recursive helper
complete the code for the my factorial recursive method shown below, which is intended to compute the factorial of the value passed to the method:
public int my factorial(int an integer)
{
if (an integer == 1)
{
return 1;
}
else
{
______________________
}
}
return (an integer * (my factorial(an integer - 1)));
consider the code for the my factorial recursive method shown below, which is intended to compute the factorial of the value passed to the method:
public int my factorial(int an integer)
{
if (an integer == 1) // line #1
{
return 1; // line #2
}
else
{
return (an integer * my factorial(an integer - 1)); // line #3
}
}
if you set a breakpoint on line #2 and then call my factorial(4), how many calls to my factorial will be visible on the debugger's call stack when the program suspends for the breakpoint?
4
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
the ________ is at least one case in which a problem can be solved without recursion.
base case
occurs when method a calls method b which in turn calls method a
indirect recursion
to use recursion for a binary search, what is required before the search can proceed?
the array must be sorted
a method is called form the main method for the first time. it then calls itself seven times. what is the depth of recursion?
7
a method that calls itself is a
recursive method
when recursive methods directly call themselves, it is known as
direct recursion
which of the following problems can be solved recursively?
greatest common denominator, towers of hanoi, and binary search
in the ________, the problem must be reduced to a smaller version of the original problem
recursive case
a recursive method must have which of the following?
some way to control the number of time it repeats
in the following code that uses recursion to find the factorial of a number, what is the base case?
private static int factorial (int n)
{
if (n == 0)
return 1;
else
return n * factorial (n-1);
}
if (n == 0)
return 1;
in a recursive program, the number of times a method calls itself is known as the
depth of recursion
in the following code that uses recursion to find the greatest common divisor of a number, what is the base case?
public static int god(int x, int y)
{
if (x % y == 0)
return y;
else
return god(y, x % y);
}
if (x % y == 0)
return y;
given the following code that uses recursion to find the factorial of a number, how many times will the else clause be executed if n = 5?
private static int factorial (int n)
{
if (n == 0)
return 1;
else
return n * factorial(n -1);
}
5
the actions performed by the jvm that take place with each method call are sometimes referred to as
overhead
Students also viewed
BJU Science 6 Chapter 2, BJU Science 6 C…
113 terms
Science 6th Grade Mrs. Registe
18 terms
Los Saludos/The Greetings
48 terms
8b vocabulary
64 terms
Sets found in the same folder
chapter 8 quiz
30 terms
chapter 9 quiz
18 terms
chapter 12 quiz
18 terms
chapter 10 quiz
16 terms
Other sets by this creator
introduction to biodiversity test 3
72 terms
chapter 2 & 3 study guide for esc 1100
82 terms
introduction to operating systems test 1
38 terms
assembly quiz
18 terms
Recommended textbook solutions
Numerical Analysis
9th Edition
J. Douglas Faires, Richard L. Burden
873 solutions
Elementary Differential Geometry
2nd Edition
Barrett O'Neill
297 solutions
Topology
2nd Edition
James Munkres
622 solutions
Numerical Analysis
9th Edition
J. Douglas Faires, Richard L. Burden
873 solutions
Other Quizlet sets
Government Test 4
205 terms
Final exam - cog psy
124 terms
biochem quizzes
58 terms
Money and Banking Unit 2
72 terms