Java II Chapter 16
How many times will the following method call itself, if 10 is passed as the argument? public static void message(int n) { if (n < 0) { System.out.println("Print this line.\n"); message(n + 1); } }
0
The actions that the JVM must perform any time a method is called is called
overhead
The Towers of Hanoi is
All of the above
Look at the following pseudocode algorithm. Algorithm Test3(int a, int b) if (a < b) return 5 else if ( a == b) return -5; else return (a + Test3(a - 1, b) end Test3 What is the base case for the algorithm?
Both a and b
The part of a problem that is solved with recursion is known as the
recursive case
Like a loop, a recursive method must have
some way to control the number of times it repeats itself
To solve a program recursively, you need to identify at least one case in which the problem can be solved without recursion - this is known as
the base case
Which of the following problems cannot be programmed recursively?
All of the above can be programmed recursively
A problem can be solved recursively if it can be broken down into successive smaller problems that are unique within the overall problem.
False
This type of method is a method that calls itself
Recursive
A problem can be solved recursively if it can be broken down into successive smaller problems that are identical to the overall problem
True
Any problem that can be solved recursively can also be solved iteratively
True
A recursive method is a method that
calls itself
The number of times that a method calls itself is known as the
depth of recursion
The depth of recursion is
the number of times that a method calls itself
Look at the following pseudocode algorithm. algorithm Test14(int x) if (x < 8) return (2 * x) else return (3 * Test14(x - 8) + 8) end Test14 What is the base case for the algorithm?
x < 8
Look at the following method. public static int Test2(int x, int y) { if ( x < y) { return -5; } else { return (Test2(x - y, y + 5) + 6); } } What is the base case for the method?
x < y
Look at the following pseudocode algorithm. algorithm Test14(int x) if (x < 8) return (2 * x) else return (3 * Test14(x - 8) + 8) end Test14 What is the recursive case for the algorithm?
x >= 8
Look at the following method. public static int test2(int x, int y) { if ( x < y) { return -5; } else { return (test2(x - y, y + 5) + 6); } } What is the recursive case for the method?
x >= y
Look at the following pseudocode algorithm. Algorithm gcd(x, y) if (x < y) gcd (y, x) else if (y = 0) return x else return gcd(y, x mod y) end gcd What is the base case for the algorithm gcd?
y == 0
Look at the following pseudocode algorithm. algorithm Test14(int x) if (x < 8) return (2 * x) else return (3 * Test14(x - 8) + 8) end Test14 What value is returned for Test14(16)?
32
Look at the following method. public static int test2(int x, int y) { if ( x < y) { return -5; } else { return (test2(x - y, y + 5) + 6); } } What is returned for test2(18,5)?
7
Look at the following method. public static int test2(int x, int y) { if ( x < y) { return -5; } else { return (test2(x - y, y + 5) + 6); } } What is returned for test2(10, 20)?
-5
Look at the following pseudocode algorithm. algorithm Test14(int x) if (x < 8) return (2 * x) else return (3 * Test14(x - 8) + 8) end Test14 What is the depth of Test14(7)?
0
Look at the following pseudocode algorithm. Algorithm gcd(x, y) if (x < y) gcd (y, x) else if (y = 0) return x else return gcd(y, x mod y) end gcd What is returned from gcd(60, 24)?
12
Look at the following pseudocode algorithm. algorithm Test14(int x) if (x < 8) return (2 * x) else return (3 * Test14(x - 8) + 8) end Test14 What value is returned for Test14(7)?
14
Look at the following method. public static int test2(int x, int y) { if ( x < y) { return -5; } else { return (test2(x - y, y + 5) + 6); } } What is the depth of test2(18,5)?
2
Look at the following pseudocode algorithm. algorithm Test14(int x) if (x < 8) return (2 * x) else return (3 * Test14(x - 8) + 8) end Test14 What is the depth of Test14(16)?
2
How many times will the following method call itself, if 10 is passed as the argument? public static void message(int n) { if (n > 0) { System.out.println("Print this line.\n"); message(n + 1); }
An infinite number of times
If Method A calls Method B which in turn calls Method A, it is called indirect recursion
True
Indirect recursion occurs when a method calls another method that in turn calls the first method
True
Look at the following pseudocode algorithm. Algorithm Test3(int a, int b) if (a < b) return 5 else if ( a == b) return -5; else return (a + Test3(a - 1, b) end Test3 What is the recursive case for the algorithm?
a > b
Like _________, a recursive method must have some way to control the number of times it repeats
a loop