Services
Discover
Ask a Question
Log in
Sign up
Filters
Done
Question type:
Essay
Multiple Choice
Short Answer
True False
Matching
Topic
Computing
Study Set
Big Java Binder Early Objects
Exam 13: Recursion
Path 4
Access For Free
Share
All types
Filters
Study Flashcards
Practice Exam
Learn
Question 1
Multiple Choice
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 { ______________________ } }
Question 2
Multiple Choice
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) ?
Question 3
Multiple Choice
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?
Question 4
Multiple Choice
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) ?
Question 5
Multiple Choice
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?
Question 6
Multiple Choice
Complete the following code snippet, which is intended to print out all permutations of the string generate by using a permutation generator object. public class PermutationGeneratorTester { Public static void main(String[] args) { PermutationGenerator generator = new PermutationGenerator("generate") ; ArrayList<String> permutations = generator.getPermutations() ; For (String s : permutations) { ____________________ } } }
Question 7
Multiple Choice
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?
Question 8
Multiple Choice
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?
Question 9
Multiple Choice
Recursion does NOT take place if any of the following happen: I method A calls method B, which calls method C, which calls method B II method A calls method B, which calls method A III method A calls method B, B returns, and A calls B again