Exam 13: Recursion
Exam 1: Introduction98 Questions
Exam 2: Using Objects76 Questions
Exam 3: Implementing Classes103 Questions
Exam 4: Fundamental Data Types125 Questions
Exam 5: Decisions120 Questions
Exam 6: Loops128 Questions
Exam 7: Arrays and Array Lists118 Questions
Exam 8: Designing Classes95 Questions
Exam 9: Inheritance101 Questions
Exam 10: Interfaces85 Questions
Exam 11: Inputoutput and Exception Handling109 Questions
Exam 12: Object-Oriented Design104 Questions
Exam 13: Recursion110 Questions
Exam 14: Sorting and Searching109 Questions
Exam 15: The Java Collections Framework110 Questions
Exam 16: Basic Data Structures104 Questions
Exam 17: Tree Structures110 Questions
Exam 18: Generic Classes75 Questions
Exam 19: Graphical User Interfaces76 Questions
Exam 20: Streams and Binary Inputoutput82 Questions
Exam 21: Multithreading82 Questions
Exam 22: Internet Networking74 Questions
Exam 23: Relational Databases75 Questions
Exam 24: XML74 Questions
Exam 25: Web Applications75 Questions
Select questions type
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));
}
}
(Multiple Choice)
4.9/5
(37)
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?
(Multiple Choice)
4.8/5
(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 (1 + newCalc(n / 10));
}
}
What value will be returned when this code is executed with a call to newCalc(5)?
(Multiple Choice)
4.8/5
(42)
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?
(Multiple Choice)
4.7/5
(40)
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));
}
}
(Multiple Choice)
4.8/5
(38)
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.
(Multiple Choice)
5.0/5
(43)
Consider the recursive version of 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);
}
}
How many recursive calls to fib(2) will be made from the original call of fib(6)?
(Multiple Choice)
4.8/5
(41)
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)?
(Multiple Choice)
4.8/5
(42)
In recursion, the non-recursive case is analogous to a loop ____.
(Multiple Choice)
4.8/5
(43)
The method below generates all substrings of a String passed as argument. Assuming that the string contains no duplicate characters, select the statement to complete the method so that it prints all substrings correctly. public static void printSubstrings(String word)
{
If (word.length() > 0)
{
For (int j = 1; j <= word.length(); j++) // print substrings that begin
{ // with the first character
System.out.println(word.substring(0, j));
}
___________________________________ // print substrings without
} // the first character
}
(Multiple Choice)
4.8/5
(33)
If recursion does not have a special terminating case, what error will occur?
(Multiple Choice)
4.7/5
(43)
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?
(Multiple Choice)
4.9/5
(38)
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);
}
(Multiple Choice)
4.8/5
(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)?
(Multiple Choice)
4.9/5
(38)
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?
(Multiple Choice)
4.8/5
(41)
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?
(Multiple Choice)
4.8/5
(31)
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)?
(Multiple Choice)
4.9/5
(29)
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?
(Multiple Choice)
4.9/5
(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);
}
}
(Multiple Choice)
4.8/5
(37)
Consider the recursive version of 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);
}
}
How many total recursive calls (not counting the original call) to fib will be made from the original call of fib(6)?
(Multiple Choice)
4.9/5
(43)
Showing 21 - 40 of 110
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)