Recursion

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

A ____ method is a method that calls itself. (a) Looping (b) Circular (c) Recursive (d) Reoccurring

(c) Recursive

The actions that the JVM must perform any time a method is called are known as: A) Overhead B) Method calls C) Housekeeping D) Stack depth

A) Overhead

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) a > b B) a == b C) a < b D) None of these

A) a > b

The depth of recursion is: A) the number of times that a method calls itself B) The order in which the method appears on the stack C) the value that will terminate the recursive calls D) the value returned from the last recursive call

A) 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? A) x < 8 B) 3 * Test14(x - 8) + 8 C) 2 * x D) x >= 8

A) 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? A) x < y B) -5 C) +6 D) Test2(x - y, y + 5) + 6

A) x < y

10) Recursive solutions are always more efficient than iterative solutions

Answer: False

True/False - A problem can be solved recursively if it can be broken down into successive smaller problems that are unique within the overall problem.

False

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)? A) 32 B) 8 C) 24 D) 16

32

2) Some problems can only be solved recursively.

Answer: False

5) Recursive solutions to problems should be used whenever possible.

Answer: False

6) In the Towers of Hanoi puzzle, it is legal to move a larger disk to a peg that already contains a smaller disk.

Answer: False

7) The Towers of Hanoi puzzle cannot be solved iteratively.

Answer: False

9) A program with infinite recursion will act similarly to a program with an infinite loop.

Answer: False

1) In Java, it is possible for a method to call itself.

Answer: True

3) All recursive methods must have a base case.

Answer: True

4) A method that calls a different method, which then calls the original calling method is a recursive method.

Answer: True

8) Determining whether or not there is a path through a maze has a recursive solution

Answer: True

8) Which of the following will result from infinite recursion in Java? a) The program will hang as though there is an infinite loop. b) The program will throw an ArrayOutOfBoundsException. c) The program will not compile. d) The program will run out of memory. e) none of the above.

d) The program will run out of memory.

15) In a recursive solution, _______________ is(are) always necessary. a) short, efficient code b) several variables c) numerous lines of code d) a base case e) none of the above

d) a base case

1) A method that calls itself is a __________________ method. a) invalid b) static c) final d) recursive e) public

d) recursive

6) In indirect recursion, how many method calls can occur between one call to a method and the next one that completes the indirect recursion a) 2 b) 3 c) 4 d) 5 e) There is no limit to the number of intervening calls between a method and its indirect recursive call.

e) There is no limit to the number of intervening calls between a method and its indirect recursive call.

Unlike a loop, a recursive method does not require code to stop it from repeating.True/False

False

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)? A) 2 B) 3 C) 0 D) 1

A) 2

Which of the following problems can be programmed recursively? A) All of these B) Quicksort C) Towers of Hanoi D) Binary search

A) All of these

Like ____, a recursive method must have some way to control the number of times it repeats. (a) A loop (b) Any method (c) A GUI method (d) A rumor

(a) A loop

Like a loop, a recursive method must have (a) A counter (b) Some way to control the number of times it repeats itself (c) A return statement (d) A predetermined number of times it will execute before terminating

(b) Some way to control the number of times it repeats itself

A recursive method is a method that (a) Uses four-letter words to tell you when you have an error (b) Keeps recurring in your program code (c) Calls itself (d) Is a method that has a loop in it

(c) Calls itself

Which of the following does the JVM not have to perform when a method is called? (a) Allocate memory for parameters (b) Store the address of the program location where to return after the method terminates (c) Terminate the calling program (d) Allocate memory for local variables

(c) Terminate the calling program

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 (a) The recursive case (b) The terminal case (c) The base case (d) The final case

(c) The base case

Which of the following cannot be programmed recursively? (a) Towers of Hanoi (b) Greatest Common Denominator (c) Binary Search (d) All of the above can be programmed recursively.

(d) All of the above can be programmed recursively

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); } } (a) 1 (b) 9 (c) 10 (d) An infinite number of times

(d) An infinite number of times

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)? A) 14 B) 0 C) 7 D) -5

A) 14

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? A) neither a < b nor a == b B) a < b C) Both a < b and a == b D) a == b

C) Both a < b and a == b

A problem can be solved recursively if it can be broken down into successive smaller problems that are identical to the overall problem.True/False

True

Indirect recursion occurs when a method calls another method that in turn calls the first method. True/False

True

True/False Any problem that can be solved recursively can also be solved iteratively.

True

True/False: If Method A calls Method B which in turn calls method A, it is called indirect recursion

True

True/False: Recusrive algorithms are usually less efficient than iterative algorithms

True

How many times will the following method call itself if the value 10 is passed as the argument? public static void message (int n){ if (n < 0) { System.out.println("Print this line.\"); } } A) 0 B) An infinite number of times C) 10 D) 9

a) 0

4) The _______________________ puzzle is a favorite of computer scientists because of its elegant recursive solution. a) Tower of Hanoi b) Sudoku c) Tetris d) Tic-Tac-Toe e) none of the above

a) Tower of Hanoi

11) The recursive solution of the Towers of Hanoi problem has _______________ complexity. a) exponential b) polynomial c) logarithmic d) low e) none of the above

a) exponential

5) In the Towers of Hanoi puzzle, there are ________________ pegs. a) 2 b) 3 c) 4 d) 5 e) The Towers of Hanoi puzzle can include any number of pegs

b) 3

9) How many base cases must a recursive method have? a) A recursive method does not have to have a base case. b) at least 1 c) more than 1 d) more than 2 e) more than 3

b) at least 1

10) All recursive programs ____________________________________ . a) are more difficult to read than iterative programs. b) can be implemented iteratively. c) are easier to read than iterative programs. d) are shorter than iterative programs. e) none of the above are true.

b) can be implemented iteratively.

14) A solution with exponential complexity is ____________________ . a) efficient b) inefficient c) easy to implement d) difficult to implement e) none of the above

b) inefficient

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)? A) 7 B) 0 C) 6 D) 1

B) 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 is the depth of Test14(16)? A) 3 B) 2 C) 1 D) 0

B) 2

This term is used for methods that directly call themselves. A) Native recursion B) Direct recursion C) Simple recursion D) Absolute recursion

B) Direct recursion

The part of a problem that is solved with recursion is known as the: A) terminal case B) recursive case C) final case D) absolute case

B) recursive case

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)? A) 24 B) 66 C) 12 D) 60

C) 12

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)? A) 1 B) -5 C) 7 D) 6

C) 7

Usually, a problem is reduced by making the value of one or more parameters ________ with each recursive call. A) larger B) negative C) smaller D) equal

C) smaller

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? A) x < y B) y > x C) y == 0 D) x == 0

C) y == 0

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)? A) 1 B) 6 C) 10 D) -5

D) -5

The Towers of Hanoi is: A) demonstrates the power of recursion B) often used in computer science textbooks C) a mathematical game D) All of these

D) All of these

This type of method is a method that calls itself. A) Reoccurring B) Circular C) Looping D) Recursive

D) Recursive

Recursion can be a powerful tool for solving: A) object-oriented problems B) basic problems C) binary problems D) repetitive problems

D) repetitive problems

Algorithm Test14(int x) if (x<6) Return (3*x) else Return (2*Test14(x-6)+6) End Test14 What is the base case for Test14? a) 3*x b) x<6 c) 2*Test14(x-6)+6 d) x>=6

b) x<6

If the base case in a recursive method is never reached: A) the method will call itself only once B) the result will always be off by one C) the method will never call itself D) the method will call itself indefinitely

D) the method will call itself indefinitely

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? A) x < 8 B) x != 8 C) 2 * x D) x >= 8

D) 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? A) -5 B) x! = y C) x < y D) x >= y

D) x >= y

13) There is(are) ____________ base case(s) in the recursive solution to finding a path through a maze. a) 0 b) 1 c) 2 d) 3 e) 4

c) 2

3) __________________ recursion results from the lack of a base case. a) Indirect b) Direct c) Infinite d) Spiral e) none of the above

c) Infinite

7) Which of the following statements is true? a) Recursion should be used in every program. b) Recursion should be avoided all the time. c) Solutions that are easily expressed recursively should be programmed recursively. d) Solutions that are easily expressed recursively should be programmed iteratively. e) None of the above.

c) Solutions that are easily expressed recursively should be programmed recursively.

2) ____________________ recursion occurs when a method calls itself, while _________________ recursion when a method calls another method that then calls the original method. a) upward, downward b) downward, upward c) direct, indirect d) indirect, direct e) none of the above

c) direct, indirect

12) Which of the following is a proper recursive definition of x raised to the y power? a) x^y = x*x^y-1 b)x^y = x*x^y-2 c)x^y = x*x^y-1 for y>1: x^y = x for y = 1 d) x^y = x*x^y-1 for all x e) none of the above

c)x^y = x*x^y-1 for y>1: x^y = x for y = 1


Kaugnay na mga set ng pag-aaral

RN Concept-Based Assessment Level 2 Online Practice B

View Set

AH3 CHAPTER 14, 25,26,27,28,29 (12,28,29,30,31,32)

View Set

Chapter 21: Numerical Differentiation

View Set

CompTia A+ Core 2 (220-1002) yesss

View Set

4-Stroke Cycle Engine Theory Terms Ch 5

View Set