Practice 16 - Recursion

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

which of the following problems cannot be programmed recursively? a. Towers of Hanoi b. summing a range of array elements c. finding the greatest common divisor d. All of the above can be programmed recursively

D.) 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

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

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

recursive algorithms are usually less efficient than iterative algorithms

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 actions that the JVM must perform any time a method is called is called:

overhead

this type of method is a method that calls itself

recursive

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

the depth of recursion is:

the number of times that a method calls itself

what is the base case for the algorithm? 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

x < 8

what is the base case for the method? look at the following method: public static int test2(int x, int y){ if (x < y) return -5; else return(test 2(x - y, y + 5) + 6); }

x < y

what is the recursive case for the algorithm? 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

x >= 8

what is the recursive case for the method? look at the following method: public static int test2(int x, int y){ if (x < y) return -5; else return(test 2(x - y, y + 5) + 6); }

x >= y

what is the base case for the algorithm gcd? 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

y == 0

what is the depth of test2(18, 5)? look at the following method: public static int Test2(int x, int y){ if (x < y) return -5; else return(Test 2(x - y, y + 5) + 6); }

2

what value is returned for Test14(16)? 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

32

what is returned for test2(18, 5)? look at the following method: public static int Test2(int x, int y){ if (x < y) return -5; else return(Test 2(x - y, y + 5) + 6); }

7

the Towers of Hanoi is:

- a mathematical game - often used in computer science books - demonstrate the power of recursion

what is returned for test2(10 , 20)? look at the following method: public static int test2(int x, int y){ if (x < y) return -5; else return(test 2(x - y, y + 5) + 6); }

-5

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"); message(n+1); } }

0

what is the depth of Test14(7)? 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

0

what is returned from gcd(60, 24)? 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

12

what is the value returned for Test14(7)? 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

14

what is the depth of Test14(16)? 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

2

what is the recursive case for the algorithm? 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

a > b

like _________, a recursive method must have some way to control the number of times it repeats

a loop

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"); message(n + 1); } }

an infinite number of times

what is the base case for the algorithm? 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

both: a < b and: a == b


संबंधित स्टडी सेट्स

Chapter 1 Multiple Choice Questions

View Set

Biopsychology- Internal Regulation

View Set

Chapter 1 -The corporation: 1.2 Ownership versus control of corporations

View Set