Deck 14: Recursion
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/43
Play
Full screen (f)
Deck 14: Recursion
1
A class member function may be recursive.
True
2
For every recursive solution,there is an)______________ solution.
iterative
3
In a recursive function,the statements)that invoke the function again are called the ______________.
recursive calls
4
You may have at most 1 recursive call in a recursive function.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
5
Every recursive definition may be rewritten iteratively.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
6
The operating system uses a stack to control recursion.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
7
Recursive functions must return a value.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
8
How do you ensure that your function does not have infinite recursion?
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
9
A definition that defines a concept or a formula in terms of the concept or formula is called
A)a recursive definition
B)indecision
C)iteration
D)reduction
A)a recursive definition
B)indecision
C)iteration
D)reduction
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
10
Recursive functions always execute faster than an iterative function.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
11
If you try to solve a problem recursively,you should
A)find all the stopping cases and the values if needed at that case
B)find a recursive call that will lead towards one of the stopping cases
C)all of the above
D)none of the above
A)find all the stopping cases and the values if needed at that case
B)find a recursive call that will lead towards one of the stopping cases
C)all of the above
D)none of the above
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
12
In a recursive power function that calculates some base to the exp power,what is the recursive call?
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
13
In a recursive power function that calculates some base to a positive exp power,at what value of exp do you stop? The function will continually multiply the base times the value returned by the power function with the base argument one smaller.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
14
Only functions that do not return a value may be recursive.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
15
A stack exhibits what behavior?
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
16
If the recursive function call does not lead towards a stopping case,you have ______________.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
17
A recursive function is a function that ______________.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
18
Recursive functions may return any type of value
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
19
Not all recursive definitions may be written iteratively.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
20
There can be more than one stopping case in a recursive function.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
21
The recursive definition of a Fibonacci Number is Fn)= Fn-1)+ Fn-2),where F0)=1 and F1)=1.What is the value of Fib5)?
A)8
B)5
C)2
D)1
A)8
B)5
C)2
D)1
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
22
What is the output of the following code fragment?
Int f1int base,int limit)
{
Ifbase > limit)
Return -1;
Else
Ifbase == limit)
Return 1;
Else
Return base * f1base+1,limit);
}
Int main)
{
Cout << f12,4)<Return 0;
}
A)2
B)3
C)-1
D)6
Int f1int base,int limit)
{
Ifbase > limit)
Return -1;
Else
Ifbase == limit)
Return 1;
Else
Return base * f1base+1,limit);
}
Int main)
{
Cout << f12,4)<
}
A)2
B)3
C)-1
D)6
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
23
In order for the binary search to work correctly
A)the list must be sorted
B)the item must exist
C)all of the above
D)none of the above
A)the list must be sorted
B)the item must exist
C)all of the above
D)none of the above
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
24
A stack exhibits _________ behavior.
A)first in first out
B)last in last out
C)last out first in
D)undefined
A)first in first out
B)last in last out
C)last out first in
D)undefined
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
25
Given the following recursive function definition,what is the stopping case?
Void towerschar source,char dest,char help,int numDisks)
{
IfnumDisks<1)
{
Return;
}
Else
{
Towerssource,help,dest,numDisks-1);
Cout << "Move disk from " << source << " to " <Towershelp,dest,source,numDisks-1);
}
}
A)numDisks == 1
B)numDisks >1
C)numDisks < 1
D)numDisks =0
Void towerschar source,char dest,char help,int numDisks)
{
IfnumDisks<1)
{
Return;
}
Else
{
Towerssource,help,dest,numDisks-1);
Cout << "Move disk from " << source << " to " <
}
}
A)numDisks == 1
B)numDisks >1
C)numDisks < 1
D)numDisks =0
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
26
What is wrong with the following recursive function? It should print out the array backwards.
Void printint array[],int start,int size)
{
Ifstart == size)
Return;
Else
{
Printarray,start-1,size);
Cout << array[start] << endl;
}
}
A)infinite recursion
B)the stopping condition is wrong
C)the recursive call is wrong
D)A and C
E)nothing
Void printint array[],int start,int size)
{
Ifstart == size)
Return;
Else
{
Printarray,start-1,size);
Cout << array[start] << endl;
}
}
A)infinite recursion
B)the stopping condition is wrong
C)the recursive call is wrong
D)A and C
E)nothing
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
27
In the binary search program,each time through the list,we cut the list approximately
A)in thirds
B)in quarters
C)by one element
D)in half
A)in thirds
B)in quarters
C)by one element
D)in half
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
28
What is the output of the following code fragment?
Int f1int n,int m)
{
Ifn < m)
Return 0;
Else ifn==m)
Return m+ f1n-1,m);
Else
Return n+ f1n-2,m-1);
}
Int main)
{
Cout << f15,4);
Return 0;
}
A)0
B)2
C)4
D)8
E)infinite recursion
Int f1int n,int m)
{
Ifn < m)
Return 0;
Else ifn==m)
Return m+ f1n-1,m);
Else
Return n+ f1n-2,m-1);
}
Int main)
{
Cout << f15,4);
Return 0;
}
A)0
B)2
C)4
D)8
E)infinite recursion
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
29
If your program makes too many recursive function calls,your program will cause a ___________
A)stack underflow
B)activation overflow
C)stack overflow
D)syntax error
A)stack underflow
B)activation overflow
C)stack overflow
D)syntax error
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
30
The recursive definition of a Fibonacci Number is Fn)= Fn-1)+ Fn-2),where F0)=1 and F1)=1.What is the value of Fib3)?
A)8
B)5
C)2
D)1
A)8
B)5
C)2
D)1
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
31
Implementing a task recursively rather than iteratively generally
A)is slower
B)takes more storage memory)
C)is sometimes easier to program
D)all of the above
A)is slower
B)takes more storage memory)
C)is sometimes easier to program
D)all of the above
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
32
The recursive definition of a Fibonacci Number is Fn)= Fn-1)+ Fn-2),where F0)=1 and F1)=1.What would be the recursive function call in a recursive implementation of this?
A)return;
B)return fibn)+ fibn-1)
C)return fibn-1)+fibn-2)
D)return 1;
A)return;
B)return fibn)+ fibn-1)
C)return fibn-1)+fibn-2)
D)return 1;
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
33
What is the output of the following code fragment?
Int f1int base,int limit)
{
Ifbase > limit)
Return -1;
Else
Ifbase == limit)
Return 1;
Else
Return base * f1base+1,limit);
}
Int main)
{
Cout << f112,4)<Return 0;
}
A)2
B)3
C)-1
D)6
Int f1int base,int limit)
{
Ifbase > limit)
Return -1;
Else
Ifbase == limit)
Return 1;
Else
Return base * f1base+1,limit);
}
Int main)
{
Cout << f112,4)<
}
A)2
B)3
C)-1
D)6
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
34
What is the output of the following code fragment?
Int f1int n,int m)
{
Ifn < m)
Return 0;
Else ifn==m)
Return m+ f1n-1,m);
Else
Return n+ f1n-2,m-1);
}
Int main)
{
Cout << f15,4);
Return 0;
}
A)0
B)2
C)4
D)8
E)infinite recursion
Int f1int n,int m)
{
Ifn < m)
Return 0;
Else ifn==m)
Return m+ f1n-1,m);
Else
Return n+ f1n-2,m-1);
}
Int main)
{
Cout << f15,4);
Return 0;
}
A)0
B)2
C)4
D)8
E)infinite recursion
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
35
What is the output of the following code fragment?
Int f1int n,int m)
{
Ifn < m)
Return 0;
Else ifn==m)
Return m+ f1n-1,m);
Else
Return n+ f1n-2,m-1);
}
Int main)
{
Cout << f11,4);
Return 0;
}
A)0
B)2
C)4
D)8
E)infinite recursion
Int f1int n,int m)
{
Ifn < m)
Return 0;
Else ifn==m)
Return m+ f1n-1,m);
Else
Return n+ f1n-2,m-1);
}
Int main)
{
Cout << f11,4);
Return 0;
}
A)0
B)2
C)4
D)8
E)infinite recursion
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
36
The factorial of an integer is the product of that integer multiplied by all the positive non-zero integers less than that integer.So,5! ! is the mathematical symbol for factorial)is 5 * 4 * 3*2*1.4! is 4*3*2*1,so 5! could be written as 5*4!.So a recursive definition of factorial is n! is n*n-1)!,as long as n >1.1! is 1.What is the recursive call for this function fact)?
A)factn)*n;
B)factn-1)*n;
C)n-1)*factn)
D)factn-2)*n-1)
A)factn)*n;
B)factn-1)*n;
C)n-1)*factn)
D)factn-2)*n-1)
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
37
To ensure that your function recurses correctly and the proper result is reached,you must ensure that
A)all stopping cases are correct
B)all recursive calls lead to one of the stopping cases
C)for each case that involves recursion,that case returns the correct value provided all recursive calls in that case return the correct value.
D)all of the above
E)none of the above
A)all stopping cases are correct
B)all recursive calls lead to one of the stopping cases
C)for each case that involves recursion,that case returns the correct value provided all recursive calls in that case return the correct value.
D)all of the above
E)none of the above
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
38
The factorial of an integer is the product of that integer multiplied by all the positive non-zero integers less than that integer.So,5! ! is the mathematical symbol for factorial)is 5 * 4 * 3*2*1.4! is 4*3*2*1,so 5! could be written as 5*4!.So a recursive definition of factorial is n! is n*n-1)!,as long as n >1.1! is 1.What is the stopping case for this function?
A)n<1
B)n==0
C)n==1
D)none of the above
A)n<1
B)n==0
C)n==1
D)none of the above
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
39
Every time a recursive function call is executed,a new __________ is put on the top of the stack.
A)Activity frame
B)Activity record
C)program
D)Activation frame
A)Activity frame
B)Activity record
C)program
D)Activation frame
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
40
In the following function,how many recursive calls are there?
Void towerschar source,char dest,char help,int numDisks)
{
IfnumDisks<1)
{
Return;
}
Else
{
Towerssource,help,dest,numDisks-1);
Cout << "Move disk from " << source << " to " <Towershelp,dest,source,numDisks-1);
}
}
A)0
B)1
C)2
D)3
Void towerschar source,char dest,char help,int numDisks)
{
IfnumDisks<1)
{
Return;
}
Else
{
Towerssource,help,dest,numDisks-1);
Cout << "Move disk from " << source << " to " <
}
}
A)0
B)1
C)2
D)3
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
41
The square of n can be calculated by noting that squaren)= squaren-1)+ diffn-1).diffn)= diffn-1)+2.The square0)=0,diff0)=1.What is the stopping condition for this recursive definition?
A)n=1
B)n=0
C)n=-1
D)unknown
A)n=1
B)n=0
C)n=-1
D)unknown
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
42
What is the output of the following code fragment?
Int f1int x,int y)
{
Ifx<0 || y<0)
Return x-y;
Else
Return f1x-1,y)+ f1x,y-1);
}
Int main)
{
Cout << f11,2)<Return 0;
}
A)0
B)-1
C)5
D)-5
Int f1int x,int y)
{
Ifx<0 || y<0)
Return x-y;
Else
Return f1x-1,y)+ f1x,y-1);
}
Int main)
{
Cout << f11,2)<
}
A)0
B)-1
C)5
D)-5
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
43
What is the output of the following code fragment?
Int f1int x,int y)
{
Ifx<0 || y<0)
Return x-y;
Else
Return f1x-1,y)+ f1x,y-1);
}
Int main)
{
Cout << f12,1)<Return 0;
}
A)0
B)-1
C)5
D)-5
Int f1int x,int y)
{
Ifx<0 || y<0)
Return x-y;
Else
Return f1x-1,y)+ f1x,y-1);
}
Int main)
{
Cout << f12,1)<
}
A)0
B)-1
C)5
D)-5
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck