Carrano Chapter 3

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

The midpoint of a sorted array can be found by ______, where first is the index of the first item in the array and last is the index of the last item in the array. first / 2 + last / 2 first / 2 - last / 2 (first + last) / 2 (first - last) / 2

(first + last) / 2

For anArray = <2, 3, 5, 6, 9, 13, 16, 19>, what is the value returned by a recursive binary search algorithm if the value being searched for is 10? -1 0 1 10

-1

A recursive solution that finds the factorial of n always reduces the problem size by ______ at each recursive call. 1 2 half one-third

1

The factorial of zero is ______. -1 0 1 2

1

How many bases cases will be required in a recursive solution that solves a problem by solving two smaller problems of the same type? 0 1 2 3

2

For anArray = <2, 3, 5, 6, 9, 13, 16, 19>, what is the value returned by a recursive binary search algorithm if the value being searched for is 6? 1 3 4 6

3

In the Fibonacci sequence, which of the following integers comes after the sequence 1, 1, 2, 3? 3 4 5 6

5

What is a base case?

A base case is a special case of a recursive solution that terminates the recursive processing

Why do some compilers automatically replace tail recursion with iteration?

Some compilers automatically replace tail recursion with iteration because tail-recursive methods are often less efficient than their iterative counterparts and the conversion of a tail-recursive method to an equivalent iterative method is rather mechanical.

What is the base case for the recursive solution to the Towers of Hanoi problem?

The base case for the recursive solution to the Towers of Hanoi problem is a recursive call that has only one disk to move.

What is the box trace?

The box trace is a systematic way to trace the actions of a recursive method.

Why does the Fibonacci sequence have two base cases? In other words, would it be possible to write a correct recursive Fibonacci method that has only one base case?

We need two base cases, because if we don't, by the time we get down to the case where n = 2, fib(2) is defined in terms of fib(1) and fib(0), and fib(0) is undefined. Since fib(0) is undefined its recursion will continue down forever.

The factorial of n is equal to ______. n - 1 n - factorial (n-1) factorial (n-1) n * factorial (n-1)

n * factorial (n-1)

A recursive method that computes the number of groups of k out of n things has the precondition that ______. n is a positive number and k is a nonnegative number n is a nonnegative number and k is a positive number n and k are nonnegative numbers n and k are positive numbers

n and k are nonnegative number

Which of the following is a precondition for a method that accepts a number n and computes the nth Fibonacci number? n is a negative integer n is a positive integer n is greater than 1 n is an even integer

n is a positive integer

An array is a(n) ______. class method object variable

object

Write a recursive method that takes 3 parameters: an integer array a, and two integers first and last. The method will find the largest value in a between indices first and last, inclusive. That is, it will return the largest value in the part of the array a[first..last] . You may assume that first ≤ last.

public static int findMax(int [] a, int first, int last) { if (first == last) return a[first]; int firstValue = a[first]; int maxOfRest = findMax(a, first + 1, last); if (firstValue > maxOfRest) return firstValue; else return maxOfRest; }

Write a recursive method that takes a String parameter and prints out the characters of the string in reverse order.

public static void reverse(String s) { if (s.length() == 0) return; char lastChar = s.charAt(s.length() - 1); System.out.print(lastChar); reverse(s.substring(0, s.length() - 1)); }

A ______ is a mathematical formula that generates the terms in a sequence from previous terms. local environment pivot item base case recurrence relation

recurrence relation

A class method is defined as ______. static abstract private protected

static

If the value being searched for by a recursive binary search algorithm is in the array, which of the following is true? the algorithm cannot return a nonpositive number the algorithm cannot return a nonnegative number the algorithm cannot return a zero the algorithm cannot return a negative number

the algorithm cannot return a negative number

In the box trace for a recursive method, a new box is created each time ______. the method is called the method returns a value the object is created the object is initialized

the method is called

In the box trace, each box contains all of the following EXCEPT ______. the values of the references and primitive types of the method's arguments the method's local variables the method's class variables a placeholder for the value returned by each recursive call from the current box the value of the method itself

the method's class variables

What is a pivot item?

A pivot item is an element that an algorithm uses to organize data by value.

What is a recurrence relation?

A recurrence relation is a mathematical formula that generates the terms in a sequence from previous terms.

How does a sequential search work?

A sequential search starts at the beginning of the collection and looks at every item in the collection in order until the item being searched for is found.

What is a tail-recursive method?

A tail-recursive method is a method where the solitary recursive call in the method is the last action that the method takes.

What is an activation record?

An activation record is a record that contains a method's local environment at the time of and as a result of the call to the method.

A recursive solution that finds the factorial of n generates ______ recursive calls. n-1 n n+1 n*2

n

What are the four questions that must be considered when constructing a recursive solution?

How can you define the problem in terms of a smaller problem of the same type? How does each recursive call diminish the size of the problem? What instance of the problem can serve as the base case? As the problem size diminishes, will you reach this base case?

Suppose a program contains a recursive method findFibonacci(int n), which computes the n-th Fibonacci number. Even if we know that a client method will never call findFibonacci( ) with the values 1 or 2 as arguments, why does the implementation of findFibonacci( ) still need to have base cases?

In the course of findFibonacci's own recursion it will eventually reach values of n = 1 or 2 that it passes to itself. If these are not identified as base cases, then the recursion will logically continue forever, which is incorrect.

Which of the following is NOT a precondition for an array that is to be sorted by a recursive binary search algorithm? (first is the index of the first item in the array, last is the index of the last item in the array, and SIZE is the maximum size of the array) SIZE <= first 0 <= first last <= SIZE - 1 anArray[first] <= anArray[first + 1] <= ... <= anArray[last]

SIZE <= first

What elements are included in a method's local environment?

The local environment of a method includes the method's local variables, a copy of the actual value arguments, a return address in the calling routine, and the value of the method itself.

What are the two base cases for a recursive binary search algorithm?

The two base cases are: first > last value == anArray[mid] (where first is the index of the first item in the array, last is the index of the last item in the array, and mid is the midpoint of the array).

What are the two factors which contribute to the inefficiency of some recursive solutions?

The two factors are the overhead associated with method calls, and the inherent inefficiency of some recursive algorithms.

When is the base case value == anArray[mid] (where mid is the midpoint of the array) reached in a recursive binary search algorithm?

This base case is reached when the value being searched for is in the original array

When is the base case first > last (where first is the index of the first item in the array and last is the index of the last item in the array) reached in a recursive binary search algorithm?

This base case is reached when the value being searched for is not in the original array.

In a recursive method that writes a string of characters in reverse order, the base case is ______. a string with a length of 0 a string whose length is a negative number a string with a length of 3 a string that is a palindrome

a string with a length of 0

In the box trace, each box roughly corresponds to a(n) ______. recursive relation activation record base case pivot item

activation record

What would happen if a negative value is passed to a method that returns the factorial of the value passed to it? the method will calculate the correct value for the factorial of the number the method will calculate a negative value for the factorial of the number the method would terminate immediately an infinite sequence of recursive calls will occur

an infinite sequence of recursive calls will occur

In a sorted array, the kth smallest item is given by ______. anArray[k-1] anArray[k] anArray[SIZE-k] anArray[SIZE+k]

anArray[k-1]

In the recursive solution to the kth smallest item problem, the problem size decreases by ______ at each recursive call. 1 at least 1 half at least half

at least 1

In a recursive solution, the ______ terminates the recursive processing. local environment pivot item base case recurrence relation

base case

When you solve a problem by solving two or more smaller problems, each of the smaller problems must be ______ the base case than the original problem. closer to farther to either closer to or the same "distance" from either farther to or the same "distance" from

closer to

In the recursive solution to the Towers of Hanoi problem, the number of disks to move ______ at each recursive call. decreases by 1 increases by 1 decreases by half increases by half

decreases by 1

The base case for a recursive definition of the factorial of n is ______. factorial (-1) factorial (0) factorial (1) factorial (n - 1)

factorial (0)

A binary search starts at the beginning of the collection of items.

false

A method that is declared as static is an object method.

false

An iterative method always calls itself.

false

The binary search algorithm can be applied to an unsorted array.

false

Which of the following is a base case for a recursive binary search algorithm? (first is the index of the first item in the array, last is the index of the last item in the array, and mid is the midpoint of the array). last > first first > last 0 <= first last <= SIZE-1

first > last

A recursive binary search algorithm always reduces the problem size by ______ at each recursive call. 1 2 half one-third

half

Suppose sa is a sorted array of integer values, and we wish to search for a number target in this array. If sa contains n elements, and we use the binary search strategy, what is the approximate maximum number of comparisons necessary to determine that the value target is or is not in the array? How would your answer change if the array were not sorted?

log2 n. But for an unsorted array, the answer would be n.

The number of ways to choose k out of n things is ______. the number of ways to choose k - 1 out of n - 1 things the number of ways to choose k out of n - 1 things the sum of the number of ways to choose k - 1 out of n - 1 things and the number of ways to choose k out of n - 1 things the product of the number of ways to choose k - 1 out of n - 1 things and the number of ways to choose k out of n - 1 things

the sum of the number of ways to choose k - 1 out of n - 1 things and the number of ways to choose k out of n - 1 things

A recursive solution can have more than one base cases.

true

A recursive solution solves a problem by solving a smaller instance of the same problem.

true

An iterative solution involves loops.

true

Every recursive method must have a base case.

true

The base case for a recursive solution to the kth smallest item problem cannot be predicted in advance.

true

When constructing a recursive solution, you should assume that a recursive call's postcondition is true if its precondition is true.

true


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

solving linear equations and inequalities (100%)

View Set

Government: Unit 4: Civil Liberties (Chapter 19)

View Set