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 Late 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 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) ?
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 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?
Question 5
Multiple Choice
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) ?
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 } } Assume the code in line #3 is changed to: Triangle smallerTriangle = new Triangle(width) ; This change would cause infinite recursion for which triangles?
Question 7
Multiple Choice
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) ?
Question 8
Multiple Choice
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?
Question 9
Multiple Choice
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
Question 10
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 11
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 12
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 13
Multiple Choice
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) ?
Question 14
Multiple Choice
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?
Question 15
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 terminating condition(s) ?
Question 16
Multiple Choice
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?
Question 17
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) ?