1/11
Week 6 Topics for MATH 140
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Any problem that can be solved recursively can also be solved iteravely, with a loop
True or False
True
Without a base case, a recursive method will call itself only once and stop
True or False
False
A recursive method can only have one base case
True or False
False
Recursion is never absolutely required to solve a problem
True or False
True
A method that calls itself is a _________ method
recursive
redundant
binary
derived
recursive
The _________ is at least one case in which a problem can be solved without recursion
recursive case
base case
termination point
point of absolution
base case
In the following cose 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);
}factorial (int n)if (n == 0)
return 1;else
return n * factorial (n - 1);Cannot tell from this code
if (n == 0)
return 1;In the following code that uses recursion to find the greatest common divisor of a number, what is the base case?
public static int gcd (int x, int y)
{
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}gcd(int x, int y)if (x % y == 0)
return y;else
return gcd(y, x % y);Cannot tell from this code
if (x % y == 0)
return y;Criticize the following recursive function:
public static String exR2(int n)
{
String s = exR2(n-3) + n + exR2(n-2) + n;
if (n <= 0)
return "";
return s;
}This code doesn't have any problems.
In line String s = exR2(n-3) + n + exR2(n-2) + n; we can't call the same method twice.
return ""; is error
Base case should be the first statement.
Base case should be the first statement.
Find the error in the following program:
public class FindTheError
{
public static void main(String[] args)
{
myMethod(0);
}
public static void myMethod(int num)
{
System.out.print(num + " ");
myMethod(num + 1);
}
}
There is no error in this code.
There is no base case for myMethod.
myMethod can't be a static method.
myMethod should return an integer.
There is no base case for myMethod.
Consider the following code;
What is the value of mystery(2,14)?
public static int mystery(int a, int b)
{
if (b == 0) return 0;
if (b%2 == 0) return mystery(a+a,b/2);
return mystery(a+a,b/2)+a;
}7
16
12
28
28
What operation does the following code implement?
public static int mystery(int a, int b)
{
if (b == 0) return 0;
if (b%2 == 0) return mystery(a+a,b/2);
return mystery(a+a,b/2)+a;
}Addition
Subtraction
Multiplication
Division
Multiplication