Question: QUESTION 1 Read the following code and determine what goes in blank #1. int Length(NodeType* list) // Pre: list has been initialized. // Post: Length
QUESTION 1
Read the following code and determine what goes in blank #1.
int Length(NodeType* list)
// Pre: list has been initialized.
// Post: Length returns the number of elements in list. No counter is kept as
// part of the list. This function is recursive.
{
if (______________) // 1
_______________; // 2
else _______ // 3
}
| A. | list == NULL | |
| B. | list->info = 0 | |
| C. | list->next == NULL | |
| D. | list != NULL | |
| E. | None of these answers is correct. |
10 points
QUESTION 2
What value does function mystery return when called with a value of 4?
int mystery ( int number ) {
if ( number <= 1 )
return 1;
else
return mystery( number - 1);
}
| A. | 1 | |
| B. | 2 | |
| C. | 4 | |
| D. | 24 | |
| E. | None of these answers are correct. |
10 points
QUESTION 3
Assume the following function:
void mystry(char ch, int n) {
if (n <= 0)
cout << endl;
else {
mystry(ch - 1, n - 1);
cout << ch;
mystry(ch + 1, n + 1);
}
}
what is the output when you call the function by mystry('M', 4); ?
| A. | M N O P | |
| B. | J K L M | |
| C. | infinite loop | |
| D. | J | |
| E. | None of these answers are correct. |
10 points
QUESTION 4
Which of the following statements could describe the general (recursive) case of a recursive algorithm?
In the following recursive function, which line(s) represent the general (recursive) case?
void PrintIt(int n ) // line 1
{ // line 2
if (n > 0) //line 3
{ //line4
cout << "Again" << endl; //line 5
PrintIt(n - 1); // line 6
} // line 7
else // line 8
cout << n << endl; // line 9
} // line 10
| A. | Lines 5 6 | |
| B. | Lines 5 9 | |
| C. | Lines 6 9 | |
| D. | Line | |
| E. | None of the above. |
10 points
QUESTION 5
For the following recursive function, find f(5):
int f(int n) { if (n == 0) return 0; else return n * f(n - 1); }
| A. | 120 | |
| B. | 60 | |
| C. | 1 | |
| D. | 0 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
