Deck 13: Recursion
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/110
Play
Full screen (f)
Deck 13: Recursion
1
Consider the following code snippet for calculating Fibonacci numbers recursively:
Int fib(int n)
{
// assumes n >= 0
If (n <= 1)
{
Return n;
}
Else
{
Return (fib(n - 1) + fib(n - 2));
}
}
Identify the terminating condition.
A) n < 1
B) n <= 1
C) fib(n - 1)
D) fib(n - 1) + fib(n - 1)
Int fib(int n)
{
// assumes n >= 0
If (n <= 1)
{
Return n;
}
Else
{
Return (fib(n - 1) + fib(n - 2));
}
}
Identify the terminating condition.
A) n < 1
B) n <= 1
C) fib(n - 1)
D) fib(n - 1) + fib(n - 1)
B
2
Complete the code for the recursive method printSum shown in this code snippet, which is intended to return the sum of digits from 1 to n: public static int printSum(int n)
{
If (n == 0)
{
Return 0;
}
Else
{
______________________________
}
}
A) return (printSum(n - 1));
B) return (n + printSum(n + 1));
C) return (n + printSum(n - 1));
D) return (n - printSum(n - 1));
{
If (n == 0)
{
Return 0;
}
Else
{
______________________________
}
}
A) return (printSum(n - 1));
B) return (n + printSum(n + 1));
C) return (n + printSum(n - 1));
D) return (n - printSum(n - 1));
C
3
Consider the recursive method myPrint shown in this code snippet: public void myPrint(int n)
{
If (n < 10)
{
System.out.print(n);
}
Else
{
Int m = n % 10;
System.out.print(m);
MyPrint(n / 10);
}
}
What does this method do?
A) Prints a positive int value forward, digit by digit.
B) Prints a positive int value backward, digit by digit.
C) Divides the int by 10 and prints out its last digit.
D) Divides the int by 10 and prints out the result.
{
If (n < 10)
{
System.out.print(n);
}
Else
{
Int m = n % 10;
System.out.print(m);
MyPrint(n / 10);
}
}
What does this method do?
A) Prints a positive int value forward, digit by digit.
B) Prints a positive int value backward, digit by digit.
C) Divides the int by 10 and prints out its last digit.
D) Divides the int by 10 and prints out the result.
B
4
Consider the recursive method shown below: public static int strangeCalc(int bottom, int top)
{
If (bottom > top)
{
Return -1;
}
Else if (bottom == top)
{
Return 1;
}
Else
{
Return bottom * strangeCalc(bottom + 1, top);
}
}
What value will be returned with a call to strangeCalc(4,7)?
A) 1
B) -1
C) 120
D) 840
{
If (bottom > top)
{
Return -1;
}
Else if (bottom == top)
{
Return 1;
}
Else
{
Return bottom * strangeCalc(bottom + 1, top);
}
}
What value will be returned with a call to strangeCalc(4,7)?
A) 1
B) -1
C) 120
D) 840
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
5
Consider the getArea method from the textbook shown below: public int getArea()
{
If (width <= 0) { return 0; } // line #1
Else if (width == 1) { return 1; } // line #2
Else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
}
Assume the code in line #3 is changed to:
Triangle smallerTriangle = new Triangle(width);
This change would cause infinite recursion for which triangles?
A) Those with width equal to 0.
B) Those with width equal to 1.
C) Those with width greater than or equal to 2.
D) Triangles of any width.
{
If (width <= 0) { return 0; } // line #1
Else if (width == 1) { return 1; } // line #2
Else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
}
Assume the code in line #3 is changed to:
Triangle smallerTriangle = new Triangle(width);
This change would cause infinite recursion for which triangles?
A) Those with width equal to 0.
B) Those with width equal to 1.
C) Those with width greater than or equal to 2.
D) Triangles of any width.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
6
Consider the getArea method from the textbook shown below. public int getArea()
{
If (width <= 0) { return 0; } // line #1
Else if (width == 1) { return 1; } // line #2
Else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
}
Where is/are the recursive call(s)?
A) line #1
B) line #2
C) lines #1 and #2
D) line #4
{
If (width <= 0) { return 0; } // line #1
Else if (width == 1) { return 1; } // line #2
Else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
}
Where is/are the recursive call(s)?
A) line #1
B) line #2
C) lines #1 and #2
D) line #4
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
7
Consider the recursive method myPrint in this code snippet: public void myPrint(int n)
{
If (n < 10)
{
System.out.print(n);
}
Else
{
Int m = n % 10;
System.out.print(m);
MyPrint(n / 10);
}
}
What is printed for the call myPrint(821)?
A) 821
B) 128
C) 12
D) 10
{
If (n < 10)
{
System.out.print(n);
}
Else
{
Int m = n % 10;
System.out.print(m);
MyPrint(n / 10);
}
}
What is printed for the call myPrint(821)?
A) 821
B) 128
C) 12
D) 10
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
8
Consider the getArea method from the textbook shown below. public int getArea()
{
If (width <= 0) { return 0; } // line #1
Else if (width == 1) { return 1; } // line #2
Else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
}
Where is/are the terminating condition(s)?
A) line #1
B) line #2
C) lines #1 and #2
D) line #4
{
If (width <= 0) { return 0; } // line #1
Else if (width == 1) { return 1; } // line #2
Else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
}
Where is/are the terminating condition(s)?
A) line #1
B) line #2
C) lines #1 and #2
D) line #4
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
9
Consider the getArea method from the book shown below. public int getArea()
{
If (width <= 0) { return 0; } // line #1
Else if (width == 1) { return 1; } // line #2
Else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
}
Assume lines #1 and #2 were changed to this:
If (width == 1) { return 1; } // new line #1-2
What will happen when this code is executed?
A) A negative or zero width would cause problems.
B) Nothing - the method would still be correct.
C) We would lose our only recursive case.
D) A positive width would reduce the correct area by 1.
{
If (width <= 0) { return 0; } // line #1
Else if (width == 1) { return 1; } // line #2
Else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
}
Assume lines #1 and #2 were changed to this:
If (width == 1) { return 1; } // new line #1-2
What will happen when this code is executed?
A) A negative or zero width would cause problems.
B) Nothing - the method would still be correct.
C) We would lose our only recursive case.
D) A positive width would reduce the correct area by 1.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
10
Consider the getArea method from the textbook shown below. public int getArea()
{
If (width <= 0) { return 0; } // line #1
Else if (width == 1) { return 1; } // line #2
Else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
}
Assume line #1 is replaced with this line:
If (width <= 0) {return width;}
What will be the result?
A) The method will still return correct results for all non-negative width triangles.
B) The method will return incorrect results for triangles with width equal to 0.
C) The method will return incorrect results for triangles with width equal to 1.
D) The method will return area to be one too high for all triangles.
{
If (width <= 0) { return 0; } // line #1
Else if (width == 1) { return 1; } // line #2
Else
{
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
}
Assume line #1 is replaced with this line:
If (width <= 0) {return width;}
What will be the result?
A) The method will still return correct results for all non-negative width triangles.
B) The method will return incorrect results for triangles with width equal to 0.
C) The method will return incorrect results for triangles with width equal to 1.
D) The method will return area to be one too high for all triangles.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
11
Consider the following recursive code snippet: public static int mystery(int n, int m)
{
If (n <= 0)
{
Return 0;
}
If (n == 1)
{
Return m;
}
Return m + mystery(n - 1, m);
}
Identify the terminating condition(s) of method mystery?
A) n <= 0
B) n == 1
C) n <= 0 or n == 1
D) n > 0
{
If (n <= 0)
{
Return 0;
}
If (n == 1)
{
Return m;
}
Return m + mystery(n - 1, m);
}
Identify the terminating condition(s) of method mystery?
A) n <= 0
B) n == 1
C) n <= 0 or n == 1
D) n > 0
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
12
Consider the recursive method shown below: public static int strangeCalc(int bottom, int top)
{
If (bottom > top)
{
Return -1;
}
Else if (bottom == top)
{
Return 1;
}
Else
{
Return bottom * strangeCalc(bottom + 1, top);
}
}
What value will be returned with a call to strangeCalc(2,3)?
A) 1
B) 2
C) 6
D) 24
{
If (bottom > top)
{
Return -1;
}
Else if (bottom == top)
{
Return 1;
}
Else
{
Return bottom * strangeCalc(bottom + 1, top);
}
}
What value will be returned with a call to strangeCalc(2,3)?
A) 1
B) 2
C) 6
D) 24
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
13
Consider the code for the recursive method myPrint shown in this code snippet: public static int myPrint(int n)
{
If (n == 0)
{
Return 0;
{
Else
{
Return (n + myPrint(n - 1));
}
}
To avoid infinite recursion, which of the following lines of code should replace the current terminating case?
A) if (n == -1)
B) if (n <= 0)
C) if (n >= 0)
D) The terminating case as shown will avoid infinite recursion.
{
If (n == 0)
{
Return 0;
{
Else
{
Return (n + myPrint(n - 1));
}
}
To avoid infinite recursion, which of the following lines of code should replace the current terminating case?
A) if (n == -1)
B) if (n <= 0)
C) if (n >= 0)
D) The terminating case as shown will avoid infinite recursion.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
14
Consider the following recursive code snippet: public int mystery(int n, int m)
{
If (n == 0)
{
Return 0;
}
If (n == 1)
{
Return m;
}
Return m + mystery(n - 1, m);
}
What value is returned from a call to mystery(1,5)?
A) 1
B) 5
C) 6
D) 11
{
If (n == 0)
{
Return 0;
}
If (n == 1)
{
Return m;
}
Return m + mystery(n - 1, m);
}
What value is returned from a call to mystery(1,5)?
A) 1
B) 5
C) 6
D) 11
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
15
Consider the following recursive code snippet: public int mystery(int n, int m)
{
If (n == 0)
{
Return 0;
}
If (n == 1)
{
Return m;
}
Return m + mystery(n - 1, m);
}
What value is returned from a call to mystery(3,6)?
A) 3
B) 6
C) 18
D) 729
{
If (n == 0)
{
Return 0;
}
If (n == 1)
{
Return m;
}
Return m + mystery(n - 1, m);
}
What value is returned from a call to mystery(3,6)?
A) 3
B) 6
C) 18
D) 729
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
16
Consider the following code snippet for recursive addition:
Int add(int i, int j)
{
// assumes i >= 0
If (i == 0)
{
Return j;
}
Else
{
Return add(i - 1, j + 1);
}
}
Identify the terminating condition in this recursive method.
A) if (i == 0)
B) return j
C) return add(i - 1, j + 1)
D) there is no terminating condition
Int add(int i, int j)
{
// assumes i >= 0
If (i == 0)
{
Return j;
}
Else
{
Return add(i - 1, j + 1);
}
}
Identify the terminating condition in this recursive method.
A) if (i == 0)
B) return j
C) return add(i - 1, j + 1)
D) there is no terminating condition
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
17
What is required to make a recursive method successful?
I special cases that handle the simplest computations directly
II a recursive call to simplify the computation
III a mutual recursion
A) I
B) II
C) I and II
D) I, II, and III
I special cases that handle the simplest computations directly
II a recursive call to simplify the computation
III a mutual recursion
A) I
B) II
C) I and II
D) I, II, and III
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
18
Consider the getArea method from the textbook shown below: public int getArea()
{
If (width <= 0) { return 0; } // line #1
If (width == 1) { return 1; } // line #2
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
Which line has the recursive case?
A) line #1
B) line #2
C) line #3
D) line #4
{
If (width <= 0) { return 0; } // line #1
If (width == 1) { return 1; } // line #2
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
Which line has the recursive case?
A) line #1
B) line #2
C) line #3
D) line #4
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
19
Consider the recursive method myPrint: public void myPrint(int n)
{
If (n < 10)
{
System.out.print(n);
}
Else
{
Int m = n % 10;
System.out.print(m);
MyPrint(n / 10);
}
}
What is printed for the call myPrint(8)?
A) 10
B) 8
C) 4
D) 21
{
If (n < 10)
{
System.out.print(n);
}
Else
{
Int m = n % 10;
System.out.print(m);
MyPrint(n / 10);
}
}
What is printed for the call myPrint(8)?
A) 10
B) 8
C) 4
D) 21
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
20
Consider the code for the recursive method mysteryPrint shown in this code snippet: public static int mysteryPrint(int n)
{
If (n == 0)
{
Return 0;
}
Else
{
Return (n + mysteryPrint(n-1));
}
}
What will be printed with a call to mysteryPrint(-4)?
A) 0
B) -10
C) -22
D) Nothing - a StackoverflowError exception will occur
{
If (n == 0)
{
Return 0;
}
Else
{
Return (n + mysteryPrint(n-1));
}
}
What will be printed with a call to mysteryPrint(-4)?
A) 0
B) -10
C) -22
D) Nothing - a StackoverflowError exception will occur
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
21
Which of the following options could be used as a terminating condition for a recursive method that finds the middle character of a String with any number of characters?
I the length of the String is 1
II first and last String characters match
III the String is not empty
A) I
B) II
C) I, II and III
D) I and III
I the length of the String is 1
II first and last String characters match
III the String is not empty
A) I
B) II
C) I, II and III
D) I and III
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
22
A recursive method without a special terminating case would _________
A) end immediately.
B) not be recursive.
C) be more efficient.
D) never terminate.
A) end immediately.
B) not be recursive.
C) be more efficient.
D) never terminate.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
23
If a recursive method does not simplify the computation within the method and the base case is not called, what will be the result?
A) The terminating condition will be executed and recursion will end.
B) The recursion calculation will occur correctly regardless.
C) This cannot be determined.
D) Infinite recursion will occur.
A) The terminating condition will be executed and recursion will end.
B) The recursion calculation will occur correctly regardless.
C) This cannot be determined.
D) Infinite recursion will occur.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
24
Consider the method below, which implements the exponentiation operation recursively. Select the statement that should be used to complete the method, so that it handles the special case correctly. public static double power(int base, int exponent)
{
If (exponent == 0)
{
_______________
}
Else
{
Reurn base * power(base, exponent - 1);
}
}
A) return 1;
B) return base;
C) return 0;
D) return 1 * power(base, exponent - 1);
{
If (exponent == 0)
{
_______________
}
Else
{
Reurn base * power(base, exponent - 1);
}
}
A) return 1;
B) return base;
C) return 0;
D) return 1 * power(base, exponent - 1);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
25
Consider the getArea method from the textbook shown below: public int getArea()
{
If (width <= 0) { return 0; } // line #1
Triangle smallerTriangle = new Triangle(width - 1); // line #2
Int smallerArea = smallerTriangle.getArea(); // line #3
Return smallerArea + width; // line #4
}
If line#1 was removed, what would be the result?
A) The recursive method would cause an exception for values below 0.
B) The recursive method would construct triangles whose width was negative.
C) The recursive method would terminate when the width reached 0.
D) The recursive method would correctly calculate the area of the original triangle.
{
If (width <= 0) { return 0; } // line #1
Triangle smallerTriangle = new Triangle(width - 1); // line #2
Int smallerArea = smallerTriangle.getArea(); // line #3
Return smallerArea + width; // line #4
}
If line#1 was removed, what would be the result?
A) The recursive method would cause an exception for values below 0.
B) The recursive method would construct triangles whose width was negative.
C) The recursive method would terminate when the width reached 0.
D) The recursive method would correctly calculate the area of the original triangle.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
26
Consider the getArea method from the textbook shown below: public int getArea()
{
If (width <= 0) { return 0; } // line #1
If (width == 1) { return 1; } // line #2
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
Assume line #3 is changed to this:
Triangle smallerTriangle = new Triangle(width + 1)
When calling the getArea method on a Triangle object with width = 4, what result will be produced?
A) area for all triangles will be computed to be too high
B) area for all triangles will be computed to be too low
C) area will only be incorrect for a triangle objects with width = 1
D) infinite recursion will occur for triangle objects with width >= 2
{
If (width <= 0) { return 0; } // line #1
If (width == 1) { return 1; } // line #2
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
Assume line #3 is changed to this:
Triangle smallerTriangle = new Triangle(width + 1)
When calling the getArea method on a Triangle object with width = 4, what result will be produced?
A) area for all triangles will be computed to be too high
B) area for all triangles will be computed to be too low
C) area will only be incorrect for a triangle objects with width = 1
D) infinite recursion will occur for triangle objects with width >= 2
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
27
Would switching the special case order affect the return value of the following method? public int mystery(int n, int m)
{
If (n == 0) // special case #1
{
Return 0;
}
If (n == 1) // special case #2
{
Return m;
}
Return m + mystery(n - 1, m);
}
A) No
B) Yes
C) It is impossible to tell.
D) An exception will be thrown.
{
If (n == 0) // special case #1
{
Return 0;
}
If (n == 1) // special case #2
{
Return m;
}
Return m + mystery(n - 1, m);
}
A) No
B) Yes
C) It is impossible to tell.
D) An exception will be thrown.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
28
How many recursive calls to the fib method shown below would be made from an original call to fib(4)? (Do not count the original call) public int fib(int n)
{ // assumes n >= 0
If (n <= 1)
{
Return n
}
Else
{
Return (fib(n - 1) + fib(n - 2));
}
}
A) 1
B) 2
C) 4
D) 8
{ // assumes n >= 0
If (n <= 1)
{
Return n
}
Else
{
Return (fib(n - 1) + fib(n - 2));
}
}
A) 1
B) 2
C) 4
D) 8
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
29
____ recursion can occur when a recursive algorithm does not contain a special case to handle the simplest computations directly.
A) Mutual
B) Non-mutual
C) Terminating condition
D) Infinite
A) Mutual
B) Non-mutual
C) Terminating condition
D) Infinite
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
30
If recursion does not have a special terminating case, what error will occur?
A) Index out of range
B) Illegal argument
C) Stack overflow
D) Out of memory
A) Index out of range
B) Illegal argument
C) Stack overflow
D) Out of memory
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
31
Consider the problem of arranging matchsticks so as to form a row of rectangles, as shown below.-----
|--|--|---
Complete the recursive method below, which is designed to return the number of matchsticks needed to form n rectangles.
Public static int matchsticks(int rectangles)
{
If (rectangles == 1) // 1 square can be formed with 6 matchsticks
{
Return 6;
}
Else
{
Return ___________________________
}
}
A) 6 + matchsticks(rectangles - 1);
B) 5 + matchsticks(rectangles - 1);
C) 6 * rectangles;
D) matchsticks(rectangles + 6);
|--|--|---
Complete the recursive method below, which is designed to return the number of matchsticks needed to form n rectangles.
Public static int matchsticks(int rectangles)
{
If (rectangles == 1) // 1 square can be formed with 6 matchsticks
{
Return 6;
}
Else
{
Return ___________________________
}
}
A) 6 + matchsticks(rectangles - 1);
B) 5 + matchsticks(rectangles - 1);
C) 6 * rectangles;
D) matchsticks(rectangles + 6);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
32
Consider the method powerOfTwo shown below: public boolean powerOfTwo(int n)
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
What is the best interpretation of line #1?
A) One is a power of two
B) One is not a power of two
C) Any multiple of one is a power of two
D) 1 is an invalid choice for n
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
What is the best interpretation of line #1?
A) One is a power of two
B) One is not a power of two
C) Any multiple of one is a power of two
D) 1 is an invalid choice for n
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
33
Consider the getArea method from the textbook shown below: public int getArea()
{
If (width <= 0) { return 0; } // line #1
If (width == 1) { return 1; } // line #2
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
Assume that line #3 is changed to this:
Triangle smallerTriangle = new Triangle(width);
This would cause infinite recursion for ____.
A) triangles with width equal to 0
B) triangles with width equal to 1
C) triangles with width greater than or equal to 2
D) triangles of any width
{
If (width <= 0) { return 0; } // line #1
If (width == 1) { return 1; } // line #2
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
Assume that line #3 is changed to this:
Triangle smallerTriangle = new Triangle(width);
This would cause infinite recursion for ____.
A) triangles with width equal to 0
B) triangles with width equal to 1
C) triangles with width greater than or equal to 2
D) triangles of any width
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
34
Insert the missing code in the following code fragment. This fragment is intended to recursively compute xn, where x and n are both non-negative integers: public int power(int x, int n)
{
If (n == 0)
{
____________________
}
Else
{
Return x * power(x, n - 1);
}
}
A) return 1;
B) return x;
C) return power(x, n - 1);
D) return x * power(x, n - 1);
{
If (n == 0)
{
____________________
}
Else
{
Return x * power(x, n - 1);
}
}
A) return 1;
B) return x;
C) return power(x, n - 1);
D) return x * power(x, n - 1);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
35
Consider the method below, which displays the characters from a String in reverse order. Each character appears on a separate line. Select the statement that should be used to complete the method, so that it performs a recursive method call correctly. public static void printReverse(String word)
{
If (word.length() > 0)
{
___________________________
System.out.println(word.charAt(0));
}
}
A) printReverse(word);
B) printReverse(new String(word.charAt(1)));
C) printReverse(word.substring(1));
D) printReverse(word.length() - 1);
{
If (word.length() > 0)
{
___________________________
System.out.println(word.charAt(0));
}
}
A) printReverse(word);
B) printReverse(new String(word.charAt(1)));
C) printReverse(word.substring(1));
D) printReverse(word.length() - 1);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
36
Consider the getArea method from the textbook shown below: public int getArea()
{
If (width <= 0) { return 0; } // line #1
If (width == 1) { return 1; } // line #2
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
If line #1 was eliminated from the method, what would be the result when calling getArea?
A) A negative or zero width would cause problems.
B) Nothing - the method would still work correctly.
C) We would lose our only recursive case.
D) A positive width would reduce the correct area by 1.
{
If (width <= 0) { return 0; } // line #1
If (width == 1) { return 1; } // line #2
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
If line #1 was eliminated from the method, what would be the result when calling getArea?
A) A negative or zero width would cause problems.
B) Nothing - the method would still work correctly.
C) We would lose our only recursive case.
D) A positive width would reduce the correct area by 1.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
37
Consider the getArea method from the textbook shown below. public int getArea()
{
If (width <= 0) { return 0; } // line #1
If (width == 1) { return 1; } // line #2
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
Assume that line #2 is changed to this:
If (width == 1) { return 2; }
How would this affect calls to getArea?
A) It would add 1 to all calls except those where width <= 0.
B) It would make no difference to any call.
C) It would double every triangle area.
D) It would subtract 1 from all calls.
{
If (width <= 0) { return 0; } // line #1
If (width == 1) { return 1; } // line #2
Triangle smallerTriangle = new Triangle(width - 1); // line #3
Int smallerArea = smallerTriangle.getArea(); // line #4
Return smallerArea + width; // line #5
}
Assume that line #2 is changed to this:
If (width == 1) { return 2; }
How would this affect calls to getArea?
A) It would add 1 to all calls except those where width <= 0.
B) It would make no difference to any call.
C) It would double every triangle area.
D) It would subtract 1 from all calls.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
38
Consider the method below, which prints the digits of an arbitrary integer in reverse order, one digit per line. The method should print the last digit first. Then, it should recursively print the integer obtained by removing the last digit. Select the statements that should be used to complete the method. public static void printReverse(int value)
{
If (value > 0)
{
_____________________ // print last digit
_____________________ // recursive call to print value without last digit
}
}
A) System.out.println(value / 10);
PrintReverse(value / 10);
B) System.out.println(value % 10);
PrintReverse(value / 10);
C) System.out.println(value / 10);
PrintReverse(value % 10);
D) System.out.println(value % 10);
PrintReverse(value % 10);
{
If (value > 0)
{
_____________________ // print last digit
_____________________ // recursive call to print value without last digit
}
}
A) System.out.println(value / 10);
PrintReverse(value / 10);
B) System.out.println(value % 10);
PrintReverse(value / 10);
C) System.out.println(value / 10);
PrintReverse(value % 10);
D) System.out.println(value % 10);
PrintReverse(value % 10);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
39
When a recursive method is called, and it does not perform recursion, what must be true?
A) The terminating condition was true.
B) One recursive case condition was true.
C) All recursive case conditions were true.
D) An exception will occur in the method.
A) The terminating condition was true.
B) One recursive case condition was true.
C) All recursive case conditions were true.
D) An exception will occur in the method.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
40
Consider the following recursive code snippet: public int mystery(int n, int m)
{
If (n == 0)
{
Return 0;
}
If (n == 1)
{
Return m;
}
Return m + mystery(n - 1, m);
}
What parameter values for n would cause an infinite recursion problem in the following method?
A) n == 0
B) n == 1
C) all n with n < 0
D) all n with n >= 0
{
If (n == 0)
{
Return 0;
}
If (n == 1)
{
Return m;
}
Return m + mystery(n - 1, m);
}
What parameter values for n would cause an infinite recursion problem in the following method?
A) n == 0
B) n == 1
C) all n with n < 0
D) all n with n >= 0
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
41
Complete the code for the myFactorial recursive method shown below, which is intended to compute the factorial of the value passed to the method: public int myFactorial(int anInteger)
{
If (anInteger == 1)
{
Return 1;
}
Else
{
______________________
}
}
A) return(anInteger * (myFactorial(anInteger)));
B) return ((anInteger - 1) * (myFactorial(anInteger)));
C) return(anInteger * (myFactorial(anInteger - 1)));
D) return((anInteger - 1)*(myFactorial(anInteger - 1)));
{
If (anInteger == 1)
{
Return 1;
}
Else
{
______________________
}
}
A) return(anInteger * (myFactorial(anInteger)));
B) return ((anInteger - 1) * (myFactorial(anInteger)));
C) return(anInteger * (myFactorial(anInteger - 1)));
D) return((anInteger - 1)*(myFactorial(anInteger - 1)));
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
42
Given the following code snippet: public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (n % 10) + newCalc(n / 10);
}
}
What value will be returned when this code is executed with a call to newCalc(15)?
A) 2
B) 2.5
C) 6
D) 6.5
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (n % 10) + newCalc(n / 10);
}
}
What value will be returned when this code is executed with a call to newCalc(15)?
A) 2
B) 2.5
C) 6
D) 6.5
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
43
Given the following class code: public class RecurseMore
{
Public static void main(String[] args)
{
Recurse(4);
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 4 + recurse(n - 2);
}
System.out.println(total);
Return total;
}
}
What values will be printed when this code is executed?
A) 0, 4, and 8
B) 4 and 8
C) 4
D) 8
{
Public static void main(String[] args)
{
Recurse(4);
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 4 + recurse(n - 2);
}
System.out.println(total);
Return total;
}
}
What values will be printed when this code is executed?
A) 0, 4, and 8
B) 4 and 8
C) 4
D) 8
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
44
Given the following code snippet: public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (1 + newCalc(n / 10));
}
}
What value will be returned when this code is executed with a call to newCalc(15)?
A) 2
B) 2.5
C) 5
D) 5.5
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (1 + newCalc(n / 10));
}
}
What value will be returned when this code is executed with a call to newCalc(15)?
A) 2
B) 2.5
C) 5
D) 5.5
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
45
Complete the following code snippet, which is intended to be a recursive method that will find the smallest value in an array of double values from index to the end of the array: public static double minVal(double[] elements, int index)
{
If (index == elements.length - 1)
{
__________________
}
Double val = minVal(elements, index + 1);
If (elements[index] < val)
{
Return elements[index];
}
Else
{
Return val;
}
}
A) return elements[index];
B) return 1;
C) return 0;
D) return elements[0];
{
If (index == elements.length - 1)
{
__________________
}
Double val = minVal(elements, index + 1);
If (elements[index] < val)
{
Return elements[index];
}
Else
{
Return val;
}
}
A) return elements[index];
B) return 1;
C) return 0;
D) return elements[0];
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
46
Given the following class code: public class RecurseSample
{
Public static void main(String[] args)
{
Recurse(3);
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;}
Else
{
Total = 3 + recurse(n - 1);
}
System.out.println(total);
Return total;
}
}
What values will be printed?
A) 1, 3, and 6
B) 1, 3, 6, and 9
C) 3, 6, and 9
D) 3, 6, 9, and 12
{
Public static void main(String[] args)
{
Recurse(3);
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;}
Else
{
Total = 3 + recurse(n - 1);
}
System.out.println(total);
Return total;
}
}
What values will be printed?
A) 1, 3, and 6
B) 1, 3, 6, and 9
C) 3, 6, and 9
D) 3, 6, 9, and 12
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
47
Given the following code snippet: public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (n % 10) + newCalc(n / 10);
}
}
What value will be returned when this code is executed with a call to newCalc(5)?
A) 2
B) 2.5
C) 5
D) 5.5
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (n % 10) + newCalc(n / 10);
}
}
What value will be returned when this code is executed with a call to newCalc(5)?
A) 2
B) 2.5
C) 5
D) 5.5
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
48
Given the following class code: public class RecurseSample
{
Public static void main(String[] args)
{
System.out.println(recurse(3));
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 3 + recurse(n - 1);
}
Return total;
}
}
What values will be printed when this code is executed?
A) 6
B) 9
C) 3, 6, and 9
D) 1, 3, 6, and 9
{
Public static void main(String[] args)
{
System.out.println(recurse(3));
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 3 + recurse(n - 1);
}
Return total;
}
}
What values will be printed when this code is executed?
A) 6
B) 9
C) 3, 6, and 9
D) 1, 3, 6, and 9
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
49
Given the following code snippet: public static int newCalc(int n)
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (1 + newCalc(n / 10));
}
}
What value will be returned when this code is executed with a call to newCalc(5)?
A) 1
B) 1.5
C) 5
D) 5.5
{
If (n < 0)
{
Return -1;
}
Else if (n < 10)
{
Return n;
}
Else
{
Return (1 + newCalc(n / 10));
}
}
What value will be returned when this code is executed with a call to newCalc(5)?
A) 1
B) 1.5
C) 5
D) 5.5
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
50
Complete the code for the calcPower recursive method shown below, which is intended to raise the base number passed into the method to the exponent power passed into the method: public static int calcPower(int baseNum, int exponent)
{
Int answer = 0;
If (exponent == 0)
{
_____________________
}
Else
{
Answer = baseNum * calcPower (baseNum, exponent - 1);
}
Return answer;
}
A) answer = 0;
B) answer = 1;
C) answer = -1;
D) answer = calcPower(1);
{
Int answer = 0;
If (exponent == 0)
{
_____________________
}
Else
{
Answer = baseNum * calcPower (baseNum, exponent - 1);
}
Return answer;
}
A) answer = 0;
B) answer = 1;
C) answer = -1;
D) answer = calcPower(1);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
51
Complete the following code snippet, which is intended to be a recursive method that will find the smallest value in an array of double values from index to the end of the array: public static double minVal(double[] elements, int index)
{
If (index == elements.length - 1)
{
Return elements[index];
}
Double val = __________________;
If (elements[index] < val)
{
Return elements[index];
}
Else
{
Return val;
}
}
A) minVal(elements, index - 1)
B) minVal(index - 1)
C) minVal(elements, index + 1)
D) minVal(index + 1)
{
If (index == elements.length - 1)
{
Return elements[index];
}
Double val = __________________;
If (elements[index] < val)
{
Return elements[index];
}
Else
{
Return val;
}
}
A) minVal(elements, index - 1)
B) minVal(index - 1)
C) minVal(elements, index + 1)
D) minVal(index + 1)
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
52
Consider the method powerOfTwo shown below: public boolean powerOfTwo(int n)
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
How many recursive calls are made from the original call powerOfTwo(63) (not including the original call)?
A) 6
B) 4
C) 1
D) 0
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
How many recursive calls are made from the original call powerOfTwo(63) (not including the original call)?
A) 6
B) 4
C) 1
D) 0
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
53
Complete the code for the myFactorial recursive method shown below, which is intended to compute the factorial of the value passed to the method: public int myFactorial(int anInteger)
{
_____________________________
{
Return 1;
}
Else
{
Return(anInteger * myFactorial(anInteger - 1));
}
}
A) if (anInteger == 1)
B) if ((anInteger - 1) == 1)
C) if (anInteger * (anInteger - 1) == 1)
D) if (myFactorial(anInteger) == 1)
{
_____________________________
{
Return 1;
}
Else
{
Return(anInteger * myFactorial(anInteger - 1));
}
}
A) if (anInteger == 1)
B) if ((anInteger - 1) == 1)
C) if (anInteger * (anInteger - 1) == 1)
D) if (myFactorial(anInteger) == 1)
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
54
Complete the code for the recursive method shown below, which is intended to compute the sum of the first n integers: public int s(int n)
{
If (n == 1)
{
Return 1;
}
Else
{
_________________
}
}
A) return n + (n - 1);
B) return s(n) + n - 1;
C) return n + s(n - 1);
D) return n + s(n + 1);
{
If (n == 1)
{
Return 1;
}
Else
{
_________________
}
}
A) return n + (n - 1);
B) return s(n) + n - 1;
C) return n + s(n - 1);
D) return n + s(n + 1);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
55
Complete the code for the calcPower recursive method shown below, which is intended to raise the base number passed into the method to the exponent power passed into the method: public static int calcPower(int baseNum, int exponent)
{
Int answer = 0;
If (exponent == 0)
{
Answer = 1;
}
Else
{
_______________________________________
}
Return answer;
}
A) answer = baseNum * calcPower (baseNum -1, exponent);
B) answer = baseNum * calcPower (baseNum, exponent - 1);
C) answer = baseNum * calcPower (baseNum, exponent);
D) answer = baseNum * calcPower (baseNum -1, exponent - 1);
{
Int answer = 0;
If (exponent == 0)
{
Answer = 1;
}
Else
{
_______________________________________
}
Return answer;
}
A) answer = baseNum * calcPower (baseNum -1, exponent);
B) answer = baseNum * calcPower (baseNum, exponent - 1);
C) answer = baseNum * calcPower (baseNum, exponent);
D) answer = baseNum * calcPower (baseNum -1, exponent - 1);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
56
Given the following class code: public class RecurseMore
{
Private static int total;
Public static void main(String[] args)
{
System.out.println(recurse(4));
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 4 + recurse(n - 2);
}
Return total;
}
}
What values will be printed when this code is executed?
A) 0, 4, and 8
B) 4 and 8
C) 4
D) 8
{
Private static int total;
Public static void main(String[] args)
{
System.out.println(recurse(4));
}
Public static int recurse(int n)
{
Int total = 0;
If (n == 0)
{
Return 0;
}
Else
{
Total = 4 + recurse(n - 2);
}
Return total;
}
}
What values will be printed when this code is executed?
A) 0, 4, and 8
B) 4 and 8
C) 4
D) 8
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
57
Consider the method powerOfTwo shown below: public boolean powerOfTwo(int n)
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
How many recursive calls are made from the original call of powerOfTwo(64) (not including the original call)?
A) 8
B) 6
C) 4
D) 2
{
If (n == 1) // line #1
{
Return true;
}
Else if (n % 2 == 1) // line #2
{
Return false;
}
Else
{
Return powerOfTwo(n / 2); // line #3
}
}
How many recursive calls are made from the original call of powerOfTwo(64) (not including the original call)?
A) 8
B) 6
C) 4
D) 2
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
58
Complete the code for the myFactorial recursive method shown below, which is intended to compute the factorial of the value passed to the method: public int myFactorial(int anInteger)
{
If (anInteger == 1)
{
______________________
}
Else
{
Return (anInteger * myFactorial(anInteger - 1));
}
}
A) return 0;
B) return -anInteger;
C) return 1;
D) return myFactorial(anInteger);
{
If (anInteger == 1)
{
______________________
}
Else
{
Return (anInteger * myFactorial(anInteger - 1));
}
}
A) return 0;
B) return -anInteger;
C) return 1;
D) return myFactorial(anInteger);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
59
Complete the code for the calcPower recursive method shown below, which is intended to raise the base number passed into the method to the exponent power passed into the method: public static int calcPower(int baseNum, int exponent)
{
Int answer = 0;
________________________
{
Answer = 1;
}
Else
{
Answer = baseNum * calcPower (baseNum, exponent - 1);
}
Return answer;
}
A) if (exponent == 0)
B) if (exponent == 1)
C) if (exponent == -1)
D) if (exponent != 1)
{
Int answer = 0;
________________________
{
Answer = 1;
}
Else
{
Answer = baseNum * calcPower (baseNum, exponent - 1);
}
Return answer;
}
A) if (exponent == 0)
B) if (exponent == 1)
C) if (exponent == -1)
D) if (exponent != 1)
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
60
Complete the following code snippet, which is intended to be a recursive method that will find the sum of all elements in an array of double values from index to the end of the array: // return the sum of all elements in arr[]
Public static double findSum(double arr[], int index)
{
If (index == 0)
{
Return arr[index];
}
Else
{
_____________________
}
}
Assume that this method would be called using an existing array named myArray as follows:
FindSum(myArray, myArray.length - 1);
A) return (findSum(arr, index + 1));
B) return (findSum(arr, index - 1));
C) return (arr[index] + findSum(arr, index + 1));
D) return (arr[index] + findSum(arr, index - 1));
Public static double findSum(double arr[], int index)
{
If (index == 0)
{
Return arr[index];
}
Else
{
_____________________
}
}
Assume that this method would be called using an existing array named myArray as follows:
FindSum(myArray, myArray.length - 1);
A) return (findSum(arr, index + 1));
B) return (findSum(arr, index - 1));
C) return (arr[index] + findSum(arr, index + 1));
D) return (arr[index] + findSum(arr, index - 1));
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
61
Consider the recursive square method shown below that takes a non-negative int argument. public int square(int n)
{
Return square(n, n);
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}
Assume that the last return statement is changed to this:
Return n * square(c - 1, n);
What would a call to square(4) return?
A) 4
B) 16
C) 64
D) 256
{
Return square(n, n);
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}
Assume that the last return statement is changed to this:
Return n * square(c - 1, n);
What would a call to square(4) return?
A) 4
B) 16
C) 64
D) 256
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
62
Consider the helper method reversePrint, which uses recursion to display in reverse the elements in a section of an array limited by the firstIndex and lastIndex arguments. What statement should be used to complete the recursive method? public static void reversePrint(int[] array, int firstIndex, int lastIndex)
{
If (firstIndex < lastIndex)
{
________________________________________
}
System.out.println(array[firstIndex]);
}
Public static void main(String[] args)
{
Int [] numbers = { 4, 7, 1, 0, 2, 7 };
ReversePrint(numbers, 0, numbers.length - 1);
}
A) reversePrint(array, firstIndex, lastIndex + 1);
B) reversePrint(array, firstIndex, lastIndex - 1);
C) reversePrint(array, firstIndex + 1, lastIndex - 1);
D) reversePrint(array, firstIndex + 1, lastIndex);
{
If (firstIndex < lastIndex)
{
________________________________________
}
System.out.println(array[firstIndex]);
}
Public static void main(String[] args)
{
Int [] numbers = { 4, 7, 1, 0, 2, 7 };
ReversePrint(numbers, 0, numbers.length - 1);
}
A) reversePrint(array, firstIndex, lastIndex + 1);
B) reversePrint(array, firstIndex, lastIndex - 1);
C) reversePrint(array, firstIndex + 1, lastIndex - 1);
D) reversePrint(array, firstIndex + 1, lastIndex);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
63
Complete the following code snippet, which is intended to be a recursive method that reverses a String value: public static String reverseIt(String s)
{
If
{
Return s;
}
Else
{
________________________
}
}
A) return reverseIt;
B) return reverseIt;
C) return reverseIt;
D) return reverseIt;
{
If
{
Return s;
}
Else
{
________________________
}
}
A) return reverseIt;
B) return reverseIt;
C) return reverseIt;
D) return reverseIt;
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
64
Consider the problem of displaying a pattern of asterisks which form a triangle of height h, as shown below for h = 4: *
**
***
****
***
**
*
The problem can be solved with the recursive helper method shape below. The method takes two parameters: low and high. It first prints a row of asterisks corresponding to parameter low. If high is higher than low, it recursively generates a shape in which low is incremented by one, and then prints another row of low asterisks. Select a statement to complete method triangle, so that the helper method shape is invoked with the correct arguments in order to display a triangle with parameter height.
Public static void shape(int low, int high)
{
If (high >= low)
{
AsterisksRow(low);
If (high > low)
{
Shape(low + 1, high);
AsterisksRow(low);
}
}
}
Public static void asterisksRow(int n) // Method to display a row of n stars
{
For (int j = 1; j < n; ++j)
{
System.out.print("*");
}
System.out.println("*");
}
Public static void triangle(int height)
{
If (height > 1)
{
_______________________
}
}
A) shape(0, height);
B) shape(0, height - 1);
C) shape(1, height);
D) shape(0, height - 1);
**
***
****
***
**
*
The problem can be solved with the recursive helper method shape below. The method takes two parameters: low and high. It first prints a row of asterisks corresponding to parameter low. If high is higher than low, it recursively generates a shape in which low is incremented by one, and then prints another row of low asterisks. Select a statement to complete method triangle, so that the helper method shape is invoked with the correct arguments in order to display a triangle with parameter height.
Public static void shape(int low, int high)
{
If (high >= low)
{
AsterisksRow(low);
If (high > low)
{
Shape(low + 1, high);
AsterisksRow(low);
}
}
}
Public static void asterisksRow(int n) // Method to display a row of n stars
{
For (int j = 1; j < n; ++j)
{
System.out.print("*");
}
System.out.println("*");
}
Public static void triangle(int height)
{
If (height > 1)
{
_______________________
}
}
A) shape(0, height);
B) shape(0, height - 1);
C) shape(1, height);
D) shape(0, height - 1);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
65
Complete the following code snippet, which is intended to be a recursive method that will find the sum of all elements in an array of double values from index to the end of the array: // return the sum of all elements in arr[]
Public static double findSum(double arr[], int index)
{
If (index == 0)
{
_____________________
}
Else
{
Return (arr[index] + findSum(arr, index - 1));
}
}
Assume that this method would be called using an existing array named myArray as follows:
FindSum(myArray,myArray.length - 1);
A) return arr[index];
B) return arr[index + 1];
C) return arr[1];
D) return arr[index - 1];
Public static double findSum(double arr[], int index)
{
If (index == 0)
{
_____________________
}
Else
{
Return (arr[index] + findSum(arr, index - 1));
}
}
Assume that this method would be called using an existing array named myArray as follows:
FindSum(myArray,myArray.length - 1);
A) return arr[index];
B) return arr[index + 1];
C) return arr[1];
D) return arr[index - 1];
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
66
A palindrome is a word or phrase that reads the same forward or backward. Consider the following code snippet: public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What is the purpose of the palindrome method?
A) Return the palindrome to the calling method.
B) Provide the string, along with its first and last indexes to the recursive isPal method.
C) Send the recursive isPal method its terminating condition.
D) Recursively call itself.
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What is the purpose of the palindrome method?
A) Return the palindrome to the calling method.
B) Provide the string, along with its first and last indexes to the recursive isPal method.
C) Send the recursive isPal method its terminating condition.
D) Recursively call itself.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
67
Consider the fib method from the textbook shown below: public static long fib(int n)
{
If (n <= 2)
{
Return 1; // line #1
}
Else
{
Return fib(n - 1) + fib(n - 2); // line #2
}
}
Assume line #2 is changed to this:
Else { return 2 * fib(n - 1) + 2 * fib(n - 2); }
What effect will this change have?
A) This will return 5 from fib(4)
B) This will return 8 from fib(4)
C) This will return 10 from fib(4)
D) This will cause an exception on a call fib(4)
{
If (n <= 2)
{
Return 1; // line #1
}
Else
{
Return fib(n - 1) + fib(n - 2); // line #2
}
}
Assume line #2 is changed to this:
Else { return 2 * fib(n - 1) + 2 * fib(n - 2); }
What effect will this change have?
A) This will return 5 from fib(4)
B) This will return 8 from fib(4)
C) This will return 10 from fib(4)
D) This will cause an exception on a call fib(4)
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
68
Assume that recursive method search returns true if argument value is one of the elements in the section of the array limited by the firstIndex and lastIndex arguments. What statement can be used in main to determine if the value 7 is one of the elements in array values? public static boolean search(int value, int[] array, int firstIndex, int lastIndex)
{
If (firstIndex <= lastIndex)
{
If (array[firstIndex] == value)
{
Return true;
}
Else
{
Return search(value, array, firstIndex + 1, lastIndex);
}
}
Return false;
}
Public static void main(String[] args)
{
Int [] values = { 4, 7, 1, 0, 2, 7 };
If ( _________________________________ )
{
System.out.println("7 is in the array");
}
}
A) search(7, values, 0, values.length)
B) search(7, values, 1, values.length)
C) search(7, values, 0, values.length - 1)
D) search(7, values, 1, values.length - 1)
{
If (firstIndex <= lastIndex)
{
If (array[firstIndex] == value)
{
Return true;
}
Else
{
Return search(value, array, firstIndex + 1, lastIndex);
}
}
Return false;
}
Public static void main(String[] args)
{
Int [] values = { 4, 7, 1, 0, 2, 7 };
If ( _________________________________ )
{
System.out.println("7 is in the array");
}
}
A) search(7, values, 0, values.length)
B) search(7, values, 1, values.length)
C) search(7, values, 0, values.length - 1)
D) search(7, values, 1, values.length - 1)
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
69
What is the purpose of a recursive helper method?
A) Shield the user of the recursive method from the recursive details.
B) Speed up the execution.
C) Eliminate the recursion.
D) Add another base case.
A) Shield the user of the recursive method from the recursive details.
B) Speed up the execution.
C) Eliminate the recursion.
D) Add another base case.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
70
Consider the fib method from the textbook shown below: public static long fib(int n)
{
If (n <= 2)
{
Return 1; // line #1
}
Else
{
Return fib(n - 1) + fib(n - 2); // line #2
}
}
Assume line #1 is changed to this:
If (n <= 2) { return 2; }
How will this change affect the result of calling fib(7)?
A) It will add 2 to the return from fib(7)
B) It will add 4 to the return from fib(7)
C) It will add 8 to the return from fib(7)
D) It will double the return from fib(7)
{
If (n <= 2)
{
Return 1; // line #1
}
Else
{
Return fib(n - 1) + fib(n - 2); // line #2
}
}
Assume line #1 is changed to this:
If (n <= 2) { return 2; }
How will this change affect the result of calling fib(7)?
A) It will add 2 to the return from fib(7)
B) It will add 4 to the return from fib(7)
C) It will add 8 to the return from fib(7)
D) It will double the return from fib(7)
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
71
Consider the recursive square method shown below that takes a non-negative int argument: public int square(int n)
{
Return square(n, n);
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}
Assume that the last return statement is changed to this:
Return square(c - 1, n);
What would a call to square(7) return?
A) 7
B) 13
C) 14
D) 49
{
Return square(n, n);
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}
Assume that the last return statement is changed to this:
Return square(c - 1, n);
What would a call to square(7) return?
A) 7
B) 13
C) 14
D) 49
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
72
Consider the fib method from the textbook shown below. public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
Calling fib(3) will trigger ___ recursive call(s) and execute the terminating condition ___ time(s), respectively.
A) 1, 1
B) 2, 2
C) 1, 2
D) 2, 1
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
Calling fib(3) will trigger ___ recursive call(s) and execute the terminating condition ___ time(s), respectively.
A) 1, 1
B) 2, 2
C) 1, 2
D) 2, 1
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
73
Consider the code for the recursive method printSum shown in this code snippet, which is intended to return the sum of digits from 1 to n: public static int printSum(int n)
{
If (n <= 0) // line #1
{
Return 0; // line #
}
Else
{
Return (n + printSum(n)); //line #3
}
}
Which of the following statements is correct?
A) line #1 is incorrect, and should be changed to if (n <= 1)
B) line #3 is incorrect, and should be changed to return (printSum (n - 1));
C) line #3 is incorrect, and should be changed to return (n + printSum (n + 1));
D) line #3 is incorrect, and should be changed to return (n + printSum (n - 1));
{
If (n <= 0) // line #1
{
Return 0; // line #
}
Else
{
Return (n + printSum(n)); //line #3
}
}
Which of the following statements is correct?
A) line #1 is incorrect, and should be changed to if (n <= 1)
B) line #3 is incorrect, and should be changed to return (printSum (n - 1));
C) line #3 is incorrect, and should be changed to return (n + printSum (n + 1));
D) line #3 is incorrect, and should be changed to return (n + printSum (n - 1));
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
74
A palindrome is a word or phrase spelled which reads the same forward or backward. Consider the following code snippet: public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What does the method palindrome return?
A) true
B) false
C) a palindrome not found exception
D) the Boolean value returned from the isPal method
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What does the method palindrome return?
A) true
B) false
C) a palindrome not found exception
D) the Boolean value returned from the isPal method
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
75
Why does the best recursive method usually run slightly slower than its iterative counterpart?
A) Testing the terminating condition takes longer.
B) Each recursive method call takes processor time.
C) Multiple recursive cases must be considered.
D) Checking multiple terminating conditions take more processor time.
A) Testing the terminating condition takes longer.
B) Each recursive method call takes processor time.
C) Multiple recursive cases must be considered.
D) Checking multiple terminating conditions take more processor time.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
76
Consider the fib method from the textbook shown below: public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
Computing the 7th fibonacci number, fib(7), recursively computes fib(6), fib(5), and fib(4) ___ times respectively.
A) 1, 2, and 3
B) 6, 5, and 4
C) 4, 5, and 6
D) 3, 2, and 1
{
If (n <= 2)
{
Return 1;
}
Else
{
Return fib(n - 1) + fib(n - 2);
}
}
Computing the 7th fibonacci number, fib(7), recursively computes fib(6), fib(5), and fib(4) ___ times respectively.
A) 1, 2, and 3
B) 6, 5, and 4
C) 4, 5, and 6
D) 3, 2, and 1
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
77
Consider the recursive square method shown below. It takes a non-negative int argument. Then it recursively adds the number n to itself n times to produce the square of n. Complete the correct code for the square helper method. public int square(int n)
{
____________________;
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}
A) return square(n, n)
B) return square(n)
C) return square(n - 1, n)
D) return square(n, n - 1)
{
____________________;
}
Public int square(int c, int n)
{
If (c == 1)
{
Return n;
}
Else
{
Return n + square(c - 1, n);
}
}
A) return square(n, n)
B) return square(n)
C) return square(n - 1, n)
D) return square(n, n - 1)
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
78
Complete the following code snippet, which is intended to be a recursive method that reverses a String value: public static String reverseIt(String s)
{
If
{
_________
}
Else
{
Return reverseIt;
}
}
A) return s;
B) return 0;
C) return s.charAt(0);
D) return s.substring(1);
{
If
{
_________
}
Else
{
Return reverseIt;
}
}
A) return s;
B) return 0;
C) return s.charAt(0);
D) return s.substring(1);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
79
A palindrome is a word or phrase that reads the same forward or backward. Consider the methods palindrome and isPal shown below: public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
The method palindrome as shown here would be considered to be a ____ method.
A) recursive
B) terminating
C) helper
D) static
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
The method palindrome as shown here would be considered to be a ____ method.
A) recursive
B) terminating
C) helper
D) static
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
80
A palindrome is a word or phrase that reads the same forward or backward. Consider the following code snippet: public boolean palindrome(String string)
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What does the condition left >= right refer to?
A) An empty or one-character string is considered a palindrome.
B) The string is not a palindrome.
C) It cannot be determined if the string is a palindrome.
D) You have reached the middle of the string.
{
Return isPal(string, 0, string.length() - 1);
}
Private boolean isPal(String string, int left, int right)
{
If (left >= right)
{
Return true;
}
Else if (string.charAt(left) == string.charAt(right))
{
Return isPal(string, left + 1, right - 1);
}
Else
{
Return false;
}
}
What does the condition left >= right refer to?
A) An empty or one-character string is considered a palindrome.
B) The string is not a palindrome.
C) It cannot be determined if the string is a palindrome.
D) You have reached the middle of the string.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck