Compsci Recursion
int mystery (int w) { if (w < 0) return 0; int x = mystery (w - 2); return w - x; } What is returned by the call mystery(2)?
2
int mystery (int w) { if (w < 0) return 0; int x = mystery (w - 2); return w - x; } What is returned by the call mystery(5)?
3
A good use of recursion would be solving a maze. (True/ False)
True
Any problem that can be solved recursively can be solved iteratively (non-recursively). (True/False)
True
Any recursive definition must have a base case that causes the recursion to end. (True/False)
True
Method f1 calling method f1 is an example of recursion. (True/False)
True
Recursion is a technique in which a method calls itself. (True/False)
True
void arrayTraverse (int current, int[ ] array) { if (current == array.length) return; // process array[current] arrayTraverse ( _________, array); } How should arrayTraverse be called to process every element in the array nums?
arrayTraverse(0,nums)
A good use of recursion would be to calculate how much an employee should be paid given their pay rate and hours worked. (True/False)
False
A recursive method must have a return value. (True/False)
False
Recursion is the same thing as iteration. (True/False)
False
When used correctly, recursion programs are relatively short and elegant. (True/False)
True
void arrayTraverse (int current, int[ ] array) { if (current == array.length) return; // process array[current] arrayTraverse ( _________, array); } The method arrayTraverse recursively visits each element in an array, processing each element in some way. Which expression should go in the blank?
current + 1
int mystery (int w) { if (w < 0) return 0; int x = mystery (w - 2); return w - x; } Which line of code is the base case in the recursion?
if (w < 0)
int mystery (int w) { if (w < 0) return 0; int x = mystery (w - 2); return w - x; } Which line would you remove to create infinite recursion?
if (w < 0)
int mystery (int w) { if (w < 0) return 0; int x = mystery (w - 2); return w - x; } Which line of code is the recursion?
int x = mystery (w - 2)