Java Early Objects Chp. 18

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

Which of the following statements about recursion are true? a. Recursion can occur infinitely. b. Recursion uses a termination test. c. Both a and b. d. Neither a nor b.

Both a and b.

True or False: Every recursive definition can have zero or more base cases.

False Why: Every recursive definition must have a base case in order for the recursive method to resolve itself.

True or False: Recursive algorithms are implemented using while loops.

False Why: Recursive algorithms are not implemented using loops, rather they are implemented through repeated method calls. The method calls itself, which is what is known as recursion, until it reaches a base case.

True or False: In the base case of a recursive solution, the solution is obtained through a call to a smaller version of the original method.

False Why: The base case by definition is the method call that results in an actual return value which then allows all other calls to the method to resolve. When the method calls to a smaller version of the original method, that is NOT known as the base case. That is known as the recursive step.

True or False: A method that calls itself is an iterative method.

False. Why: A method that calls itself is a recursive method.

True or False: A general case to a recursive algorithm must eventually reduce to a base case.

True Why: If it does not eventually reduce to a base case, it will recur infinitely, resulting in a stack overflow. It must reach a base case in order for the multiple calls to be resolved and reach a final return value, resulting in all the methods finally being implemented and control being returned to the original method call.

True or False: Every recursive call has its own code.

True Why: It contains its own code because it still executes itself up to the point of the recursion.

True or False: The body of a recursive method contains a statement that causes the same method to execute before completing the current call.

True Why: That is the definition of a recursive method; it is a method that calls itself (either directly or indirectly through another method).

True or False: You can think of a recursive method as having unlimited copies of itself.

True Why: The recursion creates multiple copies of the method that go into a stack.

Consider the following definition of a recursive method. public static int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1);} What is the output of the following statement? System.out.println(recFunc(10)); a. 10 b. 110 c. This statement results in infinite recursion. d. None of these

a. 10

Consider the following definition of a recursive method. public static int foo(int n) //Line 1 { //Line 2 if (n == 0) //Line 3 return 0; //Line 4 else //Line 5 return n + foo(n - 1); //Line 6 } Which of the statements represent the base case? a. Statements in Lines 3 and 4 b. Statements in Lines 5 and 6 c. Statements in Lines 3, 4, and 5 d. of these

a. Statements in Lines 3 and 4

Which of the following is false? a. Since BigInteger is not a primitive type, we can't use the arithmetic, relational and equality operators with BigIntegers. b. BigInteger method compareTo compares the BigInteger number that calls the method to the method's BigInteger argument, and returns -1 if the BigInteger that calls the method is less than the argument, 0 if they're equal or 1 if the BigInteger that calls the method is greater than the argument. c. The value 1 can be implicitly converted to a BigInteger. d. BigInteger can represent integer values larger than what primitive type long can represent.

a. The value 1 can be implicitly converted to a BigInteger.

Like ________, a recursive method must have some way to control the number of times it repeats. a. a loop b. any method c. a class constructor d. an event

a. a loop

When a recursive method is called to solve a problem, the method actually is capable of solving only the simplest case(s), or ________. a. base case(s). b. base step(s). c. recursive call(s). d. recursion step(s).

a. base case(s).

Recursion is often less efficient than iteration because ________. a. it can cause an explosion of method calls. b. it is not as intuitive. c. recursive methods are harder to debug. d. recursive methods take longer to program.

a. it can cause an explosion of method calls.

What is the limiting condition of the code in the code below? public static int func2(int m, int n) { if (n == 0) return 0; else return m + func2(m, n - 1); } a. n >= 0 b. m > n c. m >= 0 d. n > m

a. n >= 0

Fractals that yield an exact copy of the original when a portion of the original image is magnified are called __________ fractals. a. strictly self-similar. b. Koch Curve. c. similar. d. mirror.

a. strictly self-similar.

All of the following are true for both recursion and iteration except ________. a. they have a base case. b. they can cause infinite loops or infinite recursion. c. they are based on a control statement. d. both gradually approach termination.

a. they have a base case.

The current method executing is always the method whose activation record is ________. a. at the bottom of the stack. b. at the top of the stack. c. never placed on the stack. d. second from the top of the stack, just below the previous method call.

b. at the top of the stack.

The recursion step should: a. check for the base case. b. call a fresh copy of the recursive method to work on a smaller problem. c. make two calls to the recursive method. d. iterate until it reaches a termination condition.

b. call a fresh copy of the recursive method to work on a smaller problem.

The operands of an operator are evaluated ________. a. from right to left. b. from left to right. c. at the same time. d. in an order that is specific to each operator.

b. from left to right.

Which of the following statements about the code below is always true? public static int func2(int m, int n) { if (n == 0) return 0; else return m + func2(m, n - 1); } a. func2(m, n) = func2(n, m) for m >= 0 b. func2(m, n) = m * n for n >= 0 c. func2(m, n) = m + n for n >= 0 d. func2(m, n) = n * m for m >= 0

b. func2(m, n) = m * n for n >= 0

The number of calls to recursively calculate the Fibonacci value of 7 is: a. 7 b. 13 c. 41 d. 39

c. 41

Consider the following definition of a recursive method. public static int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } What is the output of the following statement? System.out.println(recFunc(8)); a. 8 b. 72 c. 720 d. None of these

c. 720

Which of the following statements is NOT true about infinite recursion? a. In theory, infinite recursion executes forever. b. In reality, infinite recursion will lead to a computer running out of memory and abnormal termination of the program. c. Methods that are indirectly recursive always lead to infinite recursion. d. If recursive calls never lead to a base case, infinite recursion occurs.

c. Methods that are indirectly recursive always lead to infinite recursion.

Recursion often is preferable to iteration because ________. a. it is faster. b. it requires less memory. c. it models the problem more logically. d. All of the above.

c. it models the problem more logically.

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 call itself indefinitely d. the method will never call itself

c. the method will call itself indefinitely

When the recursion step executes: a. this is known as indirect recursion. b. all of the computer's processes halt until the recursion step has completed executing. c. the original call to the method is still active. d. All of the above.

c. the original call to the method is still active.

Each time a fractal's pattern is applied to it, the fractal is said to be at a new ________. a. level. b. depth. c. order. d. All of the above.

d. All of the above.

Which of the following statements describe the base case of a recursive algorithm? (i) F(0) = 0; (ii) F(x) = 2 * F(x - 1); (iii) if (x == 0) F(x) = 5 + x; a. Only (i) b. Only (ii) c. Only (iii) d. Both (i) and (iii)

d. Both (i) and (iii)


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

Google Workspace: Deployment Services Specialist Exam

View Set

Computer Security Fundamentals, Ch1-9

View Set

Organizational Behavior - Chapter 12

View Set

Care of Patients with Musculoskeletal Trauma

View Set

Application of Organizational Ethics and Organizational Change and trandforming

View Set

APUSH: Chapter 12 "An Age Of Reform, 1820-1840"

View Set

2.4 Three types of chemical bonds are ionic, covalent, and hydrogen

View Set

NRSG 102- Water and Electrolytes

View Set

Business Analytics Midterm (Concepts)

View Set