Services
Discover
Homeschooling
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
Quiz 13: Recursion
Path 4
Access For Free
Share
All types
Filters
Study Flashcards
Practice Exam
Learn
Question 1
Multiple Choice
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.
Question 2
Multiple Choice
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 { ______________________________ } }
Question 3
Multiple Choice
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?
Question 4
Multiple Choice
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) ?
Question 5
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 } } Assume the code in line #3 is changed to: Triangle smallerTriangle = new Triangle(width) ; This change would cause infinite recursion for which triangles?
Question 6
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 7
Multiple Choice
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) ?