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
Recursion will take place if any of the following happen:
I method A calls method B, which calls method C
II method A calls method B, which calls method A
III method A calls method A
(Multiple Choice)
4.9/5
(44)
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?
(Multiple Choice)
4.8/5
(40)
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);
}
}
(Multiple Choice)
4.9/5
(41)
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);
}
(Multiple Choice)
4.9/5
(42)
Consider the mutually recursive methods below. Select the method call that could be used to generate the output sequence: A5 B4 A3 B2 A1 public static void methodA(int value)
{
If (value > 0)
{
System.out.print(" A" + value);
MethodB(value - 1);
}
}
Public static void methodB(int value)
{
If (value > 0)
{
System.out.print(" B" + value);
MethodA(value - 1);
}
}
(Multiple Choice)
4.8/5
(35)
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)?
(Multiple Choice)
4.9/5
(37)
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?
(Multiple Choice)
4.7/5
(39)
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)?
(Multiple Choice)
4.9/5
(34)
Consider the iterative version of the fib method from the textbook shown below: public static long fib(int n)
{
If (n <= 2)
{
Return 1;
}
Long fold = 1;
Long fold2 = 1;
Long fnew = 1;
For (int i = 3; i <= n; i++)
{
Fnew = fold + fold2;
Fold2 = fold;
Fold = fnew;
}
Return fnew;
}
How many iterations of the for loop will there be for the call fib(6)?
(Multiple Choice)
4.9/5
(35)
If a recursive method does not simplify the computation within the method and the base case is not called, what will be the result?
(Multiple Choice)
4.9/5
(35)
Showing 101 - 110 of 110
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)